1 /*
2 * Copyright (c) 2020 Tobias Svehagen
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_
8 #define ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_
9
10 #include <zephyr.h>
11 #include <sys/fdtable.h>
12 #include <sys/types.h>
13
14 #include <fcntl.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #define EFD_IN_USE 0x1
21 #define EFD_SEMAPHORE 0x2
22 #define EFD_NONBLOCK O_NONBLOCK
23 #define EFD_FLAGS_SET (EFD_SEMAPHORE | EFD_NONBLOCK)
24
25 typedef uint64_t eventfd_t;
26
27 /**
28 * @brief Create a file descriptor for event notification
29 *
30 * The returned file descriptor can be used with POSIX read/write calls or
31 * with the eventfd_read/eventfd_write functions.
32 *
33 * It also supports polling and by including an eventfd in a call to poll,
34 * it is possible to signal and wake the polling thread by simply writing to
35 * the eventfd.
36 *
37 * When using read() and write() on an eventfd, the size must always be at
38 * least 8 bytes or the operation will fail with EINVAL.
39 *
40 * @return New eventfd file descriptor on success, -1 on error
41 */
42 int eventfd(unsigned int initval, int flags);
43
44 /**
45 * @brief Read from an eventfd
46 *
47 * If call is successful, the value parameter will have the value 1
48 *
49 * @param fd File descriptor
50 * @param value Pointer for storing the read value
51 *
52 * @return 0 on success, -1 on error
53 */
eventfd_read(int fd,eventfd_t * value)54 static inline int eventfd_read(int fd, eventfd_t *value)
55 {
56 const struct fd_op_vtable *efd_vtable;
57 struct k_mutex *lock;
58 ssize_t ret;
59 void *obj;
60
61 obj = z_get_fd_obj_and_vtable(fd, &efd_vtable, &lock);
62
63 (void)k_mutex_lock(lock, K_FOREVER);
64
65 ret = efd_vtable->read(obj, value, sizeof(*value));
66
67 k_mutex_unlock(lock);
68
69 return ret == sizeof(eventfd_t) ? 0 : -1;
70 }
71
72 /**
73 * @brief Write to an eventfd
74 *
75 * @param fd File descriptor
76 * @param value Value to write
77 *
78 * @return 0 on success, -1 on error
79 */
eventfd_write(int fd,eventfd_t value)80 static inline int eventfd_write(int fd, eventfd_t value)
81 {
82 const struct fd_op_vtable *efd_vtable;
83 struct k_mutex *lock;
84 ssize_t ret;
85 void *obj;
86
87 obj = z_get_fd_obj_and_vtable(fd, &efd_vtable, &lock);
88
89 (void)k_mutex_lock(lock, K_FOREVER);
90
91 ret = efd_vtable->write(obj, &value, sizeof(value));
92
93 k_mutex_unlock(lock);
94
95 return ret == sizeof(eventfd_t) ? 0 : -1;
96 }
97
98 #ifdef __cplusplus
99 }
100 #endif
101
102 #endif /* ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_ */
103