epoll.c 29 KB

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