poll.c 4.9 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. */
  24. #include <stdint.h>
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #include <rtthread.h>
  28. #include <dfs.h>
  29. #include <dfs_file.h>
  30. #include <dfs_posix.h>
  31. #include <dfs_poll.h>
  32. struct rt_poll_node;
  33. struct rt_poll_table
  34. {
  35. rt_pollreq_t req;
  36. rt_uint32_t triggered; /* the waited thread whether triggered */
  37. rt_thread_t polling_thread;
  38. struct rt_poll_node *nodes;
  39. };
  40. struct rt_poll_node
  41. {
  42. struct rt_wqueue_node wqn;
  43. struct rt_poll_table *pt;
  44. struct rt_poll_node *next;
  45. };
  46. static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
  47. {
  48. struct rt_poll_node *pn;
  49. if (key && !((rt_uint32_t)key & wait->key))
  50. return -1;
  51. pn = rt_container_of(wait, struct rt_poll_node, wqn);
  52. pn->pt->triggered = 1;
  53. return __wqueue_default_wake(wait, key);
  54. }
  55. static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
  56. {
  57. struct rt_poll_table *pt;
  58. struct rt_poll_node *node;
  59. node = rt_malloc(sizeof(struct rt_poll_node));
  60. if (node == RT_NULL)
  61. return;
  62. pt = rt_container_of(req, struct rt_poll_table, req);
  63. node->wqn.key = req->_key;
  64. rt_list_init(&(node->wqn.list));
  65. node->wqn.polling_thread = pt->polling_thread;
  66. node->wqn.wakeup = __wqueue_pollwake;
  67. node->next = pt->nodes;
  68. node->pt = pt;
  69. pt->nodes = node;
  70. rt_wqueue_add(wq, &node->wqn);
  71. }
  72. static void poll_table_init(struct rt_poll_table *pt)
  73. {
  74. pt->req._proc = _poll_add;
  75. pt->triggered = 0;
  76. pt->nodes = RT_NULL;
  77. pt->polling_thread = rt_thread_self();
  78. }
  79. static int poll_wait_timeout(struct rt_poll_table *pt, int msec)
  80. {
  81. rt_int32_t timeout;
  82. int ret = 0;
  83. struct rt_thread *thread;
  84. rt_base_t level;
  85. thread = pt->polling_thread;
  86. timeout = rt_tick_from_millisecond(msec);
  87. level = rt_hw_interrupt_disable();
  88. if (timeout != 0 && !pt->triggered)
  89. {
  90. rt_thread_suspend(thread);
  91. if (timeout > 0)
  92. {
  93. rt_timer_control(&(thread->thread_timer),
  94. RT_TIMER_CTRL_SET_TIME,
  95. &timeout);
  96. rt_timer_start(&(thread->thread_timer));
  97. }
  98. rt_hw_interrupt_enable(level);
  99. rt_schedule();
  100. }
  101. else
  102. {
  103. rt_hw_interrupt_enable(level);
  104. }
  105. ret = !pt->triggered;
  106. return ret;
  107. }
  108. static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
  109. {
  110. int mask = 0;
  111. int fd;
  112. fd = pollfd->fd;
  113. if (fd >= 0)
  114. {
  115. struct dfs_fd *f = fd_get(fd);
  116. mask = POLLNVAL;
  117. if (f)
  118. {
  119. mask = POLLMASK_DEFAULT;
  120. if (f->fops->poll)
  121. {
  122. req->_key = pollfd->events | POLLERR| POLLHUP;
  123. mask = f->fops->poll(f, req);
  124. }
  125. /* Mask out unneeded events. */
  126. mask &= pollfd->events | POLLERR | POLLHUP;
  127. fd_put(f);
  128. }
  129. }
  130. pollfd->revents = mask;
  131. return mask;
  132. }
  133. static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
  134. {
  135. int num;
  136. int istimeout = 0;
  137. int n;
  138. struct pollfd *pf;
  139. if (msec == 0)
  140. {
  141. pt->req._proc = RT_NULL;
  142. istimeout = 1;
  143. }
  144. while (1)
  145. {
  146. pf = fds;
  147. num = 0;
  148. for (n = 0; n < nfds; n ++)
  149. {
  150. if (do_pollfd(pf, &pt->req))
  151. {
  152. num ++;
  153. pt->req._proc = RT_NULL;
  154. }
  155. pf ++;
  156. }
  157. pt->req._proc = RT_NULL;
  158. if (num || istimeout)
  159. break;
  160. if (poll_wait_timeout(pt, msec))
  161. istimeout = 1;
  162. }
  163. return num;
  164. }
  165. static void poll_teardown(struct rt_poll_table *pt)
  166. {
  167. struct rt_poll_node *node, *next;
  168. next = pt->nodes;
  169. while (next)
  170. {
  171. node = next;
  172. rt_wqueue_remove(&node->wqn);
  173. next = node->next;
  174. rt_free(node);
  175. }
  176. }
  177. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  178. {
  179. int num;
  180. struct rt_poll_table table;
  181. poll_table_init(&table);
  182. num = poll_do(fds, nfds, &table, timeout);
  183. poll_teardown(&table);
  184. return num;
  185. }