Android BlueDroid分析: Linux中的Eventfd

来源

Linux专有的同步机制, 需要内核高于2.6.22, 下面是引用 <<The Linux Programming Interface>>这本书中的说明:

从man手册中有如下的描述:

 eventfd() creates an "eventfd object" that can be used as an event wait/notify mechanism by user-space applications, and by the kernel to notify user-space applications of events.  The object contains
       an unsigned 64-bit integer (uint64_t) counter that is maintained by the kernel.  This counter is initialized with the value specified in the argument initval.


特征

在内核创建eventfd object, 有一个an unsigned 64-bit integer (uint64_t) counter, 这个值由内核管理

正如eventfd中包含的fd, 可以知道eventfd创建的这个eventfd对象和file open类似,然后接下来的操作都是由这个fd来指定, 而在linux下面fd的意义对于不同的进程是不一样的,因此eventfd仅仅适用于进程内(Parent child Process)的通讯, 而Android里面借助binder是可以跨进程传递fd.


适用范围

适用于Processes(父子)之间通讯,用于 an event wait/notify mechanism模型, 和文件使用write/read类似,当没有设置O_NONBLOCK flag的时候:

  • 当文件有数据的时候   <--> eventfd中的Int不为0 <--> 会马上返回
  • 当文件有数据的时候   <--> eventfd中的Int==0  <--> block等待值不为0/等待buffer不为空
  • Wait:     Read --> Object关联的INT值为0 --> Blocking
  • Notify:  Write nonzero给关联INT--> Notify --> Read return

而这个Wait/Notify模型,一般用于控制程序的执行顺序,与Semaphore模型类似, 因此, eventfd的打开flag中有一个EFD_SEMAPHORE选项


如果期望不要block可以使用fcntl来更改,例如下面函数来源于BlueDroid中(semophore.c):

bool semaphore_try_wait(semaphore_t *semaphore) {
  assert(semaphore != NULL);
  assert(semaphore->fd != INVALID_FD);

  int flags = fcntl(semaphore->fd, F_GETFL);
  if (flags == -1) {
    LOG_ERROR("%s unable to get flags for semaphore fd: %s", __func__, strerror(errno));
    return false;
  }
  if (fcntl(semaphore->fd, F_SETFL, flags | O_NONBLOCK) == -1) {
    LOG_ERROR("%s unable to set O_NONBLOCK for semaphore fd: %s", __func__, strerror(errno));
    return false;
  }

  eventfd_t value;
  if (eventfd_read(semaphore->fd, &value) == -1)
    return false;

  if (fcntl(semaphore->fd, F_SETFL, flags) == -1)
    LOG_ERROR("%s unable to resetore flags for semaphore fd: %s", __func__, strerror(errno));
  return true;
}


使用方法

可以使用glibc的wrap函数来使用:

     The GNU C library defines an additional type, and two functions that attempt to abstract some of the details of reading and writing on an eventfd file descriptor:

           typedef uint64_t eventfd_t;

           int eventfd_read(int fd, eventfd_t *value);
           int eventfd_write(int fd, eventfd_t value);

参考

http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

Linux Programming Interface的作者对IPC的Presentation视频

http://zorksylar.github.io/epoll-eventfd.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值