2 * Copyright (c) 2010 Mark Heily <mark@heily.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/socket.h>
35 e = malloc(sizeof(*e));
39 if (socketpair(AF_UNIX, SOCK_STREAM, 0, e->fd) < 0) {
43 if ((fcntl(e->fd[0], F_SETFL, O_NONBLOCK) < 0) ||
44 (fcntl(e->fd[1], F_SETFL, O_NONBLOCK) < 0)) {
55 eventfd_free(struct eventfd *e)
63 eventfd_raise(struct eventfd *e)
65 dbg_puts("raising event level");
66 if (write(e->fd[0], ".", 1) < 0) {
67 /* FIXME: handle EAGAIN and EINTR */
68 dbg_printf("write(2): %s", strerror(errno));
75 eventfd_lower(struct eventfd *e)
79 /* Reset the counter */
80 dbg_puts("lowering event level");
81 if (read(e->fd[1], &buf, sizeof(buf)) < 0) {
82 /* FIXME: handle EAGAIN and EINTR */
83 /* FIXME: loop so as to consume all data.. may need mutex */
84 dbg_printf("read(2): %s", strerror(errno));
91 eventfd_reader(struct eventfd *e)
97 eventfd_writer(struct eventfd *e)