epoll.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. */
  10. #include <rtthread.h>
  11. #include <fcntl.h>
  12. #include <stdint.h>
  13. #include <unistd.h>
  14. #include <dfs_file.h>
  15. #include "sys/epoll.h"
  16. #include "poll.h"
  17. #include <lwp_signal.h>
  18. #define EPOLL_MUTEX_NAME "EVENTEPOLL"
  19. #define EFD_SHARED_EPOLL_TYPE (EPOLL_CTL_ADD | EPOLL_CTL_DEL | EPOLL_CTL_MOD)
  20. #define EPOLLINOUT_BITS (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
  21. #define EPOLLEXCLUSIVE_BITS (EPOLLINOUT_BITS | EPOLLERR | EPOLLHUP | \
  22. EPOLLET | EPOLLEXCLUSIVE)
  23. struct rt_eventpoll;
  24. /* Monitor queue */
  25. struct rt_fd_list
  26. {
  27. rt_uint32_t revents; /* Monitored events */
  28. struct epoll_event epev;
  29. rt_pollreq_t req;
  30. struct rt_eventpoll *ep;
  31. struct rt_wqueue_node wqn;
  32. int exclusive;/* If triggered horizontally, a check is made to see if the data has been read, and if there is any data left to read, the readability event is returned in the next epoll_wait */
  33. rt_bool_t is_rdl_node;
  34. int fd;
  35. struct rt_fd_list *next;
  36. rt_slist_t rdl_node;
  37. };
  38. struct rt_eventpoll
  39. {
  40. rt_uint32_t tirggered; /* the waited thread whether triggered */
  41. rt_wqueue_t epoll_read;
  42. rt_thread_t polling_thread;
  43. struct rt_mutex lock;
  44. struct rt_fd_list *fdlist; /* Monitor list */
  45. int eventpoll_num; /* Number of ready lists */
  46. rt_pollreq_t req;
  47. struct rt_spinlock spinlock;
  48. rt_slist_t rdl_head;
  49. };
  50. static int epoll_close(struct dfs_file *file);
  51. static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req);
  52. static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req);
  53. static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event);
  54. static const struct dfs_file_ops epoll_fops =
  55. {
  56. .close = epoll_close,
  57. .poll = epoll_poll,
  58. };
  59. static int epoll_close_fdlist(struct rt_fd_list *fdlist)
  60. {
  61. struct rt_fd_list *fre_node, *list;
  62. if (fdlist != RT_NULL)
  63. {
  64. list = fdlist;
  65. while (list->next != RT_NULL)
  66. {
  67. fre_node = list->next;
  68. if (fre_node->wqn.wqueue)
  69. rt_wqueue_remove(&fre_node->wqn);
  70. list->next = fre_node->next;
  71. rt_free(fre_node);
  72. }
  73. rt_free(fdlist);
  74. }
  75. return 0;
  76. }
  77. static int epoll_close(struct dfs_file *file)
  78. {
  79. struct rt_eventpoll *ep;
  80. if (file->vnode->ref_count != 1)
  81. return 0;
  82. if (file->vnode)
  83. {
  84. if (file->vnode->data)
  85. {
  86. ep = file->vnode->data;
  87. if (ep)
  88. {
  89. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  90. if (ep->fdlist)
  91. {
  92. epoll_close_fdlist(ep->fdlist);
  93. }
  94. rt_mutex_release(&ep->lock);
  95. rt_mutex_detach(&ep->lock);
  96. rt_free(ep);
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req)
  103. {
  104. struct rt_eventpoll *ep;
  105. int events = 0;
  106. rt_base_t level;
  107. if (file->vnode->data)
  108. {
  109. ep = file->vnode->data;
  110. ep->req._key = req->_key;
  111. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  112. rt_poll_add(&ep->epoll_read, req);
  113. level = rt_spin_lock_irqsave(&ep->spinlock);
  114. if (!rt_slist_isempty(&ep->rdl_head))
  115. events |= POLLIN | EPOLLRDNORM | POLLOUT;
  116. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  117. rt_mutex_release(&ep->lock);
  118. }
  119. return events;
  120. }
  121. static int epoll_wqueue_callback(struct rt_wqueue_node *wait, void *key)
  122. {
  123. struct rt_fd_list *fdlist;
  124. struct rt_eventpoll *ep;
  125. rt_base_t level;
  126. if (key && !((rt_ubase_t)key & wait->key))
  127. return -1;
  128. fdlist = rt_container_of(wait, struct rt_fd_list, wqn);
  129. ep = fdlist->ep;
  130. if (ep)
  131. {
  132. level = rt_spin_lock_irqsave(&ep->spinlock);
  133. if (fdlist->is_rdl_node == RT_FALSE)
  134. {
  135. rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
  136. fdlist->exclusive = 0;
  137. fdlist->is_rdl_node = RT_TRUE;
  138. ep->tirggered = 1;
  139. ep->eventpoll_num ++;
  140. rt_wqueue_wakeup(&ep->epoll_read, (void *)POLLIN);
  141. }
  142. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  143. }
  144. return __wqueue_default_wake(wait, key);
  145. }
  146. static void epoll_wqueue_add_callback(rt_wqueue_t *wq, rt_pollreq_t *req)
  147. {
  148. struct rt_fd_list *fdlist;
  149. struct rt_eventpoll *ep;
  150. fdlist = rt_container_of(req, struct rt_fd_list, req);
  151. ep = fdlist->ep;
  152. fdlist->wqn.key = req->_key;
  153. rt_list_init(&(fdlist->wqn.list));
  154. fdlist->wqn.polling_thread = ep->polling_thread;
  155. fdlist->wqn.wakeup = epoll_wqueue_callback;
  156. rt_wqueue_add(wq, &fdlist->wqn);
  157. }
  158. static void epoll_ctl_install(struct rt_fd_list *fdlist, struct rt_eventpoll *ep)
  159. {
  160. rt_uint32_t mask = 0;
  161. rt_base_t level;
  162. fdlist->req._key = fdlist->revents;
  163. mask = epoll_get_event(fdlist, &fdlist->req);
  164. if (mask & fdlist->revents)
  165. {
  166. if (ep)
  167. {
  168. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  169. level = rt_spin_lock_irqsave(&ep->spinlock);
  170. rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
  171. fdlist->exclusive = 0;
  172. fdlist->is_rdl_node = RT_TRUE;
  173. ep->tirggered = 1;
  174. ep->eventpoll_num ++;
  175. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  176. rt_mutex_release(&ep->lock);
  177. }
  178. }
  179. }
  180. static void epoll_member_init(struct rt_eventpoll *ep)
  181. {
  182. ep->tirggered = 0;
  183. ep->eventpoll_num = 0;
  184. ep->polling_thread = rt_thread_self();
  185. ep->fdlist = RT_NULL;
  186. ep->req._key = 0;
  187. rt_slist_init(&(ep->rdl_head));
  188. rt_wqueue_init(&ep->epoll_read);
  189. rt_mutex_init(&ep->lock, EPOLL_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  190. rt_spin_lock_init(&ep->spinlock);
  191. }
  192. static int epoll_epf_init(int fd)
  193. {
  194. struct dfs_file *df;
  195. struct rt_eventpoll *ep;
  196. rt_err_t ret = 0;
  197. df = fd_get(fd);
  198. if (df)
  199. {
  200. ep = (struct rt_eventpoll *)rt_malloc(sizeof(struct rt_eventpoll));
  201. if (ep)
  202. {
  203. epoll_member_init(ep);
  204. #ifdef RT_USING_DFS_V2
  205. df->fops = &epoll_fops;
  206. #endif
  207. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  208. if (df->vnode)
  209. {
  210. ep->fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
  211. if (ep->fdlist)
  212. {
  213. ep->fdlist->next = RT_NULL;
  214. ep->fdlist->fd = fd;
  215. ep->fdlist->ep = ep;
  216. ep->fdlist->exclusive = 0;
  217. ep->fdlist->is_rdl_node = RT_FALSE;
  218. dfs_vnode_init(df->vnode, FT_REGULAR, &epoll_fops);
  219. df->vnode->data = ep;
  220. rt_slist_init(&ep->fdlist->rdl_node);
  221. }
  222. else
  223. {
  224. ret = -ENOMEM;
  225. rt_free(df->vnode);
  226. rt_free(ep);
  227. }
  228. }
  229. else
  230. {
  231. ret = -ENOMEM;
  232. rt_free(ep);
  233. }
  234. }
  235. else
  236. {
  237. ret = -ENOMEM;
  238. }
  239. }
  240. return ret;
  241. }
  242. static int epoll_do_create(int size)
  243. {
  244. rt_err_t ret = -1;
  245. int status;
  246. int fd;
  247. if (size < 0)
  248. {
  249. rt_set_errno(EINVAL);
  250. }
  251. else
  252. {
  253. fd = fd_new();
  254. if (fd >= 0)
  255. {
  256. ret = fd;
  257. status = epoll_epf_init(fd);
  258. if (status < 0)
  259. {
  260. fd_release(fd);
  261. rt_set_errno(-status);
  262. }
  263. }
  264. else
  265. {
  266. rt_set_errno(-fd);
  267. }
  268. }
  269. return ret;
  270. }
  271. static int epoll_ctl_add(struct dfs_file *df, int fd, struct epoll_event *event)
  272. {
  273. struct rt_fd_list *fdlist;
  274. struct rt_eventpoll *ep;
  275. rt_err_t ret = -EINVAL;
  276. if (df->vnode->data)
  277. {
  278. ep = df->vnode->data;
  279. fdlist = ep->fdlist;
  280. ret = 0;
  281. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  282. while (fdlist->next != RT_NULL)
  283. {
  284. if (fdlist->next->fd == fd)
  285. {
  286. rt_mutex_release(&ep->lock);
  287. return 0;
  288. }
  289. fdlist = fdlist->next;
  290. }
  291. rt_mutex_release(&ep->lock);
  292. fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
  293. if (fdlist)
  294. {
  295. fdlist->fd = fd;
  296. memcpy(&fdlist->epev.data, &event->data, sizeof(event->data));
  297. fdlist->epev.events = 0;
  298. fdlist->ep = ep;
  299. fdlist->exclusive = 0;
  300. fdlist->is_rdl_node = RT_FALSE;
  301. fdlist->req._proc = epoll_wqueue_add_callback;
  302. fdlist->revents = event->events;
  303. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  304. fdlist->next = ep->fdlist->next;
  305. ep->fdlist->next = fdlist;
  306. rt_mutex_release(&ep->lock);
  307. rt_slist_init(&fdlist->rdl_node);
  308. epoll_ctl_install(fdlist, ep);
  309. }
  310. else
  311. {
  312. ret = -ENOMEM;
  313. }
  314. }
  315. return ret;
  316. }
  317. static int epoll_ctl_del(struct dfs_file *df, int fd)
  318. {
  319. struct rt_fd_list *fdlist, *fre_fd, *rdlist;
  320. struct rt_eventpoll *ep = RT_NULL;
  321. rt_slist_t *node = RT_NULL;
  322. rt_err_t ret = -EINVAL;
  323. rt_base_t level;
  324. if (df->vnode->data)
  325. {
  326. ep = df->vnode->data;
  327. if (ep)
  328. {
  329. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  330. level = rt_spin_lock_irqsave(&ep->spinlock);
  331. rt_slist_for_each(node, &ep->rdl_head)
  332. {
  333. rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
  334. if (rdlist->fd == fd)
  335. rt_slist_remove(&ep->rdl_head, node);
  336. }
  337. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  338. fdlist = ep->fdlist;
  339. while (fdlist->next != RT_NULL)
  340. {
  341. if (fdlist->next->fd == fd)
  342. {
  343. fre_fd = fdlist->next;
  344. fdlist->next = fdlist->next->next;
  345. if (fre_fd->wqn.wqueue)
  346. rt_wqueue_remove(&fre_fd->wqn);
  347. rt_free(fre_fd);
  348. break;
  349. }
  350. else
  351. {
  352. fdlist = fdlist->next;
  353. }
  354. }
  355. rt_mutex_release(&ep->lock);
  356. }
  357. ret = 0;
  358. }
  359. return ret;
  360. }
  361. static int epoll_ctl_mod(struct dfs_file *df, int fd, struct epoll_event *event)
  362. {
  363. struct rt_fd_list *fdlist;
  364. struct rt_eventpoll *ep = RT_NULL;
  365. rt_err_t ret = -EINVAL;
  366. if (df->vnode->data)
  367. {
  368. ep = df->vnode->data;
  369. fdlist = ep->fdlist;
  370. while (fdlist->next != RT_NULL)
  371. {
  372. if (fdlist->next->fd == fd)
  373. {
  374. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  375. memcpy(&fdlist->next->epev.data, &event->data, sizeof(event->data));
  376. fdlist->next->revents = event->events;
  377. if (fdlist->next->wqn.wqueue)
  378. rt_wqueue_remove(&fdlist->next->wqn);
  379. rt_mutex_release(&ep->lock);
  380. epoll_ctl_install(fdlist->next, ep);
  381. break;
  382. }
  383. fdlist = fdlist->next;
  384. }
  385. ret = 0;
  386. }
  387. return ret;
  388. }
  389. static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event)
  390. {
  391. struct dfs_file *epdf;
  392. struct rt_eventpoll *ep;
  393. rt_err_t ret = 0;
  394. if (op & ~EFD_SHARED_EPOLL_TYPE)
  395. {
  396. rt_set_errno(EINVAL);
  397. return -1;
  398. }
  399. if ((epfd == fd) || (epfd < 0))
  400. {
  401. rt_set_errno(EINVAL);
  402. return -1;
  403. }
  404. if (!(op & EPOLL_CTL_DEL))
  405. {
  406. if (!(event->events & EPOLLEXCLUSIVE_BITS))
  407. {
  408. rt_set_errno(EINVAL);
  409. return -1;
  410. }
  411. event->events |= EPOLLERR | EPOLLHUP;
  412. }
  413. if (!fd_get(fd))
  414. {
  415. rt_set_errno(EBADF);
  416. return -1;
  417. }
  418. epdf = fd_get(epfd);
  419. if (epdf->vnode->data)
  420. {
  421. ep = epdf->vnode->data;
  422. switch (op)
  423. {
  424. case EPOLL_CTL_ADD:
  425. ret = epoll_ctl_add(epdf, fd, event);
  426. break;
  427. case EPOLL_CTL_DEL:
  428. ret = epoll_ctl_del(epdf, fd);
  429. break;
  430. case EPOLL_CTL_MOD:
  431. ret = epoll_ctl_mod(epdf, fd, event);
  432. break;
  433. default:
  434. rt_set_errno(EINVAL);
  435. break;
  436. }
  437. if (ret < 0)
  438. {
  439. rt_set_errno(-ret);
  440. ret = -1;
  441. }
  442. else
  443. {
  444. ep->polling_thread = rt_thread_self();
  445. }
  446. }
  447. return ret;
  448. }
  449. static int epoll_wait_timeout(struct rt_eventpoll *ep, int msec)
  450. {
  451. rt_int32_t timeout;
  452. struct rt_thread *thread;
  453. rt_base_t level;
  454. int ret = 0;
  455. thread = ep->polling_thread;
  456. timeout = rt_tick_from_millisecond(msec);
  457. level = rt_spin_lock_irqsave(&ep->spinlock);
  458. if (timeout != 0 && !ep->tirggered)
  459. {
  460. if (rt_thread_suspend_with_flag(thread, RT_KILLABLE) == RT_EOK)
  461. {
  462. if (timeout > 0)
  463. {
  464. rt_timer_control(&(thread->thread_timer),
  465. RT_TIMER_CTRL_SET_TIME,
  466. &timeout);
  467. rt_timer_start(&(thread->thread_timer));
  468. }
  469. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  470. rt_schedule();
  471. level = rt_spin_lock_irqsave(&ep->spinlock);
  472. }
  473. }
  474. ret = !ep->tirggered;
  475. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  476. return ret;
  477. }
  478. static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req)
  479. {
  480. struct dfs_file *df;
  481. int mask = 0;
  482. int fd = 0;
  483. fd = fl->fd;
  484. if (fd >= 0)
  485. {
  486. df = fd_get(fd);
  487. if (df)
  488. {
  489. if (df->vnode->fops->poll)
  490. {
  491. req->_key = fl->revents | POLLERR | POLLHUP;
  492. mask = df->vnode->fops->poll(df, req);
  493. if (mask < 0)
  494. return mask;
  495. }
  496. mask &= fl->revents | EPOLLOUT | POLLERR;
  497. }
  498. }
  499. return mask;
  500. }
  501. static int epoll_do(struct rt_eventpoll *ep, struct epoll_event *events, int maxevents, int timeout)
  502. {
  503. struct rt_fd_list *rdlist;
  504. rt_slist_t *node = RT_NULL;
  505. int event_num = 0;
  506. int istimeout = 0;
  507. int isn_add = 0;
  508. int isfree = 0;
  509. int mask = 0;
  510. rt_base_t level;
  511. while (1)
  512. {
  513. rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
  514. level = rt_spin_lock_irqsave(&ep->spinlock);
  515. if (ep->eventpoll_num > 0)
  516. {
  517. rt_slist_for_each(node,&ep->rdl_head)
  518. {
  519. rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
  520. ep->eventpoll_num --;
  521. rt_slist_remove(&ep->rdl_head, &rdlist->rdl_node);
  522. rdlist->is_rdl_node = RT_FALSE;
  523. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  524. isfree = 0;
  525. isn_add = 0;
  526. if (event_num < maxevents)
  527. {
  528. if (rdlist->wqn.wqueue)
  529. {
  530. rt_wqueue_remove(&rdlist->wqn);
  531. }
  532. mask = epoll_get_event(rdlist, &rdlist->req);
  533. if (mask & rdlist->revents)
  534. {
  535. rdlist->epev.events = mask & rdlist->revents;
  536. }
  537. else
  538. {
  539. isfree = 1;
  540. isn_add = 1;
  541. }
  542. if (rdlist->revents & EPOLLONESHOT)
  543. {
  544. rdlist->revents = 0;
  545. isfree = 1;
  546. if (rdlist->wqn.wqueue)
  547. rt_wqueue_remove(&rdlist->wqn);
  548. }
  549. else
  550. {
  551. if (rdlist->revents & EPOLLET)
  552. {
  553. isfree = 1;
  554. }
  555. else
  556. {
  557. level = rt_spin_lock_irqsave(&ep->spinlock);
  558. if (rdlist->exclusive != 1)
  559. {
  560. rdlist->exclusive = 1;
  561. }
  562. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  563. }
  564. }
  565. if (!isn_add)
  566. {
  567. memcpy(&events[event_num], &rdlist->epev, sizeof(rdlist->epev));
  568. event_num ++;
  569. }
  570. if (!isfree)
  571. {
  572. if (rdlist->is_rdl_node == RT_FALSE)
  573. {
  574. level = rt_spin_lock_irqsave(&ep->spinlock);
  575. ep->eventpoll_num ++;
  576. rt_slist_append(&ep->rdl_head, &rdlist->rdl_node);
  577. rdlist->is_rdl_node = RT_TRUE;
  578. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  579. }
  580. else
  581. {
  582. level = rt_spin_lock_irqsave(&ep->spinlock);
  583. if (!rdlist->wqn.wqueue)
  584. {
  585. epoll_get_event(rdlist, &rdlist->req);
  586. }
  587. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  588. }
  589. }
  590. }
  591. else
  592. {
  593. level = rt_spin_lock_irqsave(&ep->spinlock);
  594. break;
  595. }
  596. level = rt_spin_lock_irqsave(&ep->spinlock);
  597. }
  598. }
  599. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  600. rt_mutex_release(&ep->lock);
  601. if (event_num || istimeout)
  602. {
  603. level = rt_spin_lock_irqsave(&ep->spinlock);
  604. ep->tirggered = 0;
  605. rt_spin_unlock_irqrestore(&ep->spinlock, level);
  606. if ((timeout >= 0) || (event_num > 0))
  607. break;
  608. }
  609. if (epoll_wait_timeout(ep, timeout))
  610. {
  611. istimeout = 1;
  612. }
  613. }
  614. return event_num;
  615. }
  616. static int epoll_do_wait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  617. {
  618. struct rt_eventpoll *ep;
  619. struct dfs_file *df;
  620. lwp_sigset_t old_sig, new_sig;
  621. rt_err_t ret = 0;
  622. if (ss)
  623. {
  624. memcpy(&new_sig, ss, sizeof(lwp_sigset_t));
  625. lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_BLOCK, &new_sig, &old_sig);
  626. }
  627. if ((maxevents > 0) && (epfd >=0))
  628. {
  629. df = fd_get(epfd);
  630. if (df && df->vnode)
  631. {
  632. ep = (struct rt_eventpoll *)df->vnode->data;
  633. if (ep)
  634. {
  635. ret = epoll_do(ep, events, maxevents, timeout);
  636. }
  637. }
  638. }
  639. if (ss)
  640. {
  641. lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_SET_MASK, &old_sig, RT_NULL);
  642. }
  643. if (ret < 0)
  644. {
  645. rt_set_errno(-ret);
  646. ret = -1;
  647. }
  648. return ret;
  649. }
  650. int epoll_create(int size)
  651. {
  652. return epoll_do_create(size);
  653. }
  654. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
  655. {
  656. return epoll_do_ctl(epfd, op, fd, event);
  657. }
  658. int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
  659. {
  660. return epoll_do_wait(epfd, events, maxevents, timeout, RT_NULL);
  661. }
  662. int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  663. {
  664. return epoll_do_wait(epfd, events, maxevents, timeout, ss);
  665. }
  666. int epoll_pwait2(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
  667. {
  668. return epoll_do_wait(epfd, events, maxevents, timeout, ss);
  669. }