tty 的流控

1]有关写操作:

    1.1如果上层使用poll系统调用监测写事件,就是知道什么时候可以进行写操作,tty必须实现的接口

    其中有两个tty_ops write相关的函数,其中tty_chars_in_buffer是必须实现的
    unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
    {
        if (tty->ops->write && !tty_is_writelocked(tty) &&
            tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
            tty_write_room(tty) > 0)
        mask |= POLLOUT | POLLWRNORM;
        return mask;

    }

    1.1.1 tty_chars_in_buffer - characters pending, *Return the number of bytes of data in the device output queue.

    int tty_chars_in_buffer(struct tty_struct *tty)
    {
    if (tty->ops->chars_in_buffer)
        return tty->ops->chars_in_buffer(tty);
    else
        return 0;
    }

    1.1.2 tty_write_room:没有该函数,就没有流控,就可能丢失数据

     *    tty_write_room        -    write queue space
     *    @tty: terminal
     *
     *    Return the number of bytes that can be queued to this device
     *    at the present time. The result should be treated as a guarantee
     *    and the driver cannot offer a value it later shrinks by more than
     *    the number of bytes written. If no method is provided 2K is always
     *    returned and data may be lost as there will be no flow control.
 
    int tty_write_room(struct tty_struct *tty)
    {
        if (tty->ops->write_room)
            return tty->ops->write_room(tty);
        return 2048;
    }


  1.2 使用流控函数tty_throttle

      这个函数只在读操作中使用,不用在writ 操作中。
    
  总结:设想这样一种情形:用户要写一段数据。
  如果使用poll监测是否可写,如果可写则写入数据,这种方法有有个地方唤醒
  tty->write_wait,或者poll的参数带个timeout,没timeout就调用n_tty_poll查询一次write_room的状态。
  如果不使用poll函数,直接写,如果底层函数在虽然请求为n个字节的大写,却返回为0,则睡眠当前进程,等待 tty->write_wait;
     如果底层函数在虽然请求为n个字节的大写,不返回为0,直到写完数据才返回,则不会睡眠当前进程。

2]有关读操作

    2.1如果上层使用poll系统调用监测读事件,就是知道什么时候可以进行读操作,tty必须实现的接口

    static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
    {
        if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
            mask |= POLLIN | POLLRDNORM;
        return mask;
    }

    2.1.1     input_available_p

    该函数调用tty_flush_to_ldisc触发一次tty_buff到tty_read buffer的一次copy,最后判断read_cnt有多少数据需要读出
    static inline int input_available_p(struct tty_struct *tty, int amt)
    {
        tty_flush_to_ldisc(tty);
        if (tty->icanon && !L_EXTPROC(tty)) {
            if (tty->canon_data)
                return 1;
        } else if (tty->read_cnt >= (amt ? amt : 1))
            return 1;

        return 0;
    }

     2.2 使用流控函数tty_throttle

    2.2.1 在数据从tty_buffer到tty_read buffer的work中调用tty_throttle

    flush_to_ldisc -> n_tty_receive_buf ->{
         * Check the remaining room for the input canonicalization
         * mode.  We don't want to throttle the driver if we're in
         * canonical mode and don't have a newline yet!
        if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
            tty_throttle(tty);
    }
    void tty_throttle(struct tty_struct *tty)
    {
        mutex_lock(&tty->termios_mutex);
        /* check TTY_THROTTLED first so it indicates our state */
        if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
            tty->ops->throttle)
            tty->ops->throttle(tty);
        mutex_unlock(&tty->termios_mutex);
    }

    2.2.2 n_tty_read 调用tty_unthrottle

    n_tty_read->{
        /* If there is enough space in the read buffer now, let the
         * low-level driver know. We use n_tty_chars_in_buffer() to
         * check the buffer, as it now knows about canonical mode.
         * Otherwise, if the driver is throttled and the line is
         * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
         * we won't get any more characters.
         */
        if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
            n_tty_set_room(tty);
            check_unthrottle(tty);
        }
    }
    static void check_unthrottle(struct tty_struct *tty)
    {
        if (tty->count)
            tty_unthrottle(tty);
    }
    何时tty->count变化啊?tty_standard_install and tty_reopen
    
    void tty_unthrottle(struct tty_struct *tty)
    {
    mutex_lock(&tty->termios_mutex);
    if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
        tty->ops->unthrottle)
        tty->ops->unthrottle(tty);
    mutex_unlock(&tty->termios_mutex);
    }

    2.2.3 如果没有实现流控函数 tty_un/throttle,结果会是怎样?

    只要有数据产生,n_tty_receive_buf函数就会把数据从tty_buf拷贝到tty read_buffer这个循环buffer中,这就会造成数据的丢失。
    所以数据要及时读出,或者增大read buffer的大小。
  总结:设想这样一种情形:用户要读一段数据。
  如果使用poll监测是否有可读的数据,如果可有就调用读函数。读操作中有多处调用唤醒tty->read_wait可以唤醒poll。
  如果不使用poll函数,直接读,如果没有数据可读则睡眠当前进程,等待 tty->read_wait被唤醒;
 **/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值