event.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */
  2. /*
  3. * The contents of this file are subject to the Mozilla Public
  4. * License Version 1.1 (the "License"); you may not use this file
  5. * except in compliance with the License. You may obtain a copy of
  6. * the License at http://www.mozilla.org/MPL/
  7. *
  8. * Software distributed under the License is distributed on an "AS
  9. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing
  11. * rights and limitations under the License.
  12. *
  13. * The Original Code is the Netscape Portable Runtime library.
  14. *
  15. * The Initial Developer of the Original Code is Netscape
  16. * Communications Corporation. Portions created by Netscape are
  17. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  18. * Rights Reserved.
  19. *
  20. * Contributor(s): Silicon Graphics, Inc.
  21. * Yahoo! Inc.
  22. *
  23. * Alternatively, the contents of this file may be used under the
  24. * terms of the GNU General Public License Version 2 or later (the
  25. * "GPL"), in which case the provisions of the GPL are applicable
  26. * instead of those above. If you wish to allow use of your
  27. * version of this file only under the terms of the GPL and not to
  28. * allow others to use your version of this file under the MPL,
  29. * indicate your decision by deleting the provisions above and
  30. * replace them with the notice and other provisions required by
  31. * the GPL. If you do not delete the provisions above, a recipient
  32. * may use your version of this file under either the MPL or the
  33. * GPL.
  34. */
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <errno.h>
  41. #include "common.h"
  42. #ifdef MD_HAVE_KQUEUE
  43. #include <sys/event.h>
  44. #endif
  45. #ifdef MD_HAVE_EPOLL
  46. #include <sys/epoll.h>
  47. #endif
  48. // Global stat.
  49. #if defined(DEBUG) && defined(DEBUG_STATS)
  50. __thread unsigned long long _st_stat_epoll = 0;
  51. __thread unsigned long long _st_stat_epoll_zero = 0;
  52. __thread unsigned long long _st_stat_epoll_shake = 0;
  53. __thread unsigned long long _st_stat_epoll_spin = 0;
  54. #endif
  55. #if !defined(MD_HAVE_KQUEUE) && !defined(MD_HAVE_EPOLL) && !defined(MD_HAVE_SELECT)
  56. #error Only support epoll(for Linux), kqueue(for Darwin) or select(for Cygwin)
  57. #endif
  58. #ifdef MD_HAVE_SELECT
  59. static struct _st_seldata {
  60. fd_set fd_read_set, fd_write_set, fd_exception_set;
  61. int fd_ref_cnts[FD_SETSIZE][3];
  62. int maxfd;
  63. } *_st_select_data;
  64. #define _ST_SELECT_MAX_OSFD (_st_select_data->maxfd)
  65. #define _ST_SELECT_READ_SET (_st_select_data->fd_read_set)
  66. #define _ST_SELECT_WRITE_SET (_st_select_data->fd_write_set)
  67. #define _ST_SELECT_EXCEP_SET (_st_select_data->fd_exception_set)
  68. #define _ST_SELECT_READ_CNT(fd) (_st_select_data->fd_ref_cnts[fd][0])
  69. #define _ST_SELECT_WRITE_CNT(fd) (_st_select_data->fd_ref_cnts[fd][1])
  70. #define _ST_SELECT_EXCEP_CNT(fd) (_st_select_data->fd_ref_cnts[fd][2])
  71. #endif
  72. #ifdef MD_HAVE_KQUEUE
  73. typedef struct _kq_fd_data {
  74. int rd_ref_cnt;
  75. int wr_ref_cnt;
  76. int revents;
  77. } _kq_fd_data_t;
  78. static __thread struct _st_kqdata {
  79. _kq_fd_data_t *fd_data;
  80. struct kevent *evtlist;
  81. struct kevent *addlist;
  82. struct kevent *dellist;
  83. int fd_data_size;
  84. int evtlist_size;
  85. int addlist_size;
  86. int addlist_cnt;
  87. int dellist_size;
  88. int dellist_cnt;
  89. int kq;
  90. pid_t pid;
  91. } *_st_kq_data;
  92. #ifndef ST_KQ_MIN_EVTLIST_SIZE
  93. #define ST_KQ_MIN_EVTLIST_SIZE 64
  94. #endif
  95. #define _ST_KQ_READ_CNT(fd) (_st_kq_data->fd_data[fd].rd_ref_cnt)
  96. #define _ST_KQ_WRITE_CNT(fd) (_st_kq_data->fd_data[fd].wr_ref_cnt)
  97. #define _ST_KQ_REVENTS(fd) (_st_kq_data->fd_data[fd].revents)
  98. #endif /* MD_HAVE_KQUEUE */
  99. #ifdef MD_HAVE_EPOLL
  100. typedef struct _epoll_fd_data {
  101. int rd_ref_cnt;
  102. int wr_ref_cnt;
  103. int ex_ref_cnt;
  104. int revents;
  105. } _epoll_fd_data_t;
  106. static __thread struct _st_epolldata {
  107. _epoll_fd_data_t *fd_data;
  108. struct epoll_event *evtlist;
  109. int fd_data_size;
  110. int evtlist_size;
  111. int evtlist_cnt;
  112. int fd_hint;
  113. int epfd;
  114. } *_st_epoll_data;
  115. #ifndef ST_EPOLL_EVTLIST_SIZE
  116. /* Not a limit, just a hint */
  117. #define ST_EPOLL_EVTLIST_SIZE 4096
  118. #endif
  119. #define _ST_EPOLL_READ_CNT(fd) (_st_epoll_data->fd_data[fd].rd_ref_cnt)
  120. #define _ST_EPOLL_WRITE_CNT(fd) (_st_epoll_data->fd_data[fd].wr_ref_cnt)
  121. #define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt)
  122. #define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents)
  123. #define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0)
  124. #define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0)
  125. #define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0)
  126. #define _ST_EPOLL_EVENTS(fd) \
  127. (_ST_EPOLL_READ_BIT(fd)|_ST_EPOLL_WRITE_BIT(fd)|_ST_EPOLL_EXCEP_BIT(fd))
  128. #endif /* MD_HAVE_EPOLL */
  129. __thread _st_eventsys_t *_st_eventsys = NULL;
  130. #ifdef MD_HAVE_SELECT
  131. /*****************************************
  132. * select event system
  133. */
  134. ST_HIDDEN int _st_select_init(void)
  135. {
  136. _st_select_data = (struct _st_seldata *) malloc(sizeof(*_st_select_data));
  137. if (!_st_select_data)
  138. return -1;
  139. memset(_st_select_data, 0, sizeof(*_st_select_data));
  140. _st_select_data->maxfd = -1;
  141. return 0;
  142. }
  143. ST_HIDDEN int _st_select_pollset_add(struct pollfd *pds, int npds)
  144. {
  145. struct pollfd *pd;
  146. struct pollfd *epd = pds + npds;
  147. /* Do checks up front */
  148. for (pd = pds; pd < epd; pd++) {
  149. if (pd->fd < 0 || pd->fd >= FD_SETSIZE || !pd->events ||
  150. (pd->events & ~(POLLIN | POLLOUT | POLLPRI))) {
  151. errno = EINVAL;
  152. return -1;
  153. }
  154. }
  155. for (pd = pds; pd < epd; pd++) {
  156. if (pd->events & POLLIN) {
  157. FD_SET(pd->fd, &_ST_SELECT_READ_SET);
  158. _ST_SELECT_READ_CNT(pd->fd)++;
  159. }
  160. if (pd->events & POLLOUT) {
  161. FD_SET(pd->fd, &_ST_SELECT_WRITE_SET);
  162. _ST_SELECT_WRITE_CNT(pd->fd)++;
  163. }
  164. if (pd->events & POLLPRI) {
  165. FD_SET(pd->fd, &_ST_SELECT_EXCEP_SET);
  166. _ST_SELECT_EXCEP_CNT(pd->fd)++;
  167. }
  168. if (_ST_SELECT_MAX_OSFD < pd->fd)
  169. _ST_SELECT_MAX_OSFD = pd->fd;
  170. }
  171. return 0;
  172. }
  173. ST_HIDDEN void _st_select_pollset_del(struct pollfd *pds, int npds)
  174. {
  175. struct pollfd *pd;
  176. struct pollfd *epd = pds + npds;
  177. for (pd = pds; pd < epd; pd++) {
  178. if (pd->events & POLLIN) {
  179. if (--_ST_SELECT_READ_CNT(pd->fd) == 0)
  180. FD_CLR(pd->fd, &_ST_SELECT_READ_SET);
  181. }
  182. if (pd->events & POLLOUT) {
  183. if (--_ST_SELECT_WRITE_CNT(pd->fd) == 0)
  184. FD_CLR(pd->fd, &_ST_SELECT_WRITE_SET);
  185. }
  186. if (pd->events & POLLPRI) {
  187. if (--_ST_SELECT_EXCEP_CNT(pd->fd) == 0)
  188. FD_CLR(pd->fd, &_ST_SELECT_EXCEP_SET);
  189. }
  190. }
  191. }
  192. ST_HIDDEN void _st_select_find_bad_fd(void)
  193. {
  194. _st_clist_t *q;
  195. _st_pollq_t *pq;
  196. int notify;
  197. struct pollfd *pds, *epds;
  198. int pq_max_osfd, osfd;
  199. short events;
  200. _ST_SELECT_MAX_OSFD = -1;
  201. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  202. pq = _ST_POLLQUEUE_PTR(q);
  203. notify = 0;
  204. epds = pq->pds + pq->npds;
  205. pq_max_osfd = -1;
  206. for (pds = pq->pds; pds < epds; pds++) {
  207. osfd = pds->fd;
  208. pds->revents = 0;
  209. if (pds->events == 0)
  210. continue;
  211. if (fcntl(osfd, F_GETFL, 0) < 0) {
  212. pds->revents = POLLNVAL;
  213. notify = 1;
  214. }
  215. if (osfd > pq_max_osfd) {
  216. pq_max_osfd = osfd;
  217. }
  218. }
  219. if (notify) {
  220. ST_REMOVE_LINK(&pq->links);
  221. pq->on_ioq = 0;
  222. /*
  223. * Decrement the count of descriptors for each descriptor/event
  224. * because this I/O request is being removed from the ioq
  225. */
  226. for (pds = pq->pds; pds < epds; pds++) {
  227. osfd = pds->fd;
  228. events = pds->events;
  229. if (events & POLLIN) {
  230. if (--_ST_SELECT_READ_CNT(osfd) == 0) {
  231. FD_CLR(osfd, &_ST_SELECT_READ_SET);
  232. }
  233. }
  234. if (events & POLLOUT) {
  235. if (--_ST_SELECT_WRITE_CNT(osfd) == 0) {
  236. FD_CLR(osfd, &_ST_SELECT_WRITE_SET);
  237. }
  238. }
  239. if (events & POLLPRI) {
  240. if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) {
  241. FD_CLR(osfd, &_ST_SELECT_EXCEP_SET);
  242. }
  243. }
  244. }
  245. if (pq->thread->flags & _ST_FL_ON_SLEEPQ)
  246. _ST_DEL_SLEEPQ(pq->thread);
  247. pq->thread->state = _ST_ST_RUNNABLE;
  248. _ST_ADD_RUNQ(pq->thread);
  249. } else {
  250. if (_ST_SELECT_MAX_OSFD < pq_max_osfd)
  251. _ST_SELECT_MAX_OSFD = pq_max_osfd;
  252. }
  253. }
  254. }
  255. ST_HIDDEN void _st_select_dispatch(void)
  256. {
  257. struct timeval timeout, *tvp;
  258. fd_set r, w, e;
  259. fd_set *rp, *wp, *ep;
  260. int nfd, pq_max_osfd, osfd;
  261. _st_clist_t *q;
  262. st_utime_t min_timeout;
  263. _st_pollq_t *pq;
  264. int notify;
  265. struct pollfd *pds, *epds;
  266. short events, revents;
  267. /*
  268. * Assignment of fd_sets
  269. */
  270. r = _ST_SELECT_READ_SET;
  271. w = _ST_SELECT_WRITE_SET;
  272. e = _ST_SELECT_EXCEP_SET;
  273. rp = &r;
  274. wp = &w;
  275. ep = &e;
  276. if (_ST_SLEEPQ == NULL) {
  277. tvp = NULL;
  278. } else {
  279. min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 :
  280. (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
  281. timeout.tv_sec = (int) (min_timeout / 1000000);
  282. timeout.tv_usec = (int) (min_timeout % 1000000);
  283. tvp = &timeout;
  284. }
  285. /* Check for I/O operations */
  286. nfd = select(_ST_SELECT_MAX_OSFD + 1, rp, wp, ep, tvp);
  287. /* Notify threads that are associated with the selected descriptors */
  288. if (nfd > 0) {
  289. _ST_SELECT_MAX_OSFD = -1;
  290. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  291. pq = _ST_POLLQUEUE_PTR(q);
  292. notify = 0;
  293. epds = pq->pds + pq->npds;
  294. pq_max_osfd = -1;
  295. for (pds = pq->pds; pds < epds; pds++) {
  296. osfd = pds->fd;
  297. events = pds->events;
  298. revents = 0;
  299. if ((events & POLLIN) && FD_ISSET(osfd, rp)) {
  300. revents |= POLLIN;
  301. }
  302. if ((events & POLLOUT) && FD_ISSET(osfd, wp)) {
  303. revents |= POLLOUT;
  304. }
  305. if ((events & POLLPRI) && FD_ISSET(osfd, ep)) {
  306. revents |= POLLPRI;
  307. }
  308. pds->revents = revents;
  309. if (revents) {
  310. notify = 1;
  311. }
  312. if (osfd > pq_max_osfd) {
  313. pq_max_osfd = osfd;
  314. }
  315. }
  316. if (notify) {
  317. ST_REMOVE_LINK(&pq->links);
  318. pq->on_ioq = 0;
  319. /*
  320. * Decrement the count of descriptors for each descriptor/event
  321. * because this I/O request is being removed from the ioq
  322. */
  323. for (pds = pq->pds; pds < epds; pds++) {
  324. osfd = pds->fd;
  325. events = pds->events;
  326. if (events & POLLIN) {
  327. if (--_ST_SELECT_READ_CNT(osfd) == 0) {
  328. FD_CLR(osfd, &_ST_SELECT_READ_SET);
  329. }
  330. }
  331. if (events & POLLOUT) {
  332. if (--_ST_SELECT_WRITE_CNT(osfd) == 0) {
  333. FD_CLR(osfd, &_ST_SELECT_WRITE_SET);
  334. }
  335. }
  336. if (events & POLLPRI) {
  337. if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) {
  338. FD_CLR(osfd, &_ST_SELECT_EXCEP_SET);
  339. }
  340. }
  341. }
  342. if (pq->thread->flags & _ST_FL_ON_SLEEPQ)
  343. _ST_DEL_SLEEPQ(pq->thread);
  344. pq->thread->state = _ST_ST_RUNNABLE;
  345. _ST_ADD_RUNQ(pq->thread);
  346. } else {
  347. if (_ST_SELECT_MAX_OSFD < pq_max_osfd)
  348. _ST_SELECT_MAX_OSFD = pq_max_osfd;
  349. }
  350. }
  351. } else if (nfd < 0) {
  352. /*
  353. * It can happen when a thread closes file descriptor
  354. * that is being used by some other thread -- BAD!
  355. */
  356. if (errno == EBADF)
  357. _st_select_find_bad_fd();
  358. }
  359. }
  360. ST_HIDDEN int _st_select_fd_new(int osfd)
  361. {
  362. if (osfd >= FD_SETSIZE) {
  363. errno = EMFILE;
  364. return -1;
  365. }
  366. return 0;
  367. }
  368. ST_HIDDEN int _st_select_fd_close(int osfd)
  369. {
  370. if (_ST_SELECT_READ_CNT(osfd) || _ST_SELECT_WRITE_CNT(osfd) ||
  371. _ST_SELECT_EXCEP_CNT(osfd)) {
  372. errno = EBUSY;
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. ST_HIDDEN int _st_select_fd_getlimit(void)
  378. {
  379. return FD_SETSIZE;
  380. }
  381. ST_HIDDEN void _st_select_destroy(void)
  382. {
  383. /* TODO: FIXME: Implements it */
  384. }
  385. static _st_eventsys_t _st_select_eventsys = {
  386. "select",
  387. ST_EVENTSYS_SELECT,
  388. _st_select_init,
  389. _st_select_dispatch,
  390. _st_select_pollset_add,
  391. _st_select_pollset_del,
  392. _st_select_fd_new,
  393. _st_select_fd_close,
  394. _st_select_fd_getlimit,
  395. _st_select_destroy
  396. };
  397. #endif
  398. #ifdef MD_HAVE_KQUEUE
  399. /*****************************************
  400. * kqueue event system
  401. */
  402. ST_HIDDEN int _st_kq_init(void)
  403. {
  404. int err = 0;
  405. int rv = 0;
  406. _st_kq_data = (struct _st_kqdata *) calloc(1, sizeof(*_st_kq_data));
  407. if (!_st_kq_data)
  408. return -1;
  409. if ((_st_kq_data->kq = kqueue()) < 0) {
  410. err = errno;
  411. rv = -1;
  412. goto cleanup_kq;
  413. }
  414. fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC);
  415. _st_kq_data->pid = getpid();
  416. /*
  417. * Allocate file descriptor data array.
  418. * FD_SETSIZE looks like good initial size.
  419. */
  420. _st_kq_data->fd_data_size = FD_SETSIZE;
  421. _st_kq_data->fd_data = (_kq_fd_data_t *)calloc(_st_kq_data->fd_data_size, sizeof(_kq_fd_data_t));
  422. if (!_st_kq_data->fd_data) {
  423. err = errno;
  424. rv = -1;
  425. goto cleanup_kq;
  426. }
  427. /* Allocate event lists */
  428. _st_kq_data->evtlist_size = ST_KQ_MIN_EVTLIST_SIZE;
  429. _st_kq_data->evtlist = (struct kevent *)malloc(_st_kq_data->evtlist_size * sizeof(struct kevent));
  430. _st_kq_data->addlist_size = ST_KQ_MIN_EVTLIST_SIZE;
  431. _st_kq_data->addlist = (struct kevent *)malloc(_st_kq_data->addlist_size * sizeof(struct kevent));
  432. _st_kq_data->dellist_size = ST_KQ_MIN_EVTLIST_SIZE;
  433. _st_kq_data->dellist = (struct kevent *)malloc(_st_kq_data->dellist_size * sizeof(struct kevent));
  434. if (!_st_kq_data->evtlist || !_st_kq_data->addlist ||
  435. !_st_kq_data->dellist) {
  436. err = ENOMEM;
  437. rv = -1;
  438. }
  439. cleanup_kq:
  440. if (rv < 0) {
  441. if (_st_kq_data->kq >= 0)
  442. close(_st_kq_data->kq);
  443. free(_st_kq_data->fd_data);
  444. free(_st_kq_data->evtlist);
  445. free(_st_kq_data->addlist);
  446. free(_st_kq_data->dellist);
  447. free(_st_kq_data);
  448. _st_kq_data = NULL;
  449. errno = err;
  450. }
  451. return rv;
  452. }
  453. ST_HIDDEN int _st_kq_fd_data_expand(int maxfd)
  454. {
  455. _kq_fd_data_t *ptr;
  456. int n = _st_kq_data->fd_data_size;
  457. while (maxfd >= n)
  458. n <<= 1;
  459. ptr = (_kq_fd_data_t *)realloc(_st_kq_data->fd_data, n * sizeof(_kq_fd_data_t));
  460. if (!ptr)
  461. return -1;
  462. memset(ptr + _st_kq_data->fd_data_size, 0, (n - _st_kq_data->fd_data_size) * sizeof(_kq_fd_data_t));
  463. _st_kq_data->fd_data = ptr;
  464. _st_kq_data->fd_data_size = n;
  465. return 0;
  466. }
  467. ST_HIDDEN int _st_kq_addlist_expand(int avail)
  468. {
  469. struct kevent *ptr;
  470. int n = _st_kq_data->addlist_size;
  471. while (avail > n - _st_kq_data->addlist_cnt)
  472. n <<= 1;
  473. ptr = (struct kevent *)realloc(_st_kq_data->addlist, n * sizeof(struct kevent));
  474. if (!ptr)
  475. return -1;
  476. _st_kq_data->addlist = ptr;
  477. _st_kq_data->addlist_size = n;
  478. /*
  479. * Try to expand the result event list too
  480. * (although we don't have to do it).
  481. */
  482. ptr = (struct kevent *)realloc(_st_kq_data->evtlist, n * sizeof(struct kevent));
  483. if (ptr) {
  484. _st_kq_data->evtlist = ptr;
  485. _st_kq_data->evtlist_size = n;
  486. }
  487. return 0;
  488. }
  489. ST_HIDDEN void _st_kq_addlist_add(const struct kevent *kev)
  490. {
  491. ST_ASSERT(_st_kq_data->addlist_cnt < _st_kq_data->addlist_size);
  492. memcpy(_st_kq_data->addlist + _st_kq_data->addlist_cnt, kev, sizeof(struct kevent));
  493. _st_kq_data->addlist_cnt++;
  494. }
  495. ST_HIDDEN void _st_kq_dellist_add(const struct kevent *kev)
  496. {
  497. int n = _st_kq_data->dellist_size;
  498. if (_st_kq_data->dellist_cnt >= n) {
  499. struct kevent *ptr;
  500. n <<= 1;
  501. ptr = (struct kevent *)realloc(_st_kq_data->dellist, n * sizeof(struct kevent));
  502. if (!ptr) {
  503. /* See comment in _st_kq_pollset_del() */
  504. return;
  505. }
  506. _st_kq_data->dellist = ptr;
  507. _st_kq_data->dellist_size = n;
  508. }
  509. memcpy(_st_kq_data->dellist + _st_kq_data->dellist_cnt, kev, sizeof(struct kevent));
  510. _st_kq_data->dellist_cnt++;
  511. }
  512. ST_HIDDEN int _st_kq_pollset_add(struct pollfd *pds, int npds)
  513. {
  514. struct kevent kev;
  515. struct pollfd *pd;
  516. struct pollfd *epd = pds + npds;
  517. /*
  518. * Pollset adding is "atomic". That is, either it succeeded for
  519. * all descriptors in the set or it failed. It means that we
  520. * need to do all the checks up front so we don't have to
  521. * "unwind" if adding of one of the descriptors failed.
  522. */
  523. for (pd = pds; pd < epd; pd++) {
  524. /* POLLIN and/or POLLOUT must be set, but nothing else */
  525. if (pd->fd < 0 || !pd->events || (pd->events & ~(POLLIN | POLLOUT))) {
  526. errno = EINVAL;
  527. return -1;
  528. }
  529. if (pd->fd >= _st_kq_data->fd_data_size &&
  530. _st_kq_fd_data_expand(pd->fd) < 0)
  531. return -1;
  532. }
  533. /*
  534. * Make sure we have enough room in the addlist for twice as many
  535. * descriptors as in the pollset (for both READ and WRITE filters).
  536. */
  537. npds <<= 1;
  538. if (npds > _st_kq_data->addlist_size - _st_kq_data->addlist_cnt && _st_kq_addlist_expand(npds) < 0)
  539. return -1;
  540. for (pd = pds; pd < epd; pd++) {
  541. if ((pd->events & POLLIN) && (_ST_KQ_READ_CNT(pd->fd)++ == 0)) {
  542. memset(&kev, 0, sizeof(kev));
  543. kev.ident = pd->fd;
  544. kev.filter = EVFILT_READ;
  545. #ifdef NOTE_EOF
  546. /* Make it behave like select() and poll() */
  547. kev.fflags = NOTE_EOF;
  548. #endif
  549. kev.flags = (EV_ADD | EV_ONESHOT);
  550. _st_kq_addlist_add(&kev);
  551. }
  552. if ((pd->events & POLLOUT) && (_ST_KQ_WRITE_CNT(pd->fd)++ == 0)) {
  553. memset(&kev, 0, sizeof(kev));
  554. kev.ident = pd->fd;
  555. kev.filter = EVFILT_WRITE;
  556. kev.flags = (EV_ADD | EV_ONESHOT);
  557. _st_kq_addlist_add(&kev);
  558. }
  559. }
  560. return 0;
  561. }
  562. ST_HIDDEN void _st_kq_pollset_del(struct pollfd *pds, int npds)
  563. {
  564. struct kevent kev;
  565. struct pollfd *pd;
  566. struct pollfd *epd = pds + npds;
  567. /*
  568. * It's OK if deleting fails because a descriptor will either be
  569. * closed or fire only once (we set EV_ONESHOT flag).
  570. */
  571. _st_kq_data->dellist_cnt = 0;
  572. for (pd = pds; pd < epd; pd++) {
  573. if ((pd->events & POLLIN) && (--_ST_KQ_READ_CNT(pd->fd) == 0)) {
  574. memset(&kev, 0, sizeof(kev));
  575. kev.ident = pd->fd;
  576. kev.filter = EVFILT_READ;
  577. kev.flags = EV_DELETE;
  578. _st_kq_dellist_add(&kev);
  579. }
  580. if ((pd->events & POLLOUT) && (--_ST_KQ_WRITE_CNT(pd->fd) == 0)) {
  581. memset(&kev, 0, sizeof(kev));
  582. kev.ident = pd->fd;
  583. kev.filter = EVFILT_WRITE;
  584. kev.flags = EV_DELETE;
  585. _st_kq_dellist_add(&kev);
  586. }
  587. }
  588. if (_st_kq_data->dellist_cnt > 0) {
  589. /*
  590. * We do "synchronous" kqueue deletes to avoid deleting
  591. * closed descriptors and other possible problems.
  592. */
  593. int rv;
  594. do {
  595. /* This kevent() won't block since result list size is 0 */
  596. rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL);
  597. } while (rv < 0 && errno == EINTR);
  598. }
  599. }
  600. ST_HIDDEN void _st_kq_dispatch(void)
  601. {
  602. struct timespec timeout, *tsp;
  603. struct kevent kev;
  604. st_utime_t min_timeout;
  605. _st_clist_t *q;
  606. _st_pollq_t *pq;
  607. struct pollfd *pds, *epds;
  608. int nfd, i, osfd, notify, filter;
  609. short events, revents;
  610. if (_ST_SLEEPQ == NULL) {
  611. tsp = NULL;
  612. } else {
  613. min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
  614. timeout.tv_sec = (time_t) (min_timeout / 1000000);
  615. timeout.tv_nsec = (long) ((min_timeout % 1000000) * 1000);
  616. tsp = &timeout;
  617. }
  618. retry_kevent:
  619. /* Check for I/O operations */
  620. nfd = kevent(_st_kq_data->kq,
  621. _st_kq_data->addlist, _st_kq_data->addlist_cnt,
  622. _st_kq_data->evtlist, _st_kq_data->evtlist_size, tsp);
  623. _st_kq_data->addlist_cnt = 0;
  624. if (nfd > 0) {
  625. for (i = 0; i < nfd; i++) {
  626. osfd = _st_kq_data->evtlist[i].ident;
  627. filter = _st_kq_data->evtlist[i].filter;
  628. if (filter == EVFILT_READ) {
  629. _ST_KQ_REVENTS(osfd) |= POLLIN;
  630. } else if (filter == EVFILT_WRITE) {
  631. _ST_KQ_REVENTS(osfd) |= POLLOUT;
  632. }
  633. if (_st_kq_data->evtlist[i].flags & EV_ERROR) {
  634. if (_st_kq_data->evtlist[i].data == EBADF) {
  635. _ST_KQ_REVENTS(osfd) |= POLLNVAL;
  636. } else {
  637. _ST_KQ_REVENTS(osfd) |= POLLERR;
  638. }
  639. }
  640. }
  641. _st_kq_data->dellist_cnt = 0;
  642. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  643. pq = _ST_POLLQUEUE_PTR(q);
  644. notify = 0;
  645. epds = pq->pds + pq->npds;
  646. for (pds = pq->pds; pds < epds; pds++) {
  647. osfd = pds->fd;
  648. events = pds->events;
  649. revents = (short)(_ST_KQ_REVENTS(osfd) & ~(POLLIN | POLLOUT));
  650. if ((events & POLLIN) && (_ST_KQ_REVENTS(osfd) & POLLIN)) {
  651. revents |= POLLIN;
  652. }
  653. if ((events & POLLOUT) && (_ST_KQ_REVENTS(osfd) & POLLOUT)) {
  654. revents |= POLLOUT;
  655. }
  656. pds->revents = revents;
  657. if (revents) {
  658. notify = 1;
  659. }
  660. }
  661. if (notify) {
  662. ST_REMOVE_LINK(&pq->links);
  663. pq->on_ioq = 0;
  664. for (pds = pq->pds; pds < epds; pds++) {
  665. osfd = pds->fd;
  666. events = pds->events;
  667. /*
  668. * We set EV_ONESHOT flag so we only need to delete
  669. * descriptor if it didn't fire.
  670. */
  671. if ((events & POLLIN) && (--_ST_KQ_READ_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLIN) == 0)) {
  672. memset(&kev, 0, sizeof(kev));
  673. kev.ident = osfd;
  674. kev.filter = EVFILT_READ;
  675. kev.flags = EV_DELETE;
  676. _st_kq_dellist_add(&kev);
  677. }
  678. if ((events & POLLOUT) && (--_ST_KQ_WRITE_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLOUT) == 0)) {
  679. memset(&kev, 0, sizeof(kev));
  680. kev.ident = osfd;
  681. kev.filter = EVFILT_WRITE;
  682. kev.flags = EV_DELETE;
  683. _st_kq_dellist_add(&kev);
  684. }
  685. }
  686. if (pq->thread->flags & _ST_FL_ON_SLEEPQ)
  687. _ST_DEL_SLEEPQ(pq->thread);
  688. pq->thread->state = _ST_ST_RUNNABLE;
  689. _ST_ADD_RUNQ(pq->thread);
  690. }
  691. }
  692. if (_st_kq_data->dellist_cnt > 0) {
  693. int rv;
  694. do {
  695. /* This kevent() won't block since result list size is 0 */
  696. rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL);
  697. } while (rv < 0 && errno == EINTR);
  698. }
  699. for (i = 0; i < nfd; i++) {
  700. osfd = _st_kq_data->evtlist[i].ident;
  701. _ST_KQ_REVENTS(osfd) = 0;
  702. }
  703. } else if (nfd < 0) {
  704. if (errno == EBADF && _st_kq_data->pid != getpid()) {
  705. /* We probably forked, reinitialize kqueue */
  706. if ((_st_kq_data->kq = kqueue()) < 0) {
  707. /* There is nothing we can do here, will retry later */
  708. return;
  709. }
  710. fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC);
  711. _st_kq_data->pid = getpid();
  712. /* Re-register all descriptors on ioq with new kqueue */
  713. memset(_st_kq_data->fd_data, 0, _st_kq_data->fd_data_size * sizeof(_kq_fd_data_t));
  714. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  715. pq = _ST_POLLQUEUE_PTR(q);
  716. _st_kq_pollset_add(pq->pds, pq->npds);
  717. }
  718. goto retry_kevent;
  719. }
  720. }
  721. }
  722. ST_HIDDEN int _st_kq_fd_new(int osfd)
  723. {
  724. if (osfd >= _st_kq_data->fd_data_size && _st_kq_fd_data_expand(osfd) < 0)
  725. return -1;
  726. return 0;
  727. }
  728. ST_HIDDEN int _st_kq_fd_close(int osfd)
  729. {
  730. if (_ST_KQ_READ_CNT(osfd) || _ST_KQ_WRITE_CNT(osfd)) {
  731. errno = EBUSY;
  732. return -1;
  733. }
  734. return 0;
  735. }
  736. ST_HIDDEN int _st_kq_fd_getlimit(void)
  737. {
  738. /* zero means no specific limit */
  739. return 0;
  740. }
  741. ST_HIDDEN void _st_kq_destroy(void)
  742. {
  743. /* TODO: FIXME: Implements it */
  744. }
  745. static _st_eventsys_t _st_kq_eventsys = {
  746. "kqueue",
  747. ST_EVENTSYS_ALT,
  748. _st_kq_init,
  749. _st_kq_dispatch,
  750. _st_kq_pollset_add,
  751. _st_kq_pollset_del,
  752. _st_kq_fd_new,
  753. _st_kq_fd_close,
  754. _st_kq_fd_getlimit,
  755. _st_kq_destroy
  756. };
  757. #endif /* MD_HAVE_KQUEUE */
  758. #ifdef MD_HAVE_EPOLL
  759. /*****************************************
  760. * epoll event system
  761. */
  762. ST_HIDDEN int _st_epoll_init(void)
  763. {
  764. int fdlim;
  765. int err = 0;
  766. int rv = 0;
  767. _st_epoll_data = (struct _st_epolldata *) calloc(1, sizeof(*_st_epoll_data));
  768. if (!_st_epoll_data)
  769. return -1;
  770. fdlim = st_getfdlimit();
  771. _st_epoll_data->fd_hint = (fdlim > 0 && fdlim < ST_EPOLL_EVTLIST_SIZE) ? fdlim : ST_EPOLL_EVTLIST_SIZE;
  772. if ((_st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint)) < 0) {
  773. err = errno;
  774. rv = -1;
  775. goto cleanup_epoll;
  776. }
  777. fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC);
  778. /* Allocate file descriptor data array */
  779. _st_epoll_data->fd_data_size = _st_epoll_data->fd_hint;
  780. _st_epoll_data->fd_data = (_epoll_fd_data_t *)calloc(_st_epoll_data->fd_data_size, sizeof(_epoll_fd_data_t));
  781. if (!_st_epoll_data->fd_data) {
  782. err = errno;
  783. rv = -1;
  784. goto cleanup_epoll;
  785. }
  786. /* Allocate event lists */
  787. _st_epoll_data->evtlist_size = _st_epoll_data->fd_hint;
  788. _st_epoll_data->evtlist = (struct epoll_event *)malloc(_st_epoll_data->evtlist_size * sizeof(struct epoll_event));
  789. if (!_st_epoll_data->evtlist) {
  790. err = errno;
  791. rv = -1;
  792. }
  793. cleanup_epoll:
  794. if (rv < 0) {
  795. if (_st_epoll_data->epfd >= 0)
  796. close(_st_epoll_data->epfd);
  797. free(_st_epoll_data->fd_data);
  798. free(_st_epoll_data->evtlist);
  799. free(_st_epoll_data);
  800. _st_epoll_data = NULL;
  801. errno = err;
  802. }
  803. return rv;
  804. }
  805. ST_HIDDEN int _st_epoll_fd_data_expand(int maxfd)
  806. {
  807. _epoll_fd_data_t *ptr;
  808. int n = _st_epoll_data->fd_data_size;
  809. while (maxfd >= n)
  810. n <<= 1;
  811. ptr = (_epoll_fd_data_t *)realloc(_st_epoll_data->fd_data, n * sizeof(_epoll_fd_data_t));
  812. if (!ptr)
  813. return -1;
  814. memset(ptr + _st_epoll_data->fd_data_size, 0, (n - _st_epoll_data->fd_data_size) * sizeof(_epoll_fd_data_t));
  815. _st_epoll_data->fd_data = ptr;
  816. _st_epoll_data->fd_data_size = n;
  817. return 0;
  818. }
  819. ST_HIDDEN void _st_epoll_evtlist_expand(void)
  820. {
  821. struct epoll_event *ptr;
  822. int n = _st_epoll_data->evtlist_size;
  823. while (_st_epoll_data->evtlist_cnt > n)
  824. n <<= 1;
  825. ptr = (struct epoll_event *)realloc(_st_epoll_data->evtlist, n * sizeof(struct epoll_event));
  826. if (ptr) {
  827. _st_epoll_data->evtlist = ptr;
  828. _st_epoll_data->evtlist_size = n;
  829. }
  830. }
  831. ST_HIDDEN void _st_epoll_pollset_del(struct pollfd *pds, int npds)
  832. {
  833. struct epoll_event ev;
  834. struct pollfd *pd;
  835. struct pollfd *epd = pds + npds;
  836. int old_events, events, op;
  837. /*
  838. * It's more or less OK if deleting fails because a descriptor
  839. * will either be closed or deleted in dispatch function after
  840. * it fires.
  841. */
  842. for (pd = pds; pd < epd; pd++) {
  843. old_events = _ST_EPOLL_EVENTS(pd->fd);
  844. if (pd->events & POLLIN)
  845. _ST_EPOLL_READ_CNT(pd->fd)--;
  846. if (pd->events & POLLOUT)
  847. _ST_EPOLL_WRITE_CNT(pd->fd)--;
  848. if (pd->events & POLLPRI)
  849. _ST_EPOLL_EXCEP_CNT(pd->fd)--;
  850. events = _ST_EPOLL_EVENTS(pd->fd);
  851. /*
  852. * The _ST_EPOLL_REVENTS check below is needed so we can use
  853. * this function inside dispatch(). Outside of dispatch()
  854. * _ST_EPOLL_REVENTS is always zero for all descriptors.
  855. */
  856. if (events != old_events && _ST_EPOLL_REVENTS(pd->fd) == 0) {
  857. op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
  858. ev.events = events;
  859. ev.data.fd = pd->fd;
  860. if (epoll_ctl(_st_epoll_data->epfd, op, pd->fd, &ev) == 0 && op == EPOLL_CTL_DEL) {
  861. _st_epoll_data->evtlist_cnt--;
  862. }
  863. }
  864. }
  865. }
  866. ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds)
  867. {
  868. struct epoll_event ev;
  869. int i, fd;
  870. int old_events, events, op;
  871. /* Do as many checks as possible up front */
  872. for (i = 0; i < npds; i++) {
  873. fd = pds[i].fd;
  874. if (fd < 0 || !pds[i].events ||
  875. (pds[i].events & ~(POLLIN | POLLOUT | POLLPRI))) {
  876. errno = EINVAL;
  877. return -1;
  878. }
  879. if (fd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(fd) < 0)
  880. return -1;
  881. }
  882. for (i = 0; i < npds; i++) {
  883. fd = pds[i].fd;
  884. old_events = _ST_EPOLL_EVENTS(fd);
  885. if (pds[i].events & POLLIN)
  886. _ST_EPOLL_READ_CNT(fd)++;
  887. if (pds[i].events & POLLOUT)
  888. _ST_EPOLL_WRITE_CNT(fd)++;
  889. if (pds[i].events & POLLPRI)
  890. _ST_EPOLL_EXCEP_CNT(fd)++;
  891. events = _ST_EPOLL_EVENTS(fd);
  892. if (events != old_events) {
  893. op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
  894. ev.events = events;
  895. ev.data.fd = fd;
  896. if (epoll_ctl(_st_epoll_data->epfd, op, fd, &ev) < 0 && (op != EPOLL_CTL_ADD || errno != EEXIST))
  897. break;
  898. if (op == EPOLL_CTL_ADD) {
  899. _st_epoll_data->evtlist_cnt++;
  900. if (_st_epoll_data->evtlist_cnt > _st_epoll_data->evtlist_size)
  901. _st_epoll_evtlist_expand();
  902. }
  903. }
  904. }
  905. if (i < npds) {
  906. /* Error */
  907. int err = errno;
  908. /* Unroll the state */
  909. _st_epoll_pollset_del(pds, i + 1);
  910. errno = err;
  911. return -1;
  912. }
  913. return 0;
  914. }
  915. ST_HIDDEN void _st_epoll_dispatch(void)
  916. {
  917. st_utime_t min_timeout;
  918. _st_clist_t *q;
  919. _st_pollq_t *pq;
  920. struct pollfd *pds, *epds;
  921. struct epoll_event ev;
  922. int timeout, nfd, i, osfd, notify;
  923. int events, op;
  924. short revents;
  925. #if defined(DEBUG) && defined(DEBUG_STATS)
  926. ++_st_stat_epoll;
  927. #endif
  928. if (_ST_SLEEPQ == NULL) {
  929. timeout = -1;
  930. } else {
  931. min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
  932. timeout = (int) (min_timeout / 1000);
  933. // At least wait 1ms when <1ms, to avoid epoll_wait spin loop.
  934. if (timeout == 0) {
  935. #if defined(DEBUG) && defined(DEBUG_STATS)
  936. ++_st_stat_epoll_zero;
  937. #endif
  938. if (min_timeout > 0) {
  939. #if defined(DEBUG) && defined(DEBUG_STATS)
  940. ++_st_stat_epoll_shake;
  941. #endif
  942. timeout = 1;
  943. }
  944. }
  945. }
  946. /* Check for I/O operations */
  947. nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout);
  948. #if defined(DEBUG) && defined(DEBUG_STATS)
  949. if (nfd <= 0) {
  950. ++_st_stat_epoll_spin;
  951. }
  952. #endif
  953. if (nfd > 0) {
  954. for (i = 0; i < nfd; i++) {
  955. osfd = _st_epoll_data->evtlist[i].data.fd;
  956. _ST_EPOLL_REVENTS(osfd) = _st_epoll_data->evtlist[i].events;
  957. if (_ST_EPOLL_REVENTS(osfd) & (EPOLLERR | EPOLLHUP)) {
  958. /* Also set I/O bits on error */
  959. _ST_EPOLL_REVENTS(osfd) |= _ST_EPOLL_EVENTS(osfd);
  960. }
  961. }
  962. for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
  963. pq = _ST_POLLQUEUE_PTR(q);
  964. notify = 0;
  965. epds = pq->pds + pq->npds;
  966. for (pds = pq->pds; pds < epds; pds++) {
  967. if (_ST_EPOLL_REVENTS(pds->fd) == 0) {
  968. pds->revents = 0;
  969. continue;
  970. }
  971. osfd = pds->fd;
  972. events = pds->events;
  973. revents = 0;
  974. if ((events & POLLIN) && (_ST_EPOLL_REVENTS(osfd) & EPOLLIN))
  975. revents |= POLLIN;
  976. if ((events & POLLOUT) && (_ST_EPOLL_REVENTS(osfd) & EPOLLOUT))
  977. revents |= POLLOUT;
  978. if ((events & POLLPRI) && (_ST_EPOLL_REVENTS(osfd) & EPOLLPRI))
  979. revents |= POLLPRI;
  980. if (_ST_EPOLL_REVENTS(osfd) & EPOLLERR)
  981. revents |= POLLERR;
  982. if (_ST_EPOLL_REVENTS(osfd) & EPOLLHUP)
  983. revents |= POLLHUP;
  984. pds->revents = revents;
  985. if (revents) {
  986. notify = 1;
  987. }
  988. }
  989. if (notify) {
  990. ST_REMOVE_LINK(&pq->links);
  991. pq->on_ioq = 0;
  992. /*
  993. * Here we will only delete/modify descriptors that
  994. * didn't fire (see comments in _st_epoll_pollset_del()).
  995. */
  996. _st_epoll_pollset_del(pq->pds, pq->npds);
  997. if (pq->thread->flags & _ST_FL_ON_SLEEPQ)
  998. _ST_DEL_SLEEPQ(pq->thread);
  999. pq->thread->state = _ST_ST_RUNNABLE;
  1000. _ST_ADD_RUNQ(pq->thread);
  1001. }
  1002. }
  1003. for (i = 0; i < nfd; i++) {
  1004. /* Delete/modify descriptors that fired */
  1005. osfd = _st_epoll_data->evtlist[i].data.fd;
  1006. _ST_EPOLL_REVENTS(osfd) = 0;
  1007. events = _ST_EPOLL_EVENTS(osfd);
  1008. op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
  1009. ev.events = events;
  1010. ev.data.fd = osfd;
  1011. if (epoll_ctl(_st_epoll_data->epfd, op, osfd, &ev) == 0 && op == EPOLL_CTL_DEL) {
  1012. _st_epoll_data->evtlist_cnt--;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. ST_HIDDEN int _st_epoll_fd_new(int osfd)
  1018. {
  1019. if (osfd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(osfd) < 0)
  1020. return -1;
  1021. return 0;
  1022. }
  1023. ST_HIDDEN int _st_epoll_fd_close(int osfd)
  1024. {
  1025. if (_ST_EPOLL_READ_CNT(osfd) || _ST_EPOLL_WRITE_CNT(osfd) || _ST_EPOLL_EXCEP_CNT(osfd)) {
  1026. errno = EBUSY;
  1027. return -1;
  1028. }
  1029. return 0;
  1030. }
  1031. ST_HIDDEN int _st_epoll_fd_getlimit(void)
  1032. {
  1033. /* zero means no specific limit */
  1034. return 0;
  1035. }
  1036. /*
  1037. * Check if epoll functions are just stubs.
  1038. */
  1039. ST_HIDDEN int _st_epoll_is_supported(void)
  1040. {
  1041. struct epoll_event ev;
  1042. ev.events = EPOLLIN;
  1043. ev.data.ptr = NULL;
  1044. /* Guaranteed to fail */
  1045. epoll_ctl(-1, EPOLL_CTL_ADD, -1, &ev);
  1046. return (errno != ENOSYS);
  1047. }
  1048. ST_HIDDEN void _st_epoll_destroy(void)
  1049. {
  1050. if (_st_epoll_data->epfd >= 0) {
  1051. close(_st_epoll_data->epfd);
  1052. }
  1053. free(_st_epoll_data->fd_data);
  1054. free(_st_epoll_data->evtlist);
  1055. free(_st_epoll_data);
  1056. _st_epoll_data = NULL;
  1057. }
  1058. static _st_eventsys_t _st_epoll_eventsys = {
  1059. "epoll",
  1060. ST_EVENTSYS_ALT,
  1061. _st_epoll_init,
  1062. _st_epoll_dispatch,
  1063. _st_epoll_pollset_add,
  1064. _st_epoll_pollset_del,
  1065. _st_epoll_fd_new,
  1066. _st_epoll_fd_close,
  1067. _st_epoll_fd_getlimit,
  1068. _st_epoll_destroy
  1069. };
  1070. #endif /* MD_HAVE_EPOLL */
  1071. /*****************************************
  1072. * Public functions
  1073. */
  1074. int st_set_eventsys(int eventsys)
  1075. {
  1076. if (_st_eventsys) {
  1077. errno = EBUSY;
  1078. return -1;
  1079. }
  1080. if (eventsys == ST_EVENTSYS_SELECT || eventsys == ST_EVENTSYS_DEFAULT) {
  1081. #if defined (MD_HAVE_SELECT)
  1082. _st_eventsys = &_st_select_eventsys;
  1083. return 0;
  1084. #endif
  1085. }
  1086. if (eventsys == ST_EVENTSYS_ALT) {
  1087. #if defined (MD_HAVE_KQUEUE)
  1088. _st_eventsys = &_st_kq_eventsys;
  1089. return 0;
  1090. #elif defined (MD_HAVE_EPOLL)
  1091. if (_st_epoll_is_supported()) {
  1092. _st_eventsys = &_st_epoll_eventsys;
  1093. return 0;
  1094. }
  1095. #endif
  1096. }
  1097. errno = EINVAL;
  1098. return -1;
  1099. }
  1100. int st_get_eventsys(void)
  1101. {
  1102. return _st_eventsys ? _st_eventsys->val : -1;
  1103. }
  1104. const char *st_get_eventsys_name(void)
  1105. {
  1106. return _st_eventsys ? _st_eventsys->name : "";
  1107. }