2
0

event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is the Netscape Portable Runtime library.
  13. *
  14. * The Initial Developer of the Original Code is Netscape
  15. * Communications Corporation. Portions created by Netscape are
  16. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  17. * Rights Reserved.
  18. *
  19. * Contributor(s): Silicon Graphics, Inc.
  20. * Yahoo! Inc.
  21. *
  22. * Alternatively, the contents of this file may be used under the
  23. * terms of the GNU General Public License Version 2 or later (the
  24. * "GPL"), in which case the provisions of the GPL are applicable
  25. * instead of those above. If you wish to allow use of your
  26. * version of this file only under the terms of the GPL and not to
  27. * allow others to use your version of this file under the MPL,
  28. * indicate your decision by deleting the provisions above and
  29. * replace them with the notice and other provisions required by
  30. * the GPL. If you do not delete the provisions above, a recipient
  31. * may use your version of this file under either the MPL or the
  32. * GPL.
  33. */
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <errno.h>
  40. #include "common.h"
  41. #ifdef USE_POLL
  42. #error "Not support USE_POLL"
  43. #endif
  44. #ifdef MD_HAVE_KQUEUE
  45. #error "Not support MD_HAVE_KQUEUE"
  46. #endif
  47. #ifdef MD_HAVE_POLL
  48. #error "Not support MD_HAVE_POLL"
  49. #endif
  50. #ifndef MD_HAVE_EPOLL
  51. #error "Only support MD_HAVE_EPOLL"
  52. #endif
  53. #include <sys/epoll.h>
  54. typedef struct _epoll_fd_data {
  55. int rd_ref_cnt;
  56. int wr_ref_cnt;
  57. int ex_ref_cnt;
  58. int revents;
  59. } _epoll_fd_data_t;
  60. static struct _st_epolldata {
  61. _epoll_fd_data_t *fd_data;
  62. struct epoll_event *evtlist;
  63. int fd_data_size;
  64. int evtlist_size;
  65. int evtlist_cnt;
  66. int fd_hint;
  67. int epfd;
  68. pid_t pid;
  69. } *_st_epoll_data;
  70. #ifndef ST_EPOLL_EVTLIST_SIZE
  71. /* Not a limit, just a hint */
  72. #define ST_EPOLL_EVTLIST_SIZE 4096
  73. #endif
  74. #define _ST_EPOLL_READ_CNT(fd) (_st_epoll_data->fd_data[fd].rd_ref_cnt)
  75. #define _ST_EPOLL_WRITE_CNT(fd) (_st_epoll_data->fd_data[fd].wr_ref_cnt)
  76. #define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt)
  77. #define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents)
  78. #define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0)
  79. #define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0)
  80. #define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0)
  81. #define _ST_EPOLL_EVENTS(fd) \
  82. (_ST_EPOLL_READ_BIT(fd)|_ST_EPOLL_WRITE_BIT(fd)|_ST_EPOLL_EXCEP_BIT(fd))
  83. _st_eventsys_t *_st_eventsys = NULL;
  84. /*****************************************
  85. * epoll event system
  86. */
  87. ST_HIDDEN int _st_epoll_init(void)
  88. {
  89. int fdlim;
  90. int err = 0;
  91. int rv = 0;
  92. _st_epoll_data = (struct _st_epolldata *) calloc(1, sizeof(*_st_epoll_data));
  93. if (!_st_epoll_data) {
  94. return -1;
  95. }
  96. fdlim = st_getfdlimit();
  97. _st_epoll_data->fd_hint = (fdlim > 0 && fdlim < ST_EPOLL_EVTLIST_SIZE) ? fdlim : ST_EPOLL_EVTLIST_SIZE;
  98. if ((_st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint)) < 0) {
  99. err = errno;
  100. rv = -1;
  101. goto cleanup_epoll;
  102. }
  103. fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC);
  104. _st_epoll_data->pid = getpid();
  105. /* Allocate file descriptor data array */
  106. _st_epoll_data->fd_data_size = _st_epoll_data->fd_hint;
  107. _st_epoll_data->fd_data = (_epoll_fd_data_t *)calloc(_st_epoll_data->fd_data_size, sizeof(_epoll_fd_data_t));
  108. if (!_st_epoll_data->fd_data) {
  109. err = errno;
  110. rv = -1;
  111. goto cleanup_epoll;
  112. }
  113. /* Allocate event lists */
  114. _st_epoll_data->evtlist_size = _st_epoll_data->fd_hint;
  115. _st_epoll_data->evtlist = (struct epoll_event *)malloc(_st_epoll_data->evtlist_size * sizeof(struct epoll_event));
  116. if (!_st_epoll_data->evtlist) {
  117. err = errno;
  118. rv = -1;
  119. }
  120. cleanup_epoll:
  121. if (rv < 0) {
  122. if (_st_epoll_data->epfd >= 0) {
  123. close(_st_epoll_data->epfd);
  124. }
  125. free(_st_epoll_data->fd_data);
  126. free(_st_epoll_data->evtlist);
  127. free(_st_epoll_data);
  128. _st_epoll_data = NULL;
  129. errno = err;
  130. }
  131. return rv;
  132. }
  133. ST_HIDDEN int _st_epoll_fd_data_expand(int maxfd)
  134. {
  135. _epoll_fd_data_t *ptr;
  136. int n = _st_epoll_data->fd_data_size;
  137. while (maxfd >= n) {
  138. n <<= 1;
  139. }
  140. ptr = (_epoll_fd_data_t *)realloc(_st_epoll_data->fd_data, n * sizeof(_epoll_fd_data_t));
  141. if (!ptr) {
  142. return -1;
  143. }
  144. memset(ptr + _st_epoll_data->fd_data_size, 0, (n - _st_epoll_data->fd_data_size) * sizeof(_epoll_fd_data_t));
  145. _st_epoll_data->fd_data = ptr;
  146. _st_epoll_data->fd_data_size = n;
  147. return 0;
  148. }
  149. ST_HIDDEN void _st_epoll_evtlist_expand(void)
  150. {
  151. struct epoll_event *ptr;
  152. int n = _st_epoll_data->evtlist_size;
  153. while (_st_epoll_data->evtlist_cnt > n) {
  154. n <<= 1;
  155. }
  156. ptr = (struct epoll_event *)realloc(_st_epoll_data->evtlist, n * sizeof(struct epoll_event));
  157. if (ptr) {
  158. _st_epoll_data->evtlist = ptr;
  159. _st_epoll_data->evtlist_size = n;
  160. }
  161. }
  162. ST_HIDDEN void _st_epoll_pollset_del(struct pollfd *pds, int npds)
  163. {
  164. struct epoll_event ev;
  165. struct pollfd *pd;
  166. struct pollfd *epd = pds + npds;
  167. int old_events, events, op;
  168. /*
  169. * It's more or less OK if deleting fails because a descriptor
  170. * will either be closed or deleted in dispatch function after
  171. * it fires.
  172. */
  173. for (pd = pds; pd < epd; pd++) {
  174. old_events = _ST_EPOLL_EVENTS(pd->fd);
  175. if (pd->events & POLLIN) {
  176. _ST_EPOLL_READ_CNT(pd->fd)--;
  177. }
  178. if (pd->events & POLLOUT) {
  179. _ST_EPOLL_WRITE_CNT(pd->fd)--;
  180. }
  181. if (pd->events & POLLPRI) {
  182. _ST_EPOLL_EXCEP_CNT(pd->fd)--;
  183. }
  184. events = _ST_EPOLL_EVENTS(pd->fd);
  185. /*
  186. * The _ST_EPOLL_REVENTS check below is needed so we can use
  187. * this function inside dispatch(). Outside of dispatch()
  188. * _ST_EPOLL_REVENTS is always zero for all descriptors.
  189. */
  190. if (events != old_events && _ST_EPOLL_REVENTS(pd->fd) == 0) {
  191. op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
  192. ev.events = events;
  193. ev.data.fd = pd->fd;
  194. if (epoll_ctl(_st_epoll_data->epfd, op, pd->fd, &ev) == 0 && op == EPOLL_CTL_DEL) {
  195. _st_epoll_data->evtlist_cnt--;
  196. }
  197. }
  198. }
  199. }
  200. ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds)
  201. {
  202. struct epoll_event ev;
  203. int i, fd;
  204. int old_events, events, op;
  205. /* Do as many checks as possible up front */
  206. for (i = 0; i < npds; i++) {
  207. fd = pds[i].fd;
  208. if (fd < 0 || !pds[i].events || (pds[i].events & ~(POLLIN | POLLOUT | POLLPRI))) {
  209. errno = EINVAL;
  210. return -1;
  211. }
  212. if (fd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(fd) < 0) {
  213. return -1;
  214. }
  215. }
  216. for (i = 0; i < npds; i++) {
  217. fd = pds[i].fd;
  218. old_events = _ST_EPOLL_EVENTS(fd);
  219. if (pds[i].events & POLLIN) {
  220. _ST_EPOLL_READ_CNT(fd)++;
  221. }
  222. if (pds[i].events & POLLOUT) {
  223. _ST_EPOLL_WRITE_CNT(fd)++;
  224. }
  225. if (pds[i].events & POLLPRI) {
  226. _ST_EPOLL_EXCEP_CNT(fd)++;
  227. }
  228. events = _ST_EPOLL_EVENTS(fd);
  229. if (events != old_events) {
  230. op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
  231. ev.events = events;
  232. ev.data.fd = fd;
  233. if (epoll_ctl(_st_epoll_data->epfd, op, fd, &ev) < 0 && (op != EPOLL_CTL_ADD || errno != EEXIST)) {
  234. break;
  235. }
  236. if (op == EPOLL_CTL_ADD) {
  237. _st_epoll_data->evtlist_cnt++;
  238. if (_st_epoll_data->evtlist_cnt > _st_epoll_data->evtlist_size) {
  239. _st_epoll_evtlist_expand();
  240. }
  241. }
  242. }
  243. }
  244. if (i < npds) {
  245. /* Error */
  246. int err = errno;
  247. /* Unroll the state */
  248. _st_epoll_pollset_del(pds, i + 1);
  249. errno = err;
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. ST_HIDDEN void _st_epoll_dispatch(void)
  255. {
  256. st_utime_t min_timeout;
  257. _st_clist_t *q;
  258. _st_pollq_t *pq;
  259. struct pollfd *pds, *epds;
  260. struct epoll_event ev;
  261. int timeout, nfd, i, osfd, notify;
  262. int events, op;
  263. short revents;
  264. if (_ST_SLEEPQ == NULL) {
  265. timeout = -1;
  266. } else {
  267. min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
  268. timeout = (int) (min_timeout / 1000);
  269. }
  270. if (_st_epoll_data->pid != getpid()) {
  271. // WINLIN: remove it for bug introduced.
  272. // @see: https://github.com/ossrs/srs/issues/193
  273. exit(-1);
  274. }
  275. /* Check for I/O operations */
  276. nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout);
  277. if (nfd > 0) {
  278. for (i = 0; i < nfd; i++) {
  279. osfd = _st_epoll_data->evtlist[i].data.fd;
  280. _ST_EPOLL_REVENTS(osfd) = _st_epoll_data->evtlist[i].events;
  281. if (_ST_EPOLL_REVENTS(osfd) & (EPOLLERR | EPOLLHUP)) {
  282. /* Also set I/O bits on error */
  283. _ST_EPOLL_REVENTS(osfd) |= _ST_EPOLL_EVENTS(osfd);
  284. }
  285. }
  286. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  287. pq = _ST_POLLQUEUE_PTR(q);
  288. notify = 0;
  289. epds = pq->pds + pq->npds;
  290. for (pds = pq->pds; pds < epds; pds++) {
  291. if (_ST_EPOLL_REVENTS(pds->fd) == 0) {
  292. pds->revents = 0;
  293. continue;
  294. }
  295. osfd = pds->fd;
  296. events = pds->events;
  297. revents = 0;
  298. if ((events & POLLIN) && (_ST_EPOLL_REVENTS(osfd) & EPOLLIN)) {
  299. revents |= POLLIN;
  300. }
  301. if ((events & POLLOUT) && (_ST_EPOLL_REVENTS(osfd) & EPOLLOUT)) {
  302. revents |= POLLOUT;
  303. }
  304. if ((events & POLLPRI) && (_ST_EPOLL_REVENTS(osfd) & EPOLLPRI)) {
  305. revents |= POLLPRI;
  306. }
  307. if (_ST_EPOLL_REVENTS(osfd) & EPOLLERR) {
  308. revents |= POLLERR;
  309. }
  310. if (_ST_EPOLL_REVENTS(osfd) & EPOLLHUP) {
  311. revents |= POLLHUP;
  312. }
  313. pds->revents = revents;
  314. if (revents) {
  315. notify = 1;
  316. }
  317. }
  318. if (notify) {
  319. ST_REMOVE_LINK(&pq->links);
  320. pq->on_ioq = 0;
  321. /*
  322. * Here we will only delete/modify descriptors that
  323. * didn't fire (see comments in _st_epoll_pollset_del()).
  324. */
  325. _st_epoll_pollset_del(pq->pds, pq->npds);
  326. if (pq->thread->flags & _ST_FL_ON_SLEEPQ) {
  327. _ST_DEL_SLEEPQ(pq->thread);
  328. }
  329. pq->thread->state = _ST_ST_RUNNABLE;
  330. _ST_ADD_RUNQ(pq->thread);
  331. }
  332. }
  333. for (i = 0; i < nfd; i++) {
  334. /* Delete/modify descriptors that fired */
  335. osfd = _st_epoll_data->evtlist[i].data.fd;
  336. _ST_EPOLL_REVENTS(osfd) = 0;
  337. events = _ST_EPOLL_EVENTS(osfd);
  338. op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
  339. ev.events = events;
  340. ev.data.fd = osfd;
  341. if (epoll_ctl(_st_epoll_data->epfd, op, osfd, &ev) == 0 && op == EPOLL_CTL_DEL) {
  342. _st_epoll_data->evtlist_cnt--;
  343. }
  344. }
  345. }
  346. }
  347. ST_HIDDEN int _st_epoll_fd_new(int osfd)
  348. {
  349. if (osfd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(osfd) < 0) {
  350. return -1;
  351. }
  352. return 0;
  353. }
  354. ST_HIDDEN int _st_epoll_fd_close(int osfd)
  355. {
  356. if (_ST_EPOLL_READ_CNT(osfd) || _ST_EPOLL_WRITE_CNT(osfd) || _ST_EPOLL_EXCEP_CNT(osfd)) {
  357. errno = EBUSY;
  358. return -1;
  359. }
  360. return 0;
  361. }
  362. ST_HIDDEN int _st_epoll_fd_getlimit(void)
  363. {
  364. /* zero means no specific limit */
  365. return 0;
  366. }
  367. /*
  368. * Check if epoll functions are just stubs.
  369. */
  370. ST_HIDDEN int _st_epoll_is_supported(void)
  371. {
  372. struct epoll_event ev;
  373. ev.events = EPOLLIN;
  374. ev.data.ptr = NULL;
  375. /* Guaranteed to fail */
  376. epoll_ctl(-1, EPOLL_CTL_ADD, -1, &ev);
  377. return (errno != ENOSYS);
  378. }
  379. static _st_eventsys_t _st_epoll_eventsys = {
  380. "epoll",
  381. ST_EVENTSYS_ALT,
  382. _st_epoll_init,
  383. _st_epoll_dispatch,
  384. _st_epoll_pollset_add,
  385. _st_epoll_pollset_del,
  386. _st_epoll_fd_new,
  387. _st_epoll_fd_close,
  388. _st_epoll_fd_getlimit
  389. };
  390. /*****************************************
  391. * Public functions
  392. */
  393. int st_set_eventsys(int eventsys)
  394. {
  395. if (_st_eventsys) {
  396. errno = EBUSY;
  397. return -1;
  398. }
  399. switch (eventsys) {
  400. case ST_EVENTSYS_DEFAULT:
  401. case ST_EVENTSYS_ALT:
  402. default:
  403. if (_st_epoll_is_supported()) {
  404. _st_eventsys = &_st_epoll_eventsys;
  405. break;
  406. }
  407. errno = EINVAL;
  408. return -1;
  409. }
  410. return 0;
  411. }
  412. int st_get_eventsys(void)
  413. {
  414. return _st_eventsys ? _st_eventsys->val : -1;
  415. }
  416. const char *st_get_eventsys_name(void)
  417. {
  418. return _st_eventsys ? _st_eventsys->name : "";
  419. }