BReactor.c 20 KB

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