summary(仅考虑eventfd的flags为0 ,同时eventfd counter 没有 exceed the maximum):
the eventfd counter has a nonzero value, then a read returns 8 bytes containing that value, and the counter’s value is reset to zero;
If the eventfd counter is zero at the time of the call to read, then the call blocks until the counter becomes nonzero;
A write call adds the 8-byte integer value supplied in its buffer to the counter;
eventfd_signal(struct eventfd_ctx *ctx, __u64 n): Adds @n to the eventfd counter.
2. Data struct
eventfd_ctx结构的形式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
structeventfd_ctx { structkrefkref; wait_queue_head_t wqh; /* * Every time that a write(2) is performed on an eventfd, the * value of the __u64 being written is added to "count" and a * wakeup is performed on "wqh". A read(2) will return the "count" * value to userspace, and will reset "count" to zero. The kernel * side eventfd_signal() also, adds to the "count" counter and * issue a wakeup. */ __u64 count; unsignedint flags; int id; };
/** * eventfd_signal - Adds @n to the eventfd counter. * @ctx: [in] Pointer to the eventfd context. * @n: [in] Value of the counter to be added to the eventfd internal counter. */ __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n) { ... if (ULLONG_MAX - ctx->count < n) n = ULLONG_MAX - ctx->count; ctx->count += n; if (waitqueue_active(&ctx->wqh)) //在该eventfd上read阻塞的进程 wake_up_locked_poll(&ctx->wqh, EPOLLIN); ...