eventfd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-07-20 zmq810150896 first version
  9. * 2024-03-28 TroyMitchell Add comments for all functions
  10. */
  11. #include <rtthread.h>
  12. #include <fcntl.h>
  13. #include <rtdevice.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <dfs_file.h>
  17. #include <dfs.h>
  18. #include "poll.h"
  19. #include "eventfd.h"
  20. #define EFD_SEMAPHORE (1 << 0)
  21. #define EFD_CLOEXEC O_CLOEXEC
  22. #define EFD_NONBLOCK O_NONBLOCK
  23. #define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
  24. #define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE)
  25. #define EFD_ULLONG_MAX (~0ULL)
  26. #define EVENTFD_MUTEX_NAME "eventfd"
  27. struct eventfd_ctx
  28. {
  29. rt_wqueue_t reader_queue;
  30. rt_wqueue_t writer_queue;
  31. rt_uint64_t count;
  32. unsigned int flags;
  33. struct rt_mutex lock;
  34. };
  35. #ifndef RT_USING_DFS_V2
  36. static int eventfd_close(struct dfs_file *file);
  37. static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
  38. static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count);
  39. static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count);
  40. #else
  41. static int eventfd_close(struct dfs_file *file);
  42. static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
  43. static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
  44. static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos);
  45. #endif
  46. static const struct dfs_file_ops eventfd_fops =
  47. {
  48. .close = eventfd_close,
  49. .poll = eventfd_poll,
  50. .read = eventfd_read,
  51. .write = eventfd_write,
  52. };
  53. /**
  54. * @brief Closes an event file descriptor.
  55. * @param file Pointer to the file descriptor structure.
  56. * @return 0 on success, otherwise an error code.
  57. */
  58. static int eventfd_close(struct dfs_file *file)
  59. {
  60. struct eventfd_ctx *ctx = file->vnode->data;
  61. if (file->vnode->ref_count == 1)
  62. {
  63. rt_mutex_detach(&ctx->lock);
  64. rt_free(ctx);
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @brief Polls an event file descriptor for events.
  70. * @param file Pointer to the file descriptor structure.
  71. * @param req Pointer to the poll request structure.
  72. * @return Events that occurred on the file descriptor.
  73. */
  74. static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req)
  75. {
  76. struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
  77. int events = 0;
  78. rt_uint64_t count;
  79. count = ctx->count;
  80. rt_poll_add(&ctx->reader_queue, req);
  81. if (count > 0)
  82. events |= POLLIN;
  83. if (count == EFD_ULLONG_MAX)
  84. events |= POLLERR;
  85. if ((EFD_ULLONG_MAX - 1) > count)
  86. events |= POLLOUT;
  87. return events;
  88. }
  89. #ifndef RT_USING_DFS_V2
  90. /**
  91. * @brief Reads data from an event file descriptor.
  92. * @param file Pointer to the file descriptor structure.
  93. * @param buf Pointer to the buffer to read data into.
  94. * @param count Maximum number of bytes to read.
  95. * @return Number of bytes read on success, otherwise an error code.
  96. */
  97. static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count)
  98. #else
  99. /**
  100. * @brief Reads data from an event file descriptor.
  101. * @param file Pointer to the file descriptor structure.
  102. * @param buf Pointer to the buffer to read data into.
  103. * @param count Maximum number of bytes to read.
  104. * @param pos Pointer to the file position (not used).
  105. * @return Number of bytes read on success, otherwise an error code.
  106. */
  107. static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  108. #endif
  109. {
  110. struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
  111. rt_uint64_t counter_num = 0;
  112. rt_uint64_t *buffer;
  113. if (count < sizeof(counter_num))
  114. return -EINVAL;
  115. buffer = (rt_uint64_t *)buf;
  116. rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
  117. if (ctx->count <= 0)
  118. {
  119. if (file->flags & O_NONBLOCK)
  120. {
  121. rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT);
  122. rt_mutex_release(&ctx->lock);
  123. return -EAGAIN;
  124. }
  125. else
  126. {
  127. /* In this case, when the data is read in blocked mode, when ctx->count is 0, the mutex needs to be released and wait for writing */
  128. rt_mutex_release(&ctx->lock);
  129. rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT);
  130. rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER);
  131. rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
  132. }
  133. }
  134. if (ctx->flags & EFD_SEMAPHORE)
  135. {
  136. counter_num = 1;
  137. }
  138. else
  139. {
  140. counter_num = ctx->count;
  141. }
  142. ctx->count -= counter_num;
  143. (*buffer) = counter_num;
  144. rt_mutex_release(&ctx->lock);
  145. return sizeof(counter_num);
  146. }
  147. #ifndef RT_USING_DFS_V2
  148. /**
  149. * @brief Writes data to an event file descriptor.
  150. * @param file Pointer to the file descriptor structure.
  151. * @param buf Pointer to the buffer containing data to write.
  152. * @param count Number of bytes to write.
  153. * @return Number of bytes written on success, otherwise an error code.
  154. */
  155. static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count)
  156. #else
  157. /**
  158. * @brief Writes data to an event file descriptor.
  159. * @param file Pointer to the file descriptor structure.
  160. * @param buf Pointer to the buffer containing data to write.
  161. * @param count Number of bytes to write.
  162. * @param pos Pointer to the file position (not used).
  163. * @return Number of bytes written on success, otherwise an error code.
  164. */
  165. static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
  166. #endif
  167. {
  168. struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
  169. rt_ssize_t ret = 0;
  170. rt_uint64_t counter_num;
  171. if (count < sizeof(counter_num))
  172. return -EINVAL;
  173. counter_num = *(rt_uint64_t *)buf;
  174. if (counter_num == EFD_ULLONG_MAX)
  175. return -EINVAL;
  176. ret = -EAGAIN;
  177. rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
  178. if ((EFD_ULLONG_MAX - ctx->count) > counter_num)
  179. {
  180. ret = sizeof(counter_num);
  181. }
  182. else if (!(file->flags & O_NONBLOCK))
  183. {
  184. for (;;)
  185. {
  186. if ((EFD_ULLONG_MAX - ctx->count) >= counter_num)
  187. {
  188. ret = sizeof(counter_num);
  189. break;
  190. }
  191. /* Release the mutex to avoid a deadlock */
  192. rt_mutex_release(&ctx->lock);
  193. rt_wqueue_wait(&ctx->writer_queue, 0, RT_WAITING_FOREVER);
  194. rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
  195. }
  196. }
  197. if (ret > 0)
  198. {
  199. ctx->count += counter_num;
  200. rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN);
  201. }
  202. rt_mutex_release(&ctx->lock);
  203. return ret;
  204. }
  205. /**
  206. * @brief Creates an event file descriptor.
  207. * @param df Pointer to the file descriptor structure.
  208. * @param count Initial value of the event counter.
  209. * @param flags Flags for the event file descriptor.
  210. * @return 0 on success, otherwise an error code.
  211. */
  212. static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags)
  213. {
  214. struct eventfd_ctx *ctx = RT_NULL;
  215. rt_err_t ret = 0;
  216. ctx = (struct eventfd_ctx *)rt_malloc(sizeof(struct eventfd_ctx));
  217. if (ctx == RT_NULL)
  218. {
  219. ret = -ENOMEM;
  220. }
  221. else
  222. {
  223. ctx->count = count;
  224. ctx->flags = flags;
  225. flags &= EFD_SHARED_FCNTL_FLAGS;
  226. flags |= O_RDWR;
  227. rt_mutex_init(&ctx->lock, EVENTFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  228. rt_wqueue_init(&ctx->reader_queue);
  229. rt_wqueue_init(&ctx->writer_queue);
  230. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  231. if (df->vnode)
  232. {
  233. dfs_vnode_init(df->vnode, FT_NONLOCK, &eventfd_fops);
  234. df->vnode->data = ctx;
  235. df->flags = flags;
  236. }
  237. else
  238. {
  239. rt_mutex_detach(&ctx->lock);
  240. rt_free(ctx);
  241. ret = -ENOMEM;
  242. }
  243. #ifdef RT_USING_DFS_V2
  244. df->fops = &eventfd_fops;
  245. #endif
  246. }
  247. return ret;
  248. }
  249. /**
  250. * @brief Internal function to create an event file descriptor.
  251. * @param count Initial value of the event counter.
  252. * @param flags Flags for the event file descriptor.
  253. * @return File descriptor on success, otherwise an error code.
  254. */
  255. static int do_eventfd(unsigned int count, int flags)
  256. {
  257. struct dfs_file *file;
  258. int fd;
  259. int status;
  260. rt_ssize_t ret = 0;
  261. if (flags & ~EFD_FLAGS_SET)
  262. {
  263. rt_set_errno(EINVAL);
  264. return -1;
  265. }
  266. fd = fd_new();
  267. if (fd >= 0)
  268. {
  269. ret = fd;
  270. file = fd_get(fd);
  271. status = rt_eventfd_create(file, count, flags);
  272. if (status < 0)
  273. {
  274. fd_release(fd);
  275. rt_set_errno(-status);
  276. ret = -1;
  277. }
  278. }
  279. else
  280. {
  281. rt_set_errno(-fd);
  282. ret = -1;
  283. }
  284. return ret;
  285. }
  286. /**
  287. * @brief Creates an event file descriptor with the specified count and flags.
  288. * @param count Initial value of the event counter.
  289. * @param flags Flags for the event file descriptor.
  290. * @return File descriptor on success, otherwise an error code.
  291. */
  292. int eventfd(unsigned int count, int flags)
  293. {
  294. return do_eventfd(count, flags);
  295. }