BReactor.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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) && (event->events&EPOLLIN)) {
  190. events |= BREACTOR_READ;
  191. }
  192. if ((bfd->waitEvents&BREACTOR_WRITE) && (event->events&EPOLLOUT)) {
  193. events |= BREACTOR_WRITE;
  194. }
  195. if ((event->events&EPOLLERR) || (event->events&EPOLLHUP)) {
  196. events |= BREACTOR_ERROR;
  197. }
  198. if (!events) {
  199. BLog(BLOG_ERROR, "no events detected?");
  200. continue;
  201. }
  202. // call handler
  203. BLog(BLOG_DEBUG, "Dispatching file descriptor");
  204. bfd->handler(bfd->user, events);
  205. // dispatch jobs
  206. dispatch_jobs(bsys);
  207. }
  208. }
  209. #endif
  210. static void wait_for_events (BReactor *bsys)
  211. {
  212. // must have processed all pending events
  213. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
  214. ASSERT(LinkedList2_IsEmpty(&bsys->timers_expired_list))
  215. #ifdef BADVPN_USE_WINAPI
  216. ASSERT(!bsys->returned_object)
  217. #else
  218. ASSERT(bsys->epoll_results_pos == bsys->epoll_results_num)
  219. #endif
  220. // clean up epoll results
  221. #ifndef BADVPN_USE_WINAPI
  222. bsys->epoll_results_num = 0;
  223. bsys->epoll_results_pos = 0;
  224. #endif
  225. // timeout vars
  226. int have_timeout = 0;
  227. btime_t timeout_abs;
  228. btime_t now;
  229. // compute timeout
  230. BHeapNode *first_node;
  231. if (first_node = BHeap_GetFirst(&bsys->timers_heap)) {
  232. // get current time
  233. now = btime_gettime();
  234. // if some timers have already timed out, return them immediately
  235. if (move_expired_timers(bsys, now)) {
  236. BLog(BLOG_DEBUG, "Got already expired timers");
  237. return;
  238. }
  239. // timeout is first timer, remember absolute time
  240. BTimer *first_timer = UPPER_OBJECT(first_node, BTimer, heap_node);
  241. have_timeout = 1;
  242. timeout_abs = first_timer->absTime;
  243. }
  244. int timed_out;
  245. #ifdef BADVPN_USE_WINAPI
  246. int handle_index;
  247. #else
  248. int epoll_num_results;
  249. #endif
  250. // wait until the timeout is reached or the file descriptor / handle in ready
  251. while (1) {
  252. // compute timeout
  253. btimeout_t timeout_arg;
  254. btime_t timeout_rel;
  255. if (have_timeout) {
  256. timeout_rel = timeout_abs - now;
  257. if (timeout_rel > BTIMEOUT_T_MAX) {
  258. timeout_arg = BTIMEOUT_T_MAX;
  259. } else {
  260. timeout_arg = timeout_rel;
  261. }
  262. }
  263. // perform wait
  264. #ifdef BADVPN_USE_WINAPI
  265. BLog(BLOG_DEBUG, "Calling WaitForMultipleObjects on %d handles", bsys->enabled_num);
  266. DWORD waitres = WaitForMultipleObjects(bsys->enabled_num, bsys->enabled_handles, FALSE, (have_timeout ? timeout_arg : INFINITE));
  267. ASSERT_FORCE(waitres != WAIT_FAILED)
  268. ASSERT_FORCE(!(waitres == WAIT_TIMEOUT) || have_timeout)
  269. ASSERT_FORCE(!(waitres != WAIT_TIMEOUT) || (waitres >= WAIT_OBJECT_0 && waitres < WAIT_OBJECT_0 + bsys->enabled_num))
  270. #else
  271. BLog(BLOG_DEBUG, "Calling epoll_wait");
  272. int waitres = epoll_wait(bsys->efd, bsys->epoll_results, BSYSTEM_MAX_RESULTS, (have_timeout ? timeout_arg : -1));
  273. if (waitres < 0) {
  274. int error = errno;
  275. if (error == EINTR) {
  276. BLog(BLOG_DEBUG, "epoll_wait interrupted");
  277. goto try_again;
  278. }
  279. perror("epoll_wait");
  280. ASSERT_FORCE(0)
  281. }
  282. ASSERT_FORCE(!(waitres == 0) || have_timeout)
  283. ASSERT_FORCE(waitres <= BSYSTEM_MAX_RESULTS)
  284. #endif
  285. if (!WAITRES_TIMED_OUT(waitres) || timeout_rel <= BTIMEOUT_T_MAX) {
  286. timed_out = WAITRES_TIMED_OUT(waitres);
  287. if (!timed_out) {
  288. #ifdef BADVPN_USE_WINAPI
  289. handle_index = waitres - WAIT_OBJECT_0;
  290. #else
  291. epoll_num_results = waitres;
  292. #endif
  293. }
  294. break;
  295. }
  296. try_again:
  297. if (have_timeout) {
  298. // get current time
  299. now = btime_gettime();
  300. // check if we already reached the time we're waiting for
  301. if (now >= timeout_abs) {
  302. timed_out = 1;
  303. break;
  304. }
  305. }
  306. }
  307. if (timed_out) {
  308. // timed out, expire first timers
  309. BLog(BLOG_DEBUG, "Wait timed out");
  310. move_first_timers(bsys);
  311. } else {
  312. #ifdef BADVPN_USE_WINAPI
  313. // user's handle got signalled
  314. BLog(BLOG_DEBUG, "Wait returned handle %d", handle_index);
  315. bsys->returned_object = bsys->enabled_objects[handle_index];
  316. #else
  317. // setup returned file descriptors list
  318. BLog(BLOG_DEBUG, "Wait returned %d file descriptors", epoll_num_results);
  319. bsys->epoll_results_num = epoll_num_results;
  320. set_fd_pointers(bsys);
  321. #endif
  322. }
  323. }
  324. #ifdef BADVPN_USE_WINAPI
  325. void BHandle_Init (BHandle *bh, HANDLE handle, BHandle_handler handler, void *user)
  326. {
  327. bh->h = handle;
  328. bh->handler = handler;
  329. bh->user = user;
  330. bh->active = 0;
  331. }
  332. #else
  333. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user)
  334. {
  335. bs->fd = fd;
  336. bs->handler = handler;
  337. bs->user = user;
  338. bs->active = 0;
  339. }
  340. #endif
  341. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *handler_pointer)
  342. {
  343. bt->msTime = msTime;
  344. bt->handler = handler;
  345. bt->handler_pointer = handler_pointer;
  346. bt->active = 0;
  347. }
  348. int BTimer_IsRunning (BTimer *bt)
  349. {
  350. ASSERT(bt->active == 0 || bt->active == 1)
  351. return bt->active;
  352. }
  353. int BReactor_Init (BReactor *bsys)
  354. {
  355. BLog(BLOG_DEBUG, "Reactor initializing");
  356. bsys->exiting = 0;
  357. // init jobs
  358. BPendingGroup_Init(&bsys->pending_jobs);
  359. // init timers
  360. BHeap_Init(&bsys->timers_heap, OFFSET_DIFF(BTimer, absTime, heap_node), (BHeap_comparator)timer_comparator, NULL);
  361. LinkedList2_Init(&bsys->timers_expired_list);
  362. #ifdef BADVPN_USE_WINAPI
  363. bsys->num_handles = 0;
  364. bsys->enabled_num = 0;
  365. bsys->returned_object = NULL;
  366. #else
  367. // create epoll fd
  368. if ((bsys->efd = epoll_create(10)) < 0) {
  369. BLog(BLOG_ERROR, "epoll_create failed");
  370. goto fail0;
  371. }
  372. // init results array
  373. bsys->epoll_results_num = 0;
  374. bsys->epoll_results_pos = 0;
  375. DebugCounter_Init(&bsys->d_fds_counter);
  376. #endif
  377. // init debug object
  378. DebugObject_Init(&bsys->d_obj);
  379. return 1;
  380. fail0:
  381. BPendingGroup_Free(&bsys->pending_jobs);
  382. BLog(BLOG_ERROR, "Reactor failed to initialize");
  383. return 0;
  384. }
  385. void BReactor_Free (BReactor *bsys)
  386. {
  387. // {pending group has no BPending objects}
  388. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
  389. ASSERT(!BHeap_GetFirst(&bsys->timers_heap))
  390. ASSERT(LinkedList2_IsEmpty(&bsys->timers_expired_list))
  391. DebugObject_Free(&bsys->d_obj);
  392. #ifdef BADVPN_USE_WINAPI
  393. ASSERT(bsys->num_handles == 0)
  394. #else
  395. DebugCounter_Free(&bsys->d_fds_counter);
  396. #endif
  397. BLog(BLOG_DEBUG, "Reactor freeing");
  398. #ifndef BADVPN_USE_WINAPI
  399. // close epoll fd
  400. ASSERT_FORCE(close(bsys->efd) == 0)
  401. #endif
  402. // free jobs
  403. BPendingGroup_Free(&bsys->pending_jobs);
  404. }
  405. int BReactor_Exec (BReactor *bsys)
  406. {
  407. BLog(BLOG_DEBUG, "Entering event loop");
  408. while (1) {
  409. dispatch_jobs(bsys);
  410. dispatch_timers(bsys);
  411. dispatch_io(bsys);
  412. if (bsys->exiting) {
  413. break;
  414. }
  415. wait_for_events(bsys);
  416. }
  417. BLog(BLOG_DEBUG, "Exiting event loop, exit code %d", bsys->exit_code);
  418. return bsys->exit_code;
  419. }
  420. void BReactor_Quit (BReactor *bsys, int code)
  421. {
  422. bsys->exiting = 1;
  423. bsys->exit_code = code;
  424. }
  425. void BReactor_SetTimer (BReactor *bsys, BTimer *bt)
  426. {
  427. BReactor_SetTimerAfter(bsys, bt, bt->msTime);
  428. }
  429. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after)
  430. {
  431. btime_t now = btime_gettime();
  432. // handle overflow
  433. int overflows = add_int64_overflows(now, after);
  434. btime_t absTime;
  435. if (overflows != 0) {
  436. if (overflows > 0) {
  437. absTime = INT64_MAX;
  438. } else {
  439. absTime = INT64_MIN;
  440. }
  441. } else {
  442. absTime = now + after;
  443. }
  444. BReactor_SetTimerAbsolute(bsys, bt, absTime);
  445. }
  446. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time)
  447. {
  448. // unlink it if it's already in the list
  449. BReactor_RemoveTimer(bsys, bt);
  450. // initialize timer
  451. bt->active = 1;
  452. bt->expired = 0;
  453. bt->absTime = time;
  454. // insert to running timers heap
  455. BHeap_Insert(&bsys->timers_heap, &bt->heap_node);
  456. }
  457. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
  458. {
  459. if (!bt->active) {
  460. return;
  461. }
  462. if (bt->expired) {
  463. // remove from expired list
  464. LinkedList2_Remove(&bsys->timers_expired_list, &bt->list_node);
  465. } else {
  466. // remove from running heap
  467. BHeap_Remove(&bsys->timers_heap, &bt->heap_node);
  468. }
  469. // set inactive
  470. bt->active = 0;
  471. }
  472. BPendingGroup * BReactor_PendingGroup (BReactor *bsys)
  473. {
  474. return &bsys->pending_jobs;
  475. }
  476. int BReactor_Synchronize (BReactor *bsys, BPending *ref)
  477. {
  478. ASSERT(ref)
  479. while (!bsys->exiting) {
  480. ASSERT(BPendingGroup_HasJobs(&bsys->pending_jobs))
  481. if (BPendingGroup_PeekJob(&bsys->pending_jobs) == ref) {
  482. return 1;
  483. }
  484. BPendingGroup_ExecuteJob(&bsys->pending_jobs);
  485. }
  486. return 0;
  487. }
  488. #ifdef BADVPN_USE_WINAPI
  489. int BReactor_AddHandle (BReactor *bsys, BHandle *bh)
  490. {
  491. ASSERT(!bh->active)
  492. if (bsys->num_handles >= BSYSTEM_MAX_HANDLES) {
  493. return 0;
  494. }
  495. bh->active = 1;
  496. bh->position = -1;
  497. bsys->num_handles++;
  498. return 1;
  499. }
  500. void BReactor_RemoveHandle (BReactor *bsys, BHandle *bh)
  501. {
  502. ASSERT(bh->active)
  503. if (bh->position >= 0) {
  504. BReactor_DisableHandle(bsys, bh);
  505. }
  506. bh->active = 0;
  507. ASSERT(bsys->num_handles > 0)
  508. bsys->num_handles--;
  509. }
  510. void BReactor_EnableHandle (BReactor *bsys, BHandle *bh)
  511. {
  512. ASSERT(bh->active)
  513. ASSERT(bh->position == -1)
  514. ASSERT(bsys->enabled_num < BSYSTEM_MAX_HANDLES)
  515. bsys->enabled_handles[bsys->enabled_num] = bh->h;
  516. bsys->enabled_objects[bsys->enabled_num] = bh;
  517. bh->position = bsys->enabled_num;
  518. bsys->enabled_num++;
  519. }
  520. void BReactor_DisableHandle (BReactor *bsys, BHandle *bh)
  521. {
  522. ASSERT(bh->active)
  523. ASSERT(bh->position >= 0)
  524. ASSERT(bh->position < bsys->enabled_num)
  525. ASSERT(bh == bsys->enabled_objects[bh->position])
  526. ASSERT(bh->h == bsys->enabled_handles[bh->position])
  527. // if there are more handles after this one, move the last
  528. // one into its position
  529. if (bh->position < bsys->enabled_num - 1) {
  530. int move_position = bsys->enabled_num - 1;
  531. BHandle *move_handle = bsys->enabled_objects[move_position];
  532. ASSERT(move_handle->active)
  533. ASSERT(move_handle->position == move_position)
  534. ASSERT(move_handle->h == bsys->enabled_handles[move_position])
  535. bsys->enabled_handles[bh->position] = move_handle->h;
  536. bsys->enabled_objects[bh->position] = move_handle;
  537. move_handle->position = bh->position;
  538. }
  539. bh->position = -1;
  540. bsys->enabled_num--;
  541. // make sure the handler will not be called
  542. if (bsys->returned_object == bh) {
  543. bsys->returned_object = NULL;
  544. }
  545. }
  546. #else
  547. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  548. {
  549. ASSERT(!bs->active)
  550. // add epoll entry
  551. struct epoll_event event;
  552. memset(&event, 0, sizeof(event));
  553. event.events = 0;
  554. event.data.ptr = bs;
  555. if (epoll_ctl(bsys->efd, EPOLL_CTL_ADD, bs->fd, &event) < 0) {
  556. int error = errno;
  557. BLog(BLOG_ERROR, "epoll_ctl failed: %d", error);
  558. return 0;
  559. }
  560. bs->active = 1;
  561. bs->waitEvents = 0;
  562. bs->epoll_returned_ptr = NULL;
  563. DebugCounter_Increment(&bsys->d_fds_counter);
  564. return 1;
  565. }
  566. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  567. {
  568. ASSERT(bs->active)
  569. bs->active = 0;
  570. // delete epoll entry
  571. ASSERT_FORCE(epoll_ctl(bsys->efd, EPOLL_CTL_DEL, bs->fd, NULL) == 0)
  572. // The user can now free the file descriptor object, however the file descriptor
  573. // can still be in the list of returned events. To prevent the event dispatcher
  574. // from crashing, zero its pointer to the file descriptor.
  575. if (bs->epoll_returned_ptr) {
  576. *bs->epoll_returned_ptr = NULL;
  577. }
  578. DebugCounter_Decrement(&bsys->d_fds_counter);
  579. }
  580. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events)
  581. {
  582. ASSERT(bs->active)
  583. ASSERT(!(events&~(BREACTOR_READ|BREACTOR_WRITE)))
  584. if (bs->waitEvents == events) {
  585. return;
  586. }
  587. // update events
  588. bs->waitEvents = events;
  589. // calculate epoll events
  590. int eevents = 0;
  591. if (bs->waitEvents&BREACTOR_READ) {
  592. eevents |= EPOLLIN;
  593. }
  594. if (bs->waitEvents&BREACTOR_WRITE) {
  595. eevents |= EPOLLOUT;
  596. }
  597. // update epoll entry
  598. struct epoll_event event;
  599. memset(&event, 0, sizeof(event));
  600. event.events = eevents;
  601. event.data.ptr = bs;
  602. ASSERT_FORCE(epoll_ctl(bsys->efd, EPOLL_CTL_MOD, bs->fd, &event) == 0)
  603. }
  604. #endif