linuxeventfd的初始值和在可轮询L中的使用

2024-04-29 14:58:03 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

^{}是从2.6.22开始在Linux中使用的新系统调用。呼叫签名是

int eventfd(unsigned int initval, int flags);

我正利用这个调用构造一个在轮询循环中可用的新光剑类。在

Python^{}对象以未阻塞状态启动。我使用eventfd要求初始值为非零。如果值在内部是uint64_t

The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the kernel.

为什么初始值参数是unsigned int类型?在

公开细节

如果我使用非零值作为Lockunlocked状态,那么释放是通过编写来完成的。没有介入收购的多个版本是错误的,需要失败。这要求在事件对象包含非零值时写入失败。在它的默认模式下,事件对象将在阻塞写调用之前,添加到它的值,直到(uint64_t)0xfffffffffffffffe。为了检测这种情况,我将执行非阻塞写入操作,将该值推到该最大值之上,从而触发此情况:

If the addition would cause the counter's value to exceed the maximum, then the write(2) either blocks until a read(2) is performed on the file descriptor, or fails with the error EAGAIN if the file descriptor has been made non‐ blocking.


Tags: the对象islinux系统counter事件情况
1条回答
网友
1楼 · 发布于 2024-04-29 14:58:03

如果我没听错,你想让这个野兽的状态在0和{}之间交替?在

初始值的类型可能只是因为历史原因。一旦这样一个接口被使用,它就会被粘住,并且很难在以后更改它。在

如果您需要该值的初始值是UINT64_MAX-1,为什么不直接用0参数调用eventfd,然后在将文件描述符传播给其他任何人之前,立即使用UINT64_MAX-1执行{}:

int ev = eventfd(0, 0);
write(ev, &(uint64_t const){ UINT64_MAX-1 }, sizeof(uint64_t));

(好吧,你会加上错误检查代码,对吧)

相关问题 更多 >