BReactor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /**
  2. * @file BReactor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stddef.h>
  26. #ifdef BADVPN_USE_WINAPI
  27. #include <windows.h>
  28. #else
  29. #include <limits.h>
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #endif
  34. #include <misc/overflow.h>
  35. #include <misc/debug.h>
  36. #include <misc/offset.h>
  37. #include <system/BLog.h>
  38. #include <system/BReactor.h>
  39. #include <generated/blog_channel_BReactor.h>
  40. #ifdef BADVPN_USE_WINAPI
  41. typedef DWORD btimeout_t;
  42. #define BTIMEOUT_T_MAX ((DWORD)INFINITE - 1)
  43. #define WAITRES_TIMED_OUT(_res) ((_res) == WAIT_TIMEOUT)
  44. #else
  45. typedef int btimeout_t;
  46. #define BTIMEOUT_T_MAX INT_MAX
  47. #define WAITRES_TIMED_OUT(_res) ((_res) == 0)
  48. #endif
  49. static void dispatch_jobs (BReactor *bsys)
  50. {
  51. while (!bsys->exiting && BPendingGroup_HasJobs(&bsys->pending_jobs)) {
  52. BPendingGroup_ExecuteJob(&bsys->pending_jobs);
  53. }
  54. }
  55. static int timer_comparator (void *user, btime_t *val1, btime_t *val2)
  56. {
  57. if (*val1 < *val2) {
  58. return -1;
  59. }
  60. if (*val1 > *val2) {
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. static int move_expired_timers (BReactor *bsys, btime_t now)
  66. {
  67. int moved = 0;
  68. // move timed out timers to the expired list
  69. BHeapNode *heap_node;
  70. while (heap_node = BHeap_GetFirst(&bsys->timers_heap)) {
  71. BTimer *timer = UPPER_OBJECT(heap_node, BTimer, heap_node);
  72. ASSERT(timer->active)
  73. // if it's in the future, stop
  74. if (timer->absTime > now) {
  75. break;
  76. }
  77. moved = 1;
  78. // remove from running timers heap
  79. BHeap_Remove(&bsys->timers_heap, &timer->heap_node);
  80. // add to expired timers list
  81. LinkedList2_Append(&bsys->timers_expired_list, &timer->list_node);
  82. // set expired
  83. timer->expired = 1;
  84. }
  85. return moved;
  86. }
  87. static void move_first_timers (BReactor *bsys)
  88. {
  89. // get the time of the first timer
  90. BHeapNode *heap_node = BHeap_GetFirst(&bsys->timers_heap);
  91. ASSERT(heap_node)
  92. BTimer *first_timer = UPPER_OBJECT(heap_node, BTimer, heap_node);
  93. ASSERT(first_timer->active)
  94. btime_t first_time = first_timer->absTime;
  95. // remove from running timers heap
  96. BHeap_Remove(&bsys->timers_heap, &first_timer->heap_node);
  97. // add to expired timers list
  98. LinkedList2_Append(&bsys->timers_expired_list, &first_timer->list_node);
  99. // set expired
  100. first_timer->expired = 1;
  101. // also move other timers with the same timeout
  102. while (heap_node = BHeap_GetFirst(&bsys->timers_heap)) {
  103. BTimer *timer = UPPER_OBJECT(heap_node, BTimer, heap_node);
  104. ASSERT(timer->active)
  105. ASSERT(timer->absTime >= first_time)
  106. // if it's in the future, stop
  107. if (timer->absTime > first_time) {
  108. break;
  109. }
  110. // remove from running timers heap
  111. BHeap_Remove(&bsys->timers_heap, &timer->heap_node);
  112. // add to expired timers list
  113. LinkedList2_Append(&bsys->timers_expired_list, &timer->list_node);
  114. // set expired
  115. timer->expired = 1;
  116. }
  117. }
  118. static void dispatch_timers (BReactor *bsys)
  119. {
  120. // call event hendlers for expired timers
  121. // Handler functions are free to remove any timer, and if a pending
  122. // expired timer is removed, it will not be reported.
  123. LinkedList2Node *list_node;
  124. while (!bsys->exiting && (list_node = LinkedList2_GetFirst(&bsys->timers_expired_list))) {
  125. BTimer *timer = UPPER_OBJECT(list_node, BTimer, list_node);
  126. ASSERT(timer->active)
  127. ASSERT(timer->expired)
  128. // remove from expired list
  129. LinkedList2_Remove(&bsys->timers_expired_list, &timer->list_node);
  130. // set inactive
  131. timer->active = 0;
  132. // call handler
  133. BLog(BLOG_DEBUG, "Dispatching timer");
  134. timer->handler(timer->handler_pointer);
  135. // dispatch jobs
  136. dispatch_jobs(bsys);
  137. }
  138. }
  139. #ifdef BADVPN_USE_WINAPI
  140. static void dispatch_io (BReactor *bsys)
  141. {
  142. if (!bsys->exiting && bsys->returned_object) {
  143. BHandle *bh = bsys->returned_object;
  144. bsys->returned_object = NULL;
  145. ASSERT(bh->active)
  146. ASSERT(bh->position >= 0 && bh->position < bsys->enabled_num)
  147. ASSERT(bh == bsys->enabled_objects[bh->position])
  148. ASSERT(bh->h == bsys->enabled_handles[bh->position])
  149. // call handler
  150. BLog(BLOG_DEBUG, "Dispatching handle");
  151. bh->handler(bh->user);
  152. // dispatch jobs
  153. dispatch_jobs(bsys);
  154. }
  155. }
  156. #else
  157. static void set_fd_pointers (BReactor *bsys)
  158. {
  159. // Write pointers to our entry pointers into file descriptors.
  160. // If a handler function frees some other file descriptor, the
  161. // free routine will set our pointer to NULL so we don't dispatch it.
  162. for (int i = 0; i < bsys->epoll_results_num; i++) {
  163. struct epoll_event *event = &bsys->epoll_results[i];
  164. ASSERT(event->data.ptr)
  165. BFileDescriptor *bfd = (BFileDescriptor *)event->data.ptr;
  166. ASSERT(bfd->active)
  167. ASSERT(!bfd->epoll_returned_ptr)
  168. bfd->epoll_returned_ptr = (BFileDescriptor **)&event->data.ptr;
  169. }
  170. }
  171. static void dispatch_io (BReactor *bsys)
  172. {
  173. while (!bsys->exiting && bsys->epoll_results_pos < bsys->epoll_results_num) {
  174. // grab event
  175. struct epoll_event *event = &bsys->epoll_results[bsys->epoll_results_pos];
  176. bsys->epoll_results_pos++;
  177. // check if the BFileDescriptor was removed
  178. if (!event->data.ptr) {
  179. continue;
  180. }
  181. // get BFileDescriptor
  182. BFileDescriptor *bfd = (BFileDescriptor *)event->data.ptr;
  183. ASSERT(bfd->active)
  184. ASSERT(bfd->epoll_returned_ptr == (BFileDescriptor **)&event->data.ptr)
  185. // zero pointer to the epoll entry
  186. bfd->epoll_returned_ptr = NULL;
  187. // calculate events to report
  188. int events = 0;
  189. if (bfd->waitEvents&BREACTOR_READ) {
  190. if ((event->events&EPOLLIN) || (event->events&EPOLLERR) || (event->events&EPOLLHUP)) {
  191. events |= BREACTOR_READ;
  192. }
  193. }
  194. if (bfd->waitEvents&BREACTOR_WRITE) {
  195. if ((event->events&EPOLLOUT) || (event->events&EPOLLERR) || (event->events&EPOLLHUP)) {
  196. events |= BREACTOR_WRITE;
  197. }
  198. }
  199. // call handler
  200. BLog(BLOG_DEBUG, "Dispatching file descriptor");
  201. bfd->handler(bfd->user, events);
  202. // dispatch jobs
  203. dispatch_jobs(bsys);
  204. }
  205. }
  206. #endif
  207. static void wait_for_events (BReactor *bsys)
  208. {
  209. // must have processed all pending events
  210. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
  211. ASSERT(LinkedList2_IsEmpty(&bsys->timers_expired_list))
  212. #ifdef BADVPN_USE_WINAPI
  213. ASSERT(!bsys->returned_object)
  214. #else
  215. ASSERT(bsys->epoll_results_pos == bsys->epoll_results_num)
  216. #endif
  217. // clean up epoll results
  218. #ifndef BADVPN_USE_WINAPI
  219. bsys->epoll_results_num = 0;
  220. bsys->epoll_results_pos = 0;
  221. #endif
  222. // timeout vars
  223. int have_timeout = 0;
  224. btime_t timeout_abs;
  225. btime_t now;
  226. // compute timeout
  227. BHeapNode *first_node;
  228. if (first_node = BHeap_GetFirst(&bsys->timers_heap)) {
  229. // get current time
  230. now = btime_gettime();
  231. // if some timers have already timed out, return them immediately
  232. if (move_expired_timers(bsys, now)) {
  233. BLog(BLOG_DEBUG, "Got already expired timers");
  234. return;
  235. }
  236. // timeout is first timer, remember absolute time
  237. BTimer *first_timer = UPPER_OBJECT(first_node, BTimer, heap_node);
  238. have_timeout = 1;
  239. timeout_abs = first_timer->absTime;
  240. }
  241. int timed_out;
  242. #ifdef BADVPN_USE_WINAPI
  243. int handle_index;
  244. #else
  245. int epoll_num_results;
  246. #endif
  247. // wait until the timeout is reached or the file descriptor / handle in ready
  248. while (1) {
  249. // compute timeout
  250. btimeout_t timeout_arg;
  251. btime_t timeout_rel;
  252. if (have_timeout) {
  253. timeout_rel = timeout_abs - now;
  254. if (timeout_rel > BTIMEOUT_T_MAX) {
  255. timeout_arg = BTIMEOUT_T_MAX;
  256. } else {
  257. timeout_arg = timeout_rel;
  258. }
  259. }
  260. // perform wait
  261. #ifdef BADVPN_USE_WINAPI
  262. BLog(BLOG_DEBUG, "Calling WaitForMultipleObjects on %d handles", bsys->enabled_num);
  263. DWORD waitres = WaitForMultipleObjects(bsys->enabled_num, bsys->enabled_handles, FALSE, (have_timeout ? timeout_arg : INFINITE));
  264. ASSERT_FORCE(waitres != WAIT_FAILED)
  265. ASSERT_FORCE(!(waitres == WAIT_TIMEOUT) || have_timeout)
  266. ASSERT_FORCE(!(waitres != WAIT_TIMEOUT) || (waitres >= WAIT_OBJECT_0 && waitres < WAIT_OBJECT_0 + bsys->enabled_num))
  267. #else
  268. BLog(BLOG_DEBUG, "Calling epoll_wait");
  269. int waitres = epoll_wait(bsys->efd, bsys->epoll_results, BSYSTEM_MAX_RESULTS, (have_timeout ? timeout_arg : -1));
  270. if (waitres < 0) {
  271. int error = errno;
  272. if (error == EINTR) {
  273. BLog(BLOG_DEBUG, "epoll_wait interrupted");
  274. goto try_again;
  275. }
  276. perror("epoll_wait");
  277. ASSERT_FORCE(0)
  278. }
  279. ASSERT_FORCE(!(waitres == 0) || have_timeout)
  280. ASSERT_FORCE(waitres <= BSYSTEM_MAX_RESULTS)
  281. #endif
  282. if (!WAITRES_TIMED_OUT(waitres) || timeout_rel <= BTIMEOUT_T_MAX) {
  283. timed_out = WAITRES_TIMED_OUT(waitres);
  284. if (!timed_out) {
  285. #ifdef BADVPN_USE_WINAPI
  286. handle_index = waitres - WAIT_OBJECT_0;
  287. #else
  288. epoll_num_results = waitres;
  289. #endif
  290. }
  291. break;
  292. }
  293. try_again:
  294. if (have_timeout) {
  295. // get current time
  296. now = btime_gettime();
  297. // check if we already reached the time we're waiting for
  298. if (now >= timeout_abs) {
  299. timed_out = 1;
  300. break;
  301. }
  302. }
  303. }
  304. if (timed_out) {
  305. // timed out, expire first timers
  306. BLog(BLOG_DEBUG, "Wait timed out");
  307. move_first_timers(bsys);
  308. } else {
  309. #ifdef BADVPN_USE_WINAPI
  310. // user's handle got signalled
  311. BLog(BLOG_DEBUG, "Wait returned handle %d", handle_index);
  312. bsys->returned_object = bsys->enabled_objects[handle_index];
  313. #else
  314. // setup returned file descriptors list
  315. BLog(BLOG_DEBUG, "Wait returned %d file descriptors", epoll_num_results);
  316. bsys->epoll_results_num = epoll_num_results;
  317. set_fd_pointers(bsys);
  318. #endif
  319. }
  320. }
  321. #ifdef BADVPN_USE_WINAPI
  322. void BHandle_Init (BHandle *bh, HANDLE handle, BHandle_handler handler, void *user)
  323. {
  324. bh->h = handle;
  325. bh->handler = handler;
  326. bh->user = user;
  327. bh->active = 0;
  328. }
  329. #else
  330. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user)
  331. {
  332. bs->fd = fd;
  333. bs->handler = handler;
  334. bs->user = user;
  335. bs->active = 0;
  336. }
  337. #endif
  338. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *handler_pointer)
  339. {
  340. bt->msTime = msTime;
  341. bt->handler = handler;
  342. bt->handler_pointer = handler_pointer;
  343. bt->active = 0;
  344. }
  345. int BTimer_IsRunning (BTimer *bt)
  346. {
  347. ASSERT(bt->active == 0 || bt->active == 1)
  348. return bt->active;
  349. }
  350. int BReactor_Init (BReactor *bsys)
  351. {
  352. BLog(BLOG_DEBUG, "Reactor initializing");
  353. bsys->exiting = 0;
  354. // init jobs
  355. BPendingGroup_Init(&bsys->pending_jobs);
  356. // init timers
  357. BHeap_Init(&bsys->timers_heap, OFFSET_DIFF(BTimer, absTime, heap_node), (BHeap_comparator)timer_comparator, NULL);
  358. LinkedList2_Init(&bsys->timers_expired_list);
  359. #ifdef BADVPN_USE_WINAPI
  360. bsys->num_handles = 0;
  361. bsys->enabled_num = 0;
  362. bsys->returned_object = NULL;
  363. #else
  364. // create epoll fd
  365. if ((bsys->efd = epoll_create(10)) < 0) {
  366. BLog(BLOG_ERROR, "epoll_create failed");
  367. goto fail0;
  368. }
  369. // init results array
  370. bsys->epoll_results_num = 0;
  371. bsys->epoll_results_pos = 0;
  372. DebugCounter_Init(&bsys->d_fds_counter);
  373. #endif
  374. // init debug object
  375. DebugObject_Init(&bsys->d_obj);
  376. return 1;
  377. fail0:
  378. BPendingGroup_Free(&bsys->pending_jobs);
  379. BLog(BLOG_ERROR, "Reactor failed to initialize");
  380. return 0;
  381. }
  382. void BReactor_Free (BReactor *bsys)
  383. {
  384. // {pending group has no BPending objects}
  385. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
  386. ASSERT(!BHeap_GetFirst(&bsys->timers_heap))
  387. ASSERT(LinkedList2_IsEmpty(&bsys->timers_expired_list))
  388. DebugObject_Free(&bsys->d_obj);
  389. #ifdef BADVPN_USE_WINAPI
  390. ASSERT(bsys->num_handles == 0)
  391. #else
  392. DebugCounter_Free(&bsys->d_fds_counter);
  393. #endif
  394. BLog(BLOG_DEBUG, "Reactor freeing");
  395. #ifndef BADVPN_USE_WINAPI
  396. // close epoll fd
  397. ASSERT_FORCE(close(bsys->efd) == 0)
  398. #endif
  399. // free jobs
  400. BPendingGroup_Free(&bsys->pending_jobs);
  401. }
  402. int BReactor_Exec (BReactor *bsys)
  403. {
  404. BLog(BLOG_DEBUG, "Entering event loop");
  405. bsys->exiting = 0;
  406. while (1) {
  407. dispatch_jobs(bsys);
  408. dispatch_timers(bsys);
  409. dispatch_io(bsys);
  410. if (bsys->exiting) {
  411. break;
  412. }
  413. wait_for_events(bsys);
  414. }
  415. BLog(BLOG_DEBUG, "Exiting event loop, exit code %d", bsys->exit_code);
  416. return bsys->exit_code;
  417. }
  418. void BReactor_Quit (BReactor *bsys, int code)
  419. {
  420. bsys->exiting = 1;
  421. bsys->exit_code = code;
  422. }
  423. void BReactor_SetTimer (BReactor *bsys, BTimer *bt)
  424. {
  425. btime_t now = btime_gettime();
  426. // handle overflow
  427. int overflows = add_int64_overflows(now, bt->msTime);
  428. btime_t absTime;
  429. if (overflows != 0) {
  430. if (overflows > 0) {
  431. absTime = INT64_MAX;
  432. } else {
  433. absTime = INT64_MIN;
  434. }
  435. } else {
  436. absTime = now + bt->msTime;
  437. }
  438. BReactor_SetTimerAbsolute(bsys, bt, absTime);
  439. }
  440. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time)
  441. {
  442. // unlink it if it's already in the list
  443. BReactor_RemoveTimer(bsys, bt);
  444. // initialize timer
  445. bt->active = 1;
  446. bt->expired = 0;
  447. bt->absTime = time;
  448. // insert to running timers heap
  449. BHeap_Insert(&bsys->timers_heap, &bt->heap_node);
  450. }
  451. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
  452. {
  453. if (!bt->active) {
  454. return;
  455. }
  456. if (bt->expired) {
  457. // remove from expired list
  458. LinkedList2_Remove(&bsys->timers_expired_list, &bt->list_node);
  459. } else {
  460. // remove from running heap
  461. BHeap_Remove(&bsys->timers_heap, &bt->heap_node);
  462. }
  463. // set inactive
  464. bt->active = 0;
  465. }
  466. BPendingGroup * BReactor_PendingGroup (BReactor *bsys)
  467. {
  468. return &bsys->pending_jobs;
  469. }
  470. #ifdef BADVPN_USE_WINAPI
  471. int BReactor_AddHandle (BReactor *bsys, BHandle *bh)
  472. {
  473. ASSERT(!bh->active)
  474. if (bsys->num_handles >= BSYSTEM_MAX_HANDLES) {
  475. return 0;
  476. }
  477. bh->active = 1;
  478. bh->position = -1;
  479. bsys->num_handles++;
  480. return 1;
  481. }
  482. void BReactor_RemoveHandle (BReactor *bsys, BHandle *bh)
  483. {
  484. ASSERT(bh->active)
  485. if (bh->position >= 0) {
  486. BReactor_DisableHandle(bsys, bh);
  487. }
  488. bh->active = 0;
  489. ASSERT(bsys->num_handles > 0)
  490. bsys->num_handles--;
  491. }
  492. void BReactor_EnableHandle (BReactor *bsys, BHandle *bh)
  493. {
  494. ASSERT(bh->active)
  495. ASSERT(bh->position == -1)
  496. ASSERT(bsys->enabled_num < BSYSTEM_MAX_HANDLES)
  497. bsys->enabled_handles[bsys->enabled_num] = bh->h;
  498. bsys->enabled_objects[bsys->enabled_num] = bh;
  499. bh->position = bsys->enabled_num;
  500. bsys->enabled_num++;
  501. }
  502. void BReactor_DisableHandle (BReactor *bsys, BHandle *bh)
  503. {
  504. ASSERT(bh->active)
  505. ASSERT(bh->position >= 0)
  506. ASSERT(bh->position < bsys->enabled_num)
  507. ASSERT(bh == bsys->enabled_objects[bh->position])
  508. ASSERT(bh->h == bsys->enabled_handles[bh->position])
  509. // if there are more handles after this one, move the last
  510. // one into its position
  511. if (bh->position < bsys->enabled_num - 1) {
  512. int move_position = bsys->enabled_num - 1;
  513. BHandle *move_handle = bsys->enabled_objects[move_position];
  514. ASSERT(move_handle->active)
  515. ASSERT(move_handle->position == move_position)
  516. ASSERT(move_handle->h == bsys->enabled_handles[move_position])
  517. bsys->enabled_handles[bh->position] = move_handle->h;
  518. bsys->enabled_objects[bh->position] = move_handle;
  519. move_handle->position = bh->position;
  520. }
  521. bh->position = -1;
  522. bsys->enabled_num--;
  523. // make sure the handler will not be called
  524. if (bsys->returned_object == bh) {
  525. bsys->returned_object = NULL;
  526. }
  527. }
  528. #else
  529. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  530. {
  531. ASSERT(!bs->active)
  532. // add epoll entry
  533. struct epoll_event event;
  534. memset(&event, 0, sizeof(event));
  535. event.events = 0;
  536. event.data.ptr = bs;
  537. if (epoll_ctl(bsys->efd, EPOLL_CTL_ADD, bs->fd, &event) < 0) {
  538. int error = errno;
  539. BLog(BLOG_ERROR, "epoll_ctl failed: %d", error);
  540. return 0;
  541. }
  542. bs->active = 1;
  543. bs->waitEvents = 0;
  544. bs->epoll_returned_ptr = NULL;
  545. DebugCounter_Increment(&bsys->d_fds_counter);
  546. return 1;
  547. }
  548. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  549. {
  550. ASSERT(bs->active)
  551. bs->active = 0;
  552. // delete epoll entry
  553. ASSERT_FORCE(epoll_ctl(bsys->efd, EPOLL_CTL_DEL, bs->fd, NULL) == 0)
  554. // The user can now free the file descriptor object, however the file descriptor
  555. // can still be in the list of returned events. To prevent the event dispatcher
  556. // from crashing, zero its pointer to the file descriptor.
  557. if (bs->epoll_returned_ptr) {
  558. *bs->epoll_returned_ptr = NULL;
  559. }
  560. DebugCounter_Decrement(&bsys->d_fds_counter);
  561. }
  562. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events)
  563. {
  564. ASSERT(bs->active)
  565. ASSERT(!(events&~(BREACTOR_READ|BREACTOR_WRITE)))
  566. if (bs->waitEvents == events) {
  567. return;
  568. }
  569. // update events
  570. bs->waitEvents = events;
  571. // calculate epoll events
  572. int eevents = 0;
  573. if (bs->waitEvents&BREACTOR_READ) {
  574. eevents |= EPOLLIN;
  575. }
  576. if (bs->waitEvents&BREACTOR_WRITE) {
  577. eevents |= EPOLLOUT;
  578. }
  579. // update epoll entry
  580. struct epoll_event event;
  581. memset(&event, 0, sizeof(event));
  582. event.events = eevents;
  583. event.data.ptr = bs;
  584. ASSERT_FORCE(epoll_ctl(bsys->efd, EPOLL_CTL_MOD, bs->fd, &event) == 0)
  585. }
  586. #endif