epoll.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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-29 zmq810150896 first version
  9. * 2024/03/26 TroyMitchelle Add comments for all functions, members within structure members and fix incorrect naming of triggered
  10. */
  11. #include <rtthread.h>
  12. #include <fcntl.h>
  13. #include <stdint.h>
  14. #include <unistd.h>
  15. #include <dfs_file.h>
  16. #include "sys/epoll.h"
  17. #include "poll.h"
  18. #include <lwp_signal.h>
  19. #define EPOLL_MUTEX_NAME "EVENTEPOLL"
  20. #define EFD_SHARED_EPOLL_TYPE (EPOLL_CTL_ADD | EPOLL_CTL_DEL | EPOLL_CTL_MOD)
  21. #define EPOLLINOUT_BITS (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
  22. #define EPOLLEXCLUSIVE_BITS (EPOLLINOUT_BITS | EPOLLERR | EPOLLHUP | \
  23. EPOLLET | EPOLLEXCLUSIVE)
  24. struct rt_eventpoll;
  25. /* Monitor queue */
  26. struct rt_fd_list
  27. {
  28. rt_uint32_t revents; /**< Monitored events */
  29. struct epoll_event epev; /**< Epoll event structure */
  30. rt_pollreq_t req; /**< Poll request structure */
  31. struct rt_eventpoll *ep; /**< Pointer to the associated event poll */
  32. struct rt_wqueue_node wqn; /**< Wait queue node */
  33. int exclusive; /**< Indicates if the event is exclusive */
  34. rt_bool_t is_rdl_node; /**< Indicates if the node is in the ready list */
  35. int fd; /**< File descriptor */
  36. struct rt_fd_list *next; /**< Pointer to the next file descriptor list */
  37. rt_slist_t rdl_node; /**< Ready list node */
  38. };
  39. struct rt_eventpoll
  40. {
  41. rt_uint32_t triggered; /**< Indicates if the wait thread is triggered */
  42. rt_wqueue_t epoll_read; /**< Epoll read queue */
  43. rt_thread_t polling_thread; /**< Polling thread */
  44. struct rt_mutex lock; /**< Mutex lock */
  45. struct rt_fd_list *fdlist; /**< Monitor list */
  46. int eventpoll_num; /**< Number of ready lists */
  47. rt_pollreq_t req; /**< Poll request structure */
  48. struct rt_spinlock spinlock;/**< Spin lock */
  49. rt_slist_t rdl_head; /**< Ready list head */
  50. };
  51. static int epoll_close(struct dfs_file *file);
  52. static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req);
  53. static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req);
  54. static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event);
  55. static const struct dfs_file_ops epoll_fops =
  56. {
  57. .close = epoll_close,
  58. .poll = epoll_poll,
  59. };
  60. /**
  61. * @brief Closes the file descriptor list associated with epoll.
  62. *
  63. * This function closes the file descriptor list associated with epoll and frees the allocated memory.
  64. *
  65. * @param fdlist Pointer to the file descriptor list.
  66. *
  67. * @return Returns 0 on success.
  68. */
  69. static int epoll_close_fdlist(struct rt_fd_list *fdlist)
  70. {
  71. struct rt_fd_list *fre_node, *list;
  72. if (fdlist != RT_NULL)
  73. {
  74. list = fdlist;
  75. while (list->next != RT_NULL)
  76. {
  77. fre_node = list->next;
  78. if (fre_node->wqn.wqueue)
  79. rt_wqueue_remove(&fre_node->wqn);
  80. list->next = fre_node->next;
  81. rt_free(fre_node);
  82. }
  83. rt_free(fdlist);
  84. }
  85. return 0;
  86. }
  87. /**
  88. * @brief Closes the epoll file descriptor.
  89. *
  90. * This function closes the epoll file descriptor and cleans up associated resources.
  91. *
  92. * @param file Pointer to the file structure.
  93. *
  94. * @return Returns 0 on success.
  95. */
  96. static int epoll_close(struct dfs_file *file)
  97. {
  98. struct rt_eventpoll *ep;
  99. if (file->vnode->ref_count != 1)
  100. return 0;
  101. if (file->vnode)
  102. {
  103. if (file->vnode->data)
  104. {
  105. ep = file->vnode->data;
  106. if (ep)
  107. {
  108. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  109. if (ep->fdlist)
  110. {
  111. epoll_close_fdlist(ep->fdlist);
  112. }
  113. rt_mutex_release(&ep->lock);
  114. rt_mutex_detach(&ep->lock);
  115. rt_free(ep);
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. /**
  122. * @brief Polls the epoll file descriptor for events.
  123. *
  124. * This function polls the epoll file descriptor for events and updates the poll request accordingly.
  125. *
  126. * @param file Pointer to the file structure.
  127. * @param req Pointer to the poll request structure.
  128. *
  129. * @return Returns the events occurred on success.
  130. */
  131. static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req)
  132. {
  133. struct rt_eventpoll *ep;
  134. int events = 0;
  135. rt_base_t level;
  136. if (file->vnode->data)
  137. {
  138. ep = file->vnode->data;
  139. ep->req._key = req->_key;
  140. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  141. rt_poll_add(&ep->epoll_read, req);
  142. level = rt_spin_lock_irqsave(&ep->spinlock);
  143. if (!rt_slist_isempty(&ep->rdl_head))
  144. events |= POLLIN | EPOLLRDNORM | POLLOUT;
  145. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  146. rt_mutex_release(&ep->lock);
  147. }
  148. return events;
  149. }
  150. /**
  151. * @brief Callback function for the wait queue.
  152. *
  153. * This function is called when the file descriptor is ready for polling.
  154. *
  155. * @param wait Pointer to the wait queue node.
  156. * @param key Key associated with the wait queue node.
  157. *
  158. * @return Returns 0 on success.
  159. */
  160. static int epoll_wqueue_callback(struct rt_wqueue_node *wait, void *key)
  161. {
  162. struct rt_fd_list *fdlist;
  163. struct rt_eventpoll *ep;
  164. rt_base_t level;
  165. if (key && !((rt_ubase_t)key & wait->key))
  166. return -1;
  167. fdlist = rt_container_of(wait, struct rt_fd_list, wqn);
  168. ep = fdlist->ep;
  169. if (ep)
  170. {
  171. level = rt_spin_lock_irqsave(&ep->spinlock);
  172. if (fdlist->is_rdl_node == RT_FALSE)
  173. {
  174. rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
  175. fdlist->exclusive = 0;
  176. fdlist->is_rdl_node = RT_TRUE;
  177. ep->triggered = 1;
  178. ep->eventpoll_num++;
  179. rt_wqueue_wakeup(&ep->epoll_read, (void *)POLLIN);
  180. }
  181. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  182. }
  183. return __wqueue_default_wake(wait, key);
  184. }
  185. /**
  186. * @brief Adds a callback function to the wait queue associated with epoll.
  187. *
  188. * This function adds a callback function to the wait queue associated with epoll.
  189. *
  190. * @param wq Pointer to the wait queue.
  191. * @param req Pointer to the poll request structure.
  192. */
  193. static void epoll_wqueue_add_callback(rt_wqueue_t *wq, rt_pollreq_t *req)
  194. {
  195. struct rt_fd_list *fdlist;
  196. struct rt_eventpoll *ep;
  197. fdlist = rt_container_of(req, struct rt_fd_list, req);
  198. ep = fdlist->ep;
  199. fdlist->wqn.key = req->_key;
  200. rt_list_init(&(fdlist->wqn.list));
  201. fdlist->wqn.polling_thread = ep->polling_thread;
  202. fdlist->wqn.wakeup = epoll_wqueue_callback;
  203. rt_wqueue_add(wq, &fdlist->wqn);
  204. }
  205. /**
  206. * @brief Installs a file descriptor list into the epoll control structure.
  207. *
  208. * This function installs a file descriptor list into the epoll control structure.
  209. *
  210. * @param fdlist Pointer to the file descriptor list.
  211. * @param ep Pointer to the epoll control structure.
  212. */
  213. static void epoll_ctl_install(struct rt_fd_list *fdlist, struct rt_eventpoll *ep)
  214. {
  215. rt_uint32_t mask = 0;
  216. rt_base_t level;
  217. fdlist->req._key = fdlist->revents;
  218. mask = epoll_get_event(fdlist, &fdlist->req);
  219. if (mask & fdlist->revents)
  220. {
  221. if (ep)
  222. {
  223. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  224. level = rt_spin_lock_irqsave(&ep->spinlock);
  225. rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
  226. fdlist->exclusive = 0;
  227. fdlist->is_rdl_node = RT_TRUE;
  228. ep->triggered = 1;
  229. ep->eventpoll_num++;
  230. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  231. rt_mutex_release(&ep->lock);
  232. }
  233. }
  234. }
  235. /**
  236. * @brief Initializes the epoll control structure.
  237. *
  238. * This function initializes the epoll control structure.
  239. *
  240. * @param ep Pointer to the epoll control structure.
  241. */
  242. static void epoll_member_init(struct rt_eventpoll *ep)
  243. {
  244. ep->triggered = 0;
  245. ep->eventpoll_num = 0;
  246. ep->polling_thread = rt_thread_self();
  247. ep->fdlist = RT_NULL;
  248. ep->req._key = 0;
  249. rt_slist_init(&(ep->rdl_head));
  250. rt_wqueue_init(&ep->epoll_read);
  251. rt_mutex_init(&ep->lock, EPOLL_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  252. rt_spin_lock_init(&ep->spinlock);
  253. }
  254. /**
  255. * @brief Initializes the epoll file descriptor.
  256. *
  257. * This function initializes the epoll file descriptor.
  258. *
  259. * @param fd File descriptor.
  260. *
  261. * @return Returns 0 on success.
  262. */
  263. static int epoll_epf_init(int fd)
  264. {
  265. struct dfs_file *df;
  266. struct rt_eventpoll *ep;
  267. rt_err_t ret = 0;
  268. df = fd_get(fd);
  269. if (df)
  270. {
  271. ep = (struct rt_eventpoll *)rt_malloc(sizeof(struct rt_eventpoll));
  272. if (ep)
  273. {
  274. epoll_member_init(ep);
  275. #ifdef RT_USING_DFS_V2
  276. df->fops = &epoll_fops;
  277. #endif
  278. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  279. if (df->vnode)
  280. {
  281. ep->fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
  282. if (ep->fdlist)
  283. {
  284. ep->fdlist->next = RT_NULL;
  285. ep->fdlist->fd = fd;
  286. ep->fdlist->ep = ep;
  287. ep->fdlist->exclusive = 0;
  288. ep->fdlist->is_rdl_node = RT_FALSE;
  289. dfs_vnode_init(df->vnode, FT_REGULAR, &epoll_fops);
  290. df->vnode->data = ep;
  291. rt_slist_init(&ep->fdlist->rdl_node);
  292. }
  293. else
  294. {
  295. ret = -ENOMEM;
  296. rt_free(df->vnode);
  297. rt_free(ep);
  298. }
  299. }
  300. else
  301. {
  302. ret = -ENOMEM;
  303. rt_free(ep);
  304. }
  305. }
  306. else
  307. {
  308. ret = -ENOMEM;
  309. }
  310. }
  311. return ret;
  312. }
  313. /**
  314. * @brief Creates an epoll file descriptor.
  315. *
  316. * This function creates an epoll file descriptor.
  317. *
  318. * @param size Size of the epoll instance.
  319. *
  320. * @return Returns the file descriptor on success, or -1 on failure.
  321. */
  322. static int epoll_do_create(int size)
  323. {
  324. rt_err_t ret = -1;
  325. int status;
  326. int fd;
  327. if (size < 0)
  328. {
  329. rt_set_errno(EINVAL);
  330. }
  331. else
  332. {
  333. fd = fd_new();
  334. if (fd >= 0)
  335. {
  336. ret = fd;
  337. status = epoll_epf_init(fd);
  338. if (status < 0)
  339. {
  340. fd_release(fd);
  341. rt_set_errno(-status);
  342. }
  343. }
  344. else
  345. {
  346. rt_set_errno(-fd);
  347. }
  348. }
  349. return ret;
  350. }
  351. /**
  352. * @brief Adds a file descriptor to the epoll instance.
  353. *
  354. * This function adds a file descriptor to the epoll instance.
  355. *
  356. * @param df Pointer to the file structure.
  357. * @param fd File descriptor to add.
  358. * @param event Pointer to the epoll event structure.
  359. *
  360. * @return Returns 0 on success, or an error code on failure.
  361. */
  362. static int epoll_ctl_add(struct dfs_file *df, int fd, struct epoll_event *event)
  363. {
  364. struct rt_fd_list *fdlist;
  365. struct rt_eventpoll *ep;
  366. rt_err_t ret = -EINVAL;
  367. if (df->vnode->data)
  368. {
  369. ep = df->vnode->data;
  370. fdlist = ep->fdlist;
  371. ret = 0;
  372. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  373. while (fdlist->next != RT_NULL)
  374. {
  375. if (fdlist->next->fd == fd)
  376. {
  377. rt_mutex_release(&ep->lock);
  378. return 0;
  379. }
  380. fdlist = fdlist->next;
  381. }
  382. rt_mutex_release(&ep->lock);
  383. fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
  384. if (fdlist)
  385. {
  386. fdlist->fd = fd;
  387. memcpy(&fdlist->epev.data, &event->data, sizeof(event->data));
  388. fdlist->epev.events = 0;
  389. fdlist->ep = ep;
  390. fdlist->exclusive = 0;
  391. fdlist->is_rdl_node = RT_FALSE;
  392. fdlist->req._proc = epoll_wqueue_add_callback;
  393. fdlist->revents = event->events;
  394. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  395. fdlist->next = ep->fdlist->next;
  396. ep->fdlist->next = fdlist;
  397. rt_mutex_release(&ep->lock);
  398. rt_slist_init(&fdlist->rdl_node);
  399. epoll_ctl_install(fdlist, ep);
  400. }
  401. else
  402. {
  403. ret = -ENOMEM;
  404. }
  405. }
  406. return ret;
  407. }
  408. /**
  409. * @brief Removes a file descriptor from the epoll instance.
  410. *
  411. * This function removes a file descriptor from the epoll instance.
  412. *
  413. * @param df Pointer to the file structure.
  414. * @param fd File descriptor to remove.
  415. *
  416. * @return Returns 0 on success, or an error code on failure.
  417. */
  418. static int epoll_ctl_del(struct dfs_file *df, int fd)
  419. {
  420. struct rt_fd_list *fdlist, *fre_fd, *rdlist;
  421. struct rt_eventpoll *ep = RT_NULL;
  422. rt_slist_t *node = RT_NULL;
  423. rt_err_t ret = -EINVAL;
  424. rt_base_t level;
  425. if (df->vnode->data)
  426. {
  427. ep = df->vnode->data;
  428. if (ep)
  429. {
  430. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  431. level = rt_spin_lock_irqsave(&ep->spinlock);
  432. rt_slist_for_each(node, &ep->rdl_head)
  433. {
  434. rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
  435. if (rdlist->fd == fd)
  436. rt_slist_remove(&ep->rdl_head, node);
  437. }
  438. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  439. fdlist = ep->fdlist;
  440. while (fdlist->next != RT_NULL)
  441. {
  442. if (fdlist->next->fd == fd)
  443. {
  444. fre_fd = fdlist->next;
  445. fdlist->next = fdlist->next->next;
  446. if (fre_fd->wqn.wqueue)
  447. rt_wqueue_remove(&fre_fd->wqn);
  448. rt_free(fre_fd);
  449. break;
  450. }
  451. else
  452. {
  453. fdlist = fdlist->next;
  454. }
  455. }
  456. rt_mutex_release(&ep->lock);
  457. }
  458. ret = 0;
  459. }
  460. return ret;
  461. }
  462. /**
  463. * @brief Modifies the events associated with a file descriptor in the epoll instance.
  464. *
  465. * This function modifies the events associated with a file descriptor in the epoll instance.
  466. *
  467. * @param df Pointer to the file structure.
  468. * @param fd File descriptor to modify.
  469. * @param event Pointer to the epoll event structure.
  470. *
  471. * @return Returns 0 on success, or an error code on failure.
  472. */
  473. static int epoll_ctl_mod(struct dfs_file *df, int fd, struct epoll_event *event)
  474. {
  475. struct rt_fd_list *fdlist;
  476. struct rt_eventpoll *ep = RT_NULL;
  477. rt_err_t ret = -EINVAL;
  478. if (df->vnode->data)
  479. {
  480. ep = df->vnode->data;
  481. fdlist = ep->fdlist;
  482. while (fdlist->next != RT_NULL)
  483. {
  484. if (fdlist->next->fd == fd)
  485. {
  486. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  487. memcpy(&fdlist->next->epev.data, &event->data, sizeof(event->data));
  488. fdlist->next->revents = event->events;
  489. if (fdlist->next->wqn.wqueue)
  490. rt_wqueue_remove(&fdlist->next->wqn);
  491. rt_mutex_release(&ep->lock);
  492. epoll_ctl_install(fdlist->next, ep);
  493. break;
  494. }
  495. fdlist = fdlist->next;
  496. }
  497. ret = 0;
  498. }
  499. return ret;
  500. }
  501. /**
  502. * @brief Controls an epoll instance.
  503. *
  504. * This function controls an epoll instance, performing operations such as adding,
  505. * modifying, or removing file descriptors associated with the epoll instance.
  506. *
  507. * @param epfd File descriptor of the epoll instance.
  508. * @param op Operation to perform (EPOLL_CTL_ADD, EPOLL_CTL_DEL, or EPOLL_CTL_MOD).
  509. * @param fd File descriptor to add, modify, or remove.
  510. * @param event Pointer to the epoll event structure.
  511. *
  512. * @return Returns 0 on success, or -1 on failure with errno set appropriately.
  513. */
  514. static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event)
  515. {
  516. struct dfs_file *epdf;
  517. struct rt_eventpoll *ep;
  518. rt_err_t ret = 0;
  519. if (op & ~EFD_SHARED_EPOLL_TYPE)
  520. {
  521. rt_set_errno(EINVAL);
  522. return -1;
  523. }
  524. if ((epfd == fd) || (epfd < 0))
  525. {
  526. rt_set_errno(EINVAL);
  527. return -1;
  528. }
  529. if (!(op & EPOLL_CTL_DEL))
  530. {
  531. if (!(event->events & EPOLLEXCLUSIVE_BITS))
  532. {
  533. rt_set_errno(EINVAL);
  534. return -1;
  535. }
  536. event->events |= EPOLLERR | EPOLLHUP;
  537. }
  538. if (!fd_get(fd))
  539. {
  540. rt_set_errno(EBADF);
  541. return -1;
  542. }
  543. epdf = fd_get(epfd);
  544. if (epdf->vnode->data)
  545. {
  546. ep = epdf->vnode->data;
  547. switch (op)
  548. {
  549. case EPOLL_CTL_ADD:
  550. ret = epoll_ctl_add(epdf, fd, event);
  551. break;
  552. case EPOLL_CTL_DEL:
  553. ret = epoll_ctl_del(epdf, fd);
  554. break;
  555. case EPOLL_CTL_MOD:
  556. ret = epoll_ctl_mod(epdf, fd, event);
  557. break;
  558. default:
  559. rt_set_errno(EINVAL);
  560. break;
  561. }
  562. if (ret < 0)
  563. {
  564. rt_set_errno(-ret);
  565. ret = -1;
  566. }
  567. else
  568. {
  569. ep->polling_thread = rt_thread_self();
  570. }
  571. }
  572. return ret;
  573. }
  574. /**
  575. * @brief Waits for events on an epoll instance with a specified timeout.
  576. *
  577. * This function waits for events on the specified epoll instance within the given timeout.
  578. *
  579. * @param ep Pointer to the epoll instance.
  580. * @param msec Timeout in milliseconds.
  581. *
  582. * @return Returns 0 if no events occurred within the timeout, or 1 if events were triggered.
  583. */
  584. static int epoll_wait_timeout(struct rt_eventpoll *ep, int msec)
  585. {
  586. rt_int32_t timeout;
  587. struct rt_thread *thread;
  588. rt_base_t level;
  589. int ret = 0;
  590. thread = ep->polling_thread;
  591. timeout = rt_tick_from_millisecond(msec);
  592. level = rt_spin_lock_irqsave(&ep->spinlock);
  593. if (timeout != 0 && !ep->triggered)
  594. {
  595. if (rt_thread_suspend_with_flag(thread, RT_KILLABLE) == RT_EOK)
  596. {
  597. if (timeout > 0)
  598. {
  599. rt_timer_control(&(thread->thread_timer),
  600. RT_TIMER_CTRL_SET_TIME,
  601. &timeout);
  602. rt_timer_start(&(thread->thread_timer));
  603. }
  604. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  605. rt_schedule();
  606. level = rt_spin_lock_irqsave(&ep->spinlock);
  607. }
  608. }
  609. ret = !ep->triggered;
  610. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  611. return ret;
  612. }
  613. /**
  614. * @brief Gets events associated with a file descriptor in the epoll instance.
  615. *
  616. * This function gets events associated with a file descriptor in the epoll instance.
  617. *
  618. * @param fl Pointer to the file descriptor list structure.
  619. * @param req Pointer to the poll request structure.
  620. *
  621. * @return Returns the bitmask of events associated with the file descriptor.
  622. */
  623. static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req)
  624. {
  625. struct dfs_file *df;
  626. int mask = 0;
  627. int fd = 0;
  628. fd = fl->fd;
  629. if (fd >= 0)
  630. {
  631. df = fd_get(fd);
  632. if (df)
  633. {
  634. if (df->vnode->fops->poll)
  635. {
  636. req->_key = fl->revents | POLLERR | POLLHUP;
  637. mask = df->vnode->fops->poll(df, req);
  638. if (mask < 0)
  639. return mask;
  640. }
  641. mask &= fl->revents | EPOLLOUT | POLLERR;
  642. }
  643. }
  644. return mask;
  645. }
  646. /**
  647. * @brief Performs epoll operation to get triggered events.
  648. *
  649. * This function performs epoll operation to get triggered events.
  650. *
  651. * @param ep Pointer to the epoll instance.
  652. * @param events Pointer to the array to store triggered events.
  653. * @param maxevents Maximum number of events to store in the array.
  654. * @param timeout Timeout value in milliseconds.
  655. *
  656. * @return Returns the number of triggered events.
  657. */
  658. static int epoll_do(struct rt_eventpoll *ep, struct epoll_event *events, int maxevents, int timeout)
  659. {
  660. struct rt_fd_list *rdlist;
  661. rt_slist_t *node = RT_NULL;
  662. int event_num = 0;
  663. int istimeout = 0;
  664. int isn_add = 0;
  665. int isfree = 0;
  666. int mask = 0;
  667. rt_base_t level;
  668. while (1)
  669. {
  670. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  671. level = rt_spin_lock_irqsave(&ep->spinlock);
  672. if (ep->eventpoll_num > 0)
  673. {
  674. rt_slist_for_each(node,&ep->rdl_head)
  675. {
  676. rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
  677. ep->eventpoll_num --;
  678. rt_slist_remove(&ep->rdl_head, &rdlist->rdl_node);
  679. rdlist->is_rdl_node = RT_FALSE;
  680. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  681. isfree = 0;
  682. isn_add = 0;
  683. if (event_num < maxevents)
  684. {
  685. if (rdlist->wqn.wqueue)
  686. {
  687. rt_wqueue_remove(&rdlist->wqn);
  688. }
  689. mask = epoll_get_event(rdlist, &rdlist->req);
  690. if (mask & rdlist->revents)
  691. {
  692. rdlist->epev.events = mask & rdlist->revents;
  693. }
  694. else
  695. {
  696. isfree = 1;
  697. isn_add = 1;
  698. }
  699. if (rdlist->revents & EPOLLONESHOT)
  700. {
  701. rdlist->revents = 0;
  702. isfree = 1;
  703. if (rdlist->wqn.wqueue)
  704. rt_wqueue_remove(&rdlist->wqn);
  705. }
  706. else
  707. {
  708. if (rdlist->revents & EPOLLET)
  709. {
  710. isfree = 1;
  711. }
  712. else
  713. {
  714. level = rt_spin_lock_irqsave(&ep->spinlock);
  715. if (rdlist->exclusive != 1)
  716. {
  717. rdlist->exclusive = 1;
  718. }
  719. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  720. }
  721. }
  722. if (!isn_add)
  723. {
  724. memcpy(&events[event_num], &rdlist->epev, sizeof(rdlist->epev));
  725. event_num ++;
  726. }
  727. if (!isfree)
  728. {
  729. if (rdlist->is_rdl_node == RT_FALSE)
  730. {
  731. level = rt_spin_lock_irqsave(&ep->spinlock);
  732. ep->eventpoll_num ++;
  733. rt_slist_append(&ep->rdl_head, &rdlist->rdl_node);
  734. rdlist->is_rdl_node = RT_TRUE;
  735. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  736. }
  737. else
  738. {
  739. level = rt_spin_lock_irqsave(&ep->spinlock);
  740. if (!rdlist->wqn.wqueue)
  741. {
  742. epoll_get_event(rdlist, &rdlist->req);
  743. }
  744. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  745. }
  746. }
  747. }
  748. else
  749. {
  750. level = rt_spin_lock_irqsave(&ep->spinlock);
  751. break;
  752. }
  753. level = rt_spin_lock_irqsave(&ep->spinlock);
  754. }
  755. }
  756. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  757. rt_mutex_release(&ep->lock);
  758. if (event_num || istimeout)
  759. {
  760. level = rt_spin_lock_irqsave(&ep->spinlock);
  761. ep->triggered = 0;
  762. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  763. if ((timeout >= 0) || (event_num > 0))
  764. break;
  765. }
  766. if (epoll_wait_timeout(ep, timeout))
  767. {
  768. istimeout = 1;
  769. }
  770. }
  771. return event_num;
  772. }
  773. /**
  774. * @brief Waits for events on an epoll instance with specified parameters.
  775. *
  776. * This function waits for events on the specified epoll instance within the given timeout, optionally blocking signals based on the provided signal set.
  777. *
  778. * @param epfd File descriptor referring to the epoll instance.
  779. * @param events Pointer to the array to store triggered events.
  780. * @param maxevents Maximum number of events to store in the array.
  781. * @param timeout Timeout value in milliseconds.
  782. * @param ss Pointer to the signal set indicating signals to block during the wait operation. Pass NULL if no signals need to be blocked.
  783. *
  784. * @return Returns the number of triggered events on success, or -1 on failure.
  785. */
  786. static int epoll_do_wait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  787. {
  788. struct rt_eventpoll *ep;
  789. struct dfs_file *df;
  790. lwp_sigset_t old_sig, new_sig;
  791. rt_err_t ret = 0;
  792. if (ss)
  793. {
  794. memcpy(&new_sig, ss, sizeof(lwp_sigset_t));
  795. lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_BLOCK, &new_sig, &old_sig);
  796. }
  797. if ((maxevents > 0) && (epfd >= 0))
  798. {
  799. df = fd_get(epfd);
  800. if (df && df->vnode)
  801. {
  802. ep = (struct rt_eventpoll *)df->vnode->data;
  803. if (ep)
  804. {
  805. ret = epoll_do(ep, events, maxevents, timeout);
  806. }
  807. }
  808. }
  809. if (ss)
  810. {
  811. lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_SET_MASK, &old_sig, RT_NULL);
  812. }
  813. if (ret < 0)
  814. {
  815. rt_set_errno(-ret);
  816. ret = -1;
  817. }
  818. return ret;
  819. }
  820. /**
  821. * @brief Creates an epoll instance.
  822. *
  823. * This function creates an epoll instance with the specified size.
  824. *
  825. * @param size Size of the epoll instance.
  826. *
  827. * @return Returns the file descriptor referring to the created epoll instance on success, or -1 on failure.
  828. */
  829. int epoll_create(int size)
  830. {
  831. return epoll_do_create(size);
  832. }
  833. /**
  834. * @brief Modifies an epoll instance.
  835. *
  836. * This function modifies the epoll instance referred to by 'epfd' according to the specified operation 'op', associated with the file descriptor 'fd', and the event structure 'event'.
  837. *
  838. * @param epfd File descriptor referring to the epoll instance.
  839. * @param op Operation to perform (EPOLL_CTL_ADD, EPOLL_CTL_DEL, or EPOLL_CTL_MOD).
  840. * @param fd File descriptor to associate with the epoll instance or remove from it.
  841. * @param event Pointer to the event structure containing the events to modify.
  842. *
  843. * @return Returns 0 on success, or -1 on failure.
  844. */
  845. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
  846. {
  847. return epoll_do_ctl(epfd, op, fd, event);
  848. }
  849. /**
  850. * @brief Waits for events on an epoll instance.
  851. *
  852. * This function waits for events on the epoll instance referred to by 'epfd' within the given timeout.
  853. *
  854. * @param epfd File descriptor referring to the epoll instance.
  855. * @param events Pointer to the array to store triggered events.
  856. * @param maxevents Maximum number of events to store in the array.
  857. * @param timeout Timeout value in milliseconds.
  858. *
  859. * @return Returns the number of triggered events on success, or -1 on failure.
  860. */
  861. int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
  862. {
  863. return epoll_do_wait(epfd, events, maxevents, timeout, RT_NULL);
  864. }
  865. /**
  866. * @brief Waits for events on an epoll instance, blocking signals.
  867. *
  868. * This function waits for events on the epoll instance referred to by 'epfd' within the given timeout, blocking signals based on the provided signal set 'ss'.
  869. *
  870. * @param epfd File descriptor referring to the epoll instance.
  871. * @param events Pointer to the array to store triggered events.
  872. * @param maxevents Maximum number of events to store in the array.
  873. * @param timeout Timeout value in milliseconds.
  874. * @param ss Pointer to the signal set indicating signals to block during the wait operation.
  875. *
  876. * @return Returns the number of triggered events on success, or -1 on failure.
  877. */
  878. int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  879. {
  880. return epoll_do_wait(epfd, events, maxevents, timeout, ss);
  881. }
  882. /**
  883. * @brief Waits for events on an epoll instance, blocking signals.
  884. *
  885. * This function waits for events on the epoll instance referred to by 'epfd' within the given timeout, blocking signals based on the provided signal set 'ss'.
  886. *
  887. * @param epfd File descriptor referring to the epoll instance.
  888. * @param events Pointer to the array to store triggered events.
  889. * @param maxevents Maximum number of events to store in the array.
  890. * @param timeout Timeout value in milliseconds.
  891. * @param ss Pointer to the signal set indicating signals to block during the wait operation.
  892. *
  893. * @return Returns the number of triggered events on success, or -1 on failure.
  894. */
  895. int epoll_pwait2(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  896. {
  897. return epoll_do_wait(epfd, events, maxevents, timeout, ss);
  898. }