6188 add support for eventfd
[illumos-gate.git] / usr / src / lib / libc / port / sys / eventfd.c
blobf165491cc13e08c41f6ce68ab45b60f060add23a
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2015, Joyent, Inc. All rights reserved.
16 #include <sys/eventfd.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <fcntl.h>
22 int
23 eventfd(unsigned int initval, int flags)
25 int oflags = O_RDWR;
26 uint64_t val = initval;
27 int fd;
29 if (flags & ~(EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE)) {
30 errno = EINVAL;
31 return (-1);
34 if (flags & EFD_NONBLOCK)
35 oflags |= O_NONBLOCK;
37 if (flags & EFD_CLOEXEC)
38 oflags |= O_CLOEXEC;
40 if ((fd = open("/dev/eventfd", oflags)) < 0)
41 return (-1);
43 if ((flags & EFD_SEMAPHORE) &&
44 ioctl(fd, EVENTFDIOC_SEMAPHORE, 0) != 0) {
45 (void) close(fd);
46 return (-1);
49 if (write(fd, &val, sizeof (val)) < sizeof (val)) {
50 (void) close(fd);
51 return (-1);
54 return (fd);
57 int
58 eventfd_read(int fd, eventfd_t *valp)
60 return (read(fd, valp, sizeof (*valp)) < sizeof (*valp) ? -1 : 0);
63 int
64 eventfd_write(int fd, eventfd_t val)
66 return (write(fd, &val, sizeof (val)) < sizeof (val) ? -1 : 0);