tty_ldisc_ref_wait函数
时间: 2023-10-11 17:08:41 浏览: 59
在 Linux 内核中,tty_ldisc_ref_wait 函数用于等待 tty line discipline 的引用计数变为零。
以下是 tty_ldisc_ref_wait 函数的原型:
```c
void tty_ldisc_ref_wait(struct tty_ldisc *ld)
```
该函数的参数是一个 tty_ldisc 结构体,表示要等待的 tty line discipline。
函数的实现如下:
```c
void tty_ldisc_ref_wait(struct tty_ldisc *ld)
{
wait_event(ld->w_wait, !atomic_read(&ld->refcount));
}
```
该函数使用 wait_event 宏等待 tty line discipline 的引用计数变为零。wait_event 宏会阻塞当前进程,直到第一个参数中的条件为真。
在本例中,第一个参数是 ld->w_wait,表示等待队列头。第二个参数是 !atomic_read(&ld->refcount),表示等待 tty line discipline 的引用计数变为零。
当 tty line discipline 的引用计数变为零时,其他进程将会调用 tty_ldisc_put 函数来释放该 tty line discipline 的引用,从而唤醒等待在 ld->w_wait 上的进程。
相关问题
tty_ldisc_try函数
tty_ldisc_try函数是一个Linux内核函数,主要用于尝试为给定的tty(终端)设备设置一个新的行规则(line discipline)。行规则是用于解释和处理在tty设备上输入和输出的字符流的一组规则和程序。
函数定义如下:
int tty_ldisc_try(struct tty_struct *tty, int disc);
函数参数:
- tty:指向tty_struct结构体的指针,表示要设置行规则的tty设备。
- disc:表示要设置的行规则编号。
函数返回值:
- 如果行规则设置成功,则返回0。
- 如果设置失败,则返回-EINVAL。
函数功能:
该函数尝试将给定的tty设备的行规则设置为指定的行规则编号。如果设置成功,则返回0;否则返回-EINVAL。如果tty设备已经有一个行规则,则该函数将首先关闭该行规则,然后再尝试设置新的行规则。
注意事项:
- 该函数应该在获取tty设备的锁之后调用,以避免并发设置行规则。
- 行规则编号必须是已经注册的行规则之一,否则设置将失败。
- 如果要设置的行规则与当前行规则相同,则该函数将返回成功,而不执行任何操作。
示例代码:
以下示例代码展示了如何使用tty_ldisc_try函数来设置tty设备的行规则:
#include <linux/tty.h>
struct tty_struct *mytty;
int set_ldisc(int disc)
{
int res;
/* 获取tty设备的锁 */
tty_lock(mytty);
/* 尝试设置行规则 */
res = tty_ldisc_try(mytty, disc);
/* 释放tty设备的锁 */
tty_unlock(mytty);
return res;
}
在上面的示例代码中,set_ldisc函数尝试将全局变量mytty指向的tty设备的行规则设置为指定的行规则编号。函数首先获取tty设备的锁,然后调用tty_ldisc_try函数来尝试设置行规则。最后,函数释放tty设备的锁,并将设置结果返回。
dw8250_set_ldisc 函数
dw8250_set_ldisc 函数是 Linux 内核串行驱动程序中的一个函数,用于设置串行端口的行规程和字符规程。
函数原型:
void dw8250_set_ldisc(struct uart_port *port);
参数说明:
- port:指向串口设备结构体的指针。
函数功能:
dw8250_set_ldisc 函数通过调用 tty_set_ldisc 函数设置串口端口的行规程和字符规程。在设置行规程和字符规程之前,该函数会先判断当前串口是否已经打开。如果串口已经打开,那么会通过 tty_set_ldisc 函数重新设置行规程和字符规程。如果串口未打开,则不进行任何操作。
函数实现:
函数实现的具体细节可以参考 Linux 内核源代码中的 drivers/tty/serial/dw8250.c 文件。以下是该文件中 dw8250_set_ldisc 函数的代码实现:
void dw8250_set_ldisc(struct uart_port *port)
{
if (port->state != NULL && port->state->port.tty != NULL) {
tty_set_ldisc(port->state->port.tty, N_TTY);
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)