poll.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * File : poll.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016-12-28 Bernard first version
  23. * 2018-03-09 Bernard Add protection for pt->triggered.
  24. */
  25. #include <stdint.h>
  26. #include <rthw.h>
  27. #include <rtdevice.h>
  28. #include <rtthread.h>
  29. #include <dfs.h>
  30. #include <dfs_file.h>
  31. #include <dfs_posix.h>
  32. #include <dfs_poll.h>
  33. struct rt_poll_node;
  34. struct rt_poll_table
  35. {
  36. rt_pollreq_t req;
  37. rt_uint32_t triggered; /* the waited thread whether triggered */
  38. rt_thread_t polling_thread;
  39. struct rt_poll_node *nodes;
  40. };
  41. struct rt_poll_node
  42. {
  43. struct rt_wqueue_node wqn;
  44. struct rt_poll_table *pt;
  45. struct rt_poll_node *next;
  46. };
  47. static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
  48. {
  49. struct rt_poll_node *pn;
  50. if (key && !((rt_uint32_t)key & wait->key))
  51. return -1;
  52. pn = rt_container_of(wait, struct rt_poll_node, wqn);
  53. pn->pt->triggered = 1;
  54. return __wqueue_default_wake(wait, key);
  55. }
  56. static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
  57. {
  58. struct rt_poll_table *pt;
  59. struct rt_poll_node *node;
  60. node = rt_malloc(sizeof(struct rt_poll_node));
  61. if (node == RT_NULL)
  62. return;
  63. pt = rt_container_of(req, struct rt_poll_table, req);
  64. node->wqn.key = req->_key;
  65. rt_list_init(&(node->wqn.list));
  66. node->wqn.polling_thread = pt->polling_thread;
  67. node->wqn.wakeup = __wqueue_pollwake;
  68. node->next = pt->nodes;
  69. node->pt = pt;
  70. pt->nodes = node;
  71. rt_wqueue_add(wq, &node->wqn);
  72. }
  73. static void poll_table_init(struct rt_poll_table *pt)
  74. {
  75. pt->req._proc = _poll_add;
  76. pt->triggered = 0;
  77. pt->nodes = RT_NULL;
  78. pt->polling_thread = rt_thread_self();
  79. }
  80. static int poll_wait_timeout(struct rt_poll_table *pt, int msec)
  81. {
  82. rt_int32_t timeout;
  83. int ret = 0;
  84. struct rt_thread *thread;
  85. rt_base_t level;
  86. thread = pt->polling_thread;
  87. timeout = rt_tick_from_millisecond(msec);
  88. level = rt_hw_interrupt_disable();
  89. if (timeout != 0 && !pt->triggered)
  90. {
  91. rt_thread_suspend(thread);
  92. if (timeout > 0)
  93. {
  94. rt_timer_control(&(thread->thread_timer),
  95. RT_TIMER_CTRL_SET_TIME,
  96. &timeout);
  97. rt_timer_start(&(thread->thread_timer));
  98. }
  99. rt_hw_interrupt_enable(level);
  100. rt_schedule();
  101. level = rt_hw_interrupt_disable();
  102. }
  103. ret = !pt->triggered;
  104. rt_hw_interrupt_enable(level);
  105. return ret;
  106. }
  107. static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
  108. {
  109. int mask = 0;
  110. int fd;
  111. fd = pollfd->fd;
  112. if (fd >= 0)
  113. {
  114. struct dfs_fd *f = fd_get(fd);
  115. mask = POLLNVAL;
  116. if (f)
  117. {
  118. mask = POLLMASK_DEFAULT;
  119. if (f->fops->poll)
  120. {
  121. req->_key = pollfd->events | POLLERR| POLLHUP;
  122. mask = f->fops->poll(f, req);
  123. }
  124. /* Mask out unneeded events. */
  125. mask &= pollfd->events | POLLERR | POLLHUP;
  126. fd_put(f);
  127. }
  128. }
  129. pollfd->revents = mask;
  130. return mask;
  131. }
  132. static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
  133. {
  134. int num;
  135. int istimeout = 0;
  136. int n;
  137. struct pollfd *pf;
  138. if (msec == 0)
  139. {
  140. pt->req._proc = RT_NULL;
  141. istimeout = 1;
  142. }
  143. while (1)
  144. {
  145. pf = fds;
  146. num = 0;
  147. for (n = 0; n < nfds; n ++)
  148. {
  149. if (do_pollfd(pf, &pt->req))
  150. {
  151. num ++;
  152. pt->req._proc = RT_NULL;
  153. }
  154. pf ++;
  155. }
  156. pt->req._proc = RT_NULL;
  157. if (num || istimeout)
  158. break;
  159. if (poll_wait_timeout(pt, msec))
  160. istimeout = 1;
  161. }
  162. return num;
  163. }
  164. static void poll_teardown(struct rt_poll_table *pt)
  165. {
  166. struct rt_poll_node *node, *next;
  167. next = pt->nodes;
  168. while (next)
  169. {
  170. node = next;
  171. rt_wqueue_remove(&node->wqn);
  172. next = node->next;
  173. rt_free(node);
  174. }
  175. }
  176. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  177. {
  178. int num;
  179. struct rt_poll_table table;
  180. poll_table_init(&table);
  181. num = poll_do(fds, nfds, &table, timeout);
  182. poll_teardown(&table);
  183. return num;
  184. }