BReactor.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @file BReactor.h
  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. * @section DESCRIPTION
  23. *
  24. * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  25. * and timers.
  26. */
  27. #ifndef BADVPN_SYSTEM_BREACTOR_H
  28. #define BADVPN_SYSTEM_BREACTOR_H
  29. #if (defined(BADVPN_USE_WINAPI) + defined(BADVPN_USE_EPOLL) + defined(BADVPN_USE_KEVENT) + defined(BADVPN_USE_POLL)) != 1
  30. #error Unknown event backend or too many event backends
  31. #endif
  32. #ifdef BADVPN_USE_WINAPI
  33. #include <windows.h>
  34. #endif
  35. #ifdef BADVPN_USE_EPOLL
  36. #include <sys/epoll.h>
  37. #endif
  38. #ifdef BADVPN_USE_KEVENT
  39. #include <sys/types.h>
  40. #include <sys/event.h>
  41. #include <sys/time.h>
  42. #endif
  43. #ifdef BADVPN_USE_POLL
  44. #include <poll.h>
  45. #endif
  46. #include <stdint.h>
  47. #include <misc/debug.h>
  48. #include <misc/debugcounter.h>
  49. #include <system/DebugObject.h>
  50. #include <structure/LinkedList1.h>
  51. #include <structure/BHeap.h>
  52. #include <system/BTime.h>
  53. #include <system/BPending.h>
  54. struct BTimer_t;
  55. /**
  56. * Handler function invoked when the timer expires.
  57. * The timer was in running state.
  58. * The timer enters not running state before this function is invoked.
  59. * This function is being called from within the timer's previosly
  60. * associated reactor.
  61. *
  62. * @param user value passed to {@link BTimer_Init}
  63. */
  64. typedef void (*BTimer_handler) (void *user);
  65. /**
  66. * Timer object used with {@link BReactor}.
  67. */
  68. typedef struct BTimer_t {
  69. btime_t msTime;
  70. BTimer_handler handler;
  71. void *handler_pointer;
  72. uint8_t active;
  73. uint8_t expired;
  74. btime_t absTime;
  75. union {
  76. BHeapNode heap_node;
  77. LinkedList1Node list_node;
  78. };
  79. } BTimer;
  80. /**
  81. * Initializes the timer object.
  82. * The timer object is initialized in not running state.
  83. *
  84. * @param bt the object
  85. * @param msTime default timeout in milliseconds
  86. * @param handler handler function invoked when the timer expires
  87. * @param user value to pass to the handler function
  88. */
  89. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user);
  90. /**
  91. * Checks if the timer is running.
  92. *
  93. * @param bt the object
  94. * @return 1 if running, 0 if not running
  95. */
  96. int BTimer_IsRunning (BTimer *bt);
  97. #ifdef BADVPN_USE_WINAPI
  98. /**
  99. * Handler function invoked by the reactor when the handle is signalled
  100. * The handle object is in active state, is being called from within
  101. * the associated reactor, and is in monitored state.
  102. *
  103. * @param user value passed to {@link BHandle_Init}
  104. */
  105. typedef void (*BHandle_handler) (void *user);
  106. struct BHandle_t;
  107. /**
  108. * Windows handle object used with {@link BReactor}.
  109. */
  110. typedef struct BHandle_t {
  111. HANDLE h;
  112. BHandle_handler handler;
  113. void *user;
  114. int active;
  115. int position;
  116. } BHandle;
  117. /**
  118. * Initializes the handle object.
  119. * The handle object is initialized in not active state.
  120. *
  121. * @param bh the object
  122. * @param handle underlying Windows handle
  123. * @param handler handler function invoked when the handle is signalled
  124. * @param user value to pass to the handler function
  125. */
  126. void BHandle_Init (BHandle *bh, HANDLE handle, BHandle_handler handler, void *user);
  127. #else
  128. struct BFileDescriptor_t;
  129. #define BREACTOR_READ (1 << 0)
  130. #define BREACTOR_WRITE (1 << 1)
  131. #define BREACTOR_ERROR (1 << 2)
  132. /**
  133. * Handler function invoked by the reactor when one or more events are detected.
  134. * The events argument will contain a subset of the monitored events (BREACTOR_READ, BREACTOR_WRITE),
  135. * plus possibly the error event (BREACTOR_ERROR).
  136. * The file descriptor object is in active state, being called from within
  137. * the associated reactor.
  138. *
  139. * @param user value passed to {@link BFileDescriptor_Init}
  140. * @param events bitmask composed of a subset of monitored events (BREACTOR_READ, BREACTOR_WRITE),
  141. * and possibly the error event (BREACTOR_ERROR).
  142. * Will be nonzero.
  143. */
  144. typedef void (*BFileDescriptor_handler) (void *user, int events);
  145. /**
  146. * File descriptor object used with {@link BReactor}.
  147. */
  148. typedef struct BFileDescriptor_t {
  149. int fd;
  150. BFileDescriptor_handler handler;
  151. void *user;
  152. int active;
  153. int waitEvents;
  154. #ifdef BADVPN_USE_EPOLL
  155. struct BFileDescriptor_t **epoll_returned_ptr;
  156. #endif
  157. #ifdef BADVPN_USE_KEVENT
  158. int kevent_tag;
  159. int **kevent_returned_ptr;
  160. #endif
  161. #ifdef BADVPN_USE_POLL
  162. LinkedList1Node poll_enabled_fds_list_node;
  163. int poll_returned_index;
  164. #endif
  165. } BFileDescriptor;
  166. /**
  167. * Intializes the file descriptor object.
  168. * The object is initialized in not active state.
  169. *
  170. * @param bs file descriptor object to initialize
  171. * @param fb file descriptor to represent
  172. * @param handler handler function invoked by the reactor when a monitored event is detected
  173. * @param user value passed to the handler functuon
  174. */
  175. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user);
  176. #endif
  177. // BReactor
  178. #define BSYSTEM_MAX_RESULTS 64
  179. #define BSYSTEM_MAX_HANDLES 64
  180. #define BSYSTEM_MAX_POLL_FDS 4096
  181. /**
  182. * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  183. * and timers.
  184. */
  185. typedef struct {
  186. int exiting;
  187. int exit_code;
  188. // jobs
  189. BPendingGroup pending_jobs;
  190. // timers
  191. BHeap timers_heap;
  192. LinkedList1 timers_expired_list;
  193. #ifdef BADVPN_USE_WINAPI
  194. int num_handles; // number of user handles
  195. int enabled_num; // number of user handles in the enabled array
  196. HANDLE enabled_handles[BSYSTEM_MAX_HANDLES]; // enabled user handles
  197. BHandle *enabled_objects[BSYSTEM_MAX_HANDLES]; // objects corresponding to enabled handles
  198. BHandle *returned_object;
  199. #endif
  200. #ifdef BADVPN_USE_EPOLL
  201. int efd; // epoll fd
  202. struct epoll_event epoll_results[BSYSTEM_MAX_RESULTS]; // epoll returned events buffer
  203. int epoll_results_num; // number of events in the array
  204. int epoll_results_pos; // number of events processed so far
  205. #endif
  206. #ifdef BADVPN_USE_KEVENT
  207. int kqueue_fd;
  208. struct kevent kevent_results[BSYSTEM_MAX_RESULTS];
  209. int kevent_results_num;
  210. int kevent_results_pos;
  211. #endif
  212. #ifdef BADVPN_USE_POLL
  213. LinkedList1 poll_enabled_fds_list;
  214. int poll_num_enabled_fds;
  215. int poll_results_num;
  216. int poll_results_pos;
  217. struct pollfd *poll_results_pollfds;
  218. BFileDescriptor **poll_results_bfds;
  219. #endif
  220. DebugObject d_obj;
  221. #ifndef BADVPN_USE_WINAPI
  222. DebugCounter d_fds_counter;
  223. #endif
  224. #ifdef BADVPN_USE_KEVENT
  225. DebugCounter d_kevent_ctr;
  226. #endif
  227. } BReactor;
  228. /**
  229. * Initializes the reactor.
  230. * {@link BLog_Init} must have been done.
  231. * {@link BTime_Init} must have been done.
  232. *
  233. * @param bsys the object
  234. * @return 1 on success, 0 on failure
  235. */
  236. int BReactor_Init (BReactor *bsys) WARN_UNUSED;
  237. /**
  238. * Frees the reactor.
  239. * Must not be called from within the event loop ({@link BReactor_Exec}).
  240. * There must be no {@link BPending} objects using the pending group
  241. * returned by {@link BReactor_PendingGroup}.
  242. * There must be no running timers in this reactor.
  243. * There must be no file descriptors or handles registered
  244. * with this reactor.
  245. *
  246. * @param bsys the object
  247. */
  248. void BReactor_Free (BReactor *bsys);
  249. /**
  250. * Runs the event loop.
  251. *
  252. * @param bsys the object
  253. * @return value passed to {@link BReactor_Quit}
  254. */
  255. int BReactor_Exec (BReactor *bsys);
  256. /**
  257. * Causes the event loop ({@link BReactor_Exec}) to cease
  258. * dispatching events and return.
  259. * Any further calls of {@link BReactor_Exec} will return immediately.
  260. *
  261. * @param bsys the object
  262. * @param code value {@link BReactor_Exec} should return. If this is
  263. * called more than once, it will return the last code.
  264. */
  265. void BReactor_Quit (BReactor *bsys, int code);
  266. /**
  267. * Starts a timer to expire after its default time.
  268. * The timer must have been initialized with {@link BTimer_Init}.
  269. * If the timer is in running state, it must be associated with this reactor.
  270. * The timer enters running state, associated with this reactor.
  271. *
  272. * @param bsys the object
  273. * @param bt timer to start
  274. */
  275. void BReactor_SetTimer (BReactor *bsys, BTimer *bt);
  276. /**
  277. * Starts a timer to expire after a given time.
  278. * The timer must have been initialized with {@link BTimer_Init}.
  279. * If the timer is in running state, it must be associated with this reactor.
  280. * The timer enters running state, associated with this reactor.
  281. *
  282. * @param bsys the object
  283. * @param bt timer to start
  284. * @param after relative expiration time
  285. */
  286. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after);
  287. /**
  288. * Starts a timer to expire at the specified time.
  289. * The timer must have been initialized with {@link BTimer_Init}.
  290. * If the timer is in running state, it must be associated with this reactor.
  291. * The timer enters running state, associated with this reactor.
  292. * The timer's expiration time is set to the time argument.
  293. *
  294. * @param bsys the object
  295. * @param bt timer to start
  296. * @param time absolute expiration time (according to {@link btime_gettime})
  297. */
  298. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time);
  299. /**
  300. * Stops a timer.
  301. * If the timer is in running state, it must be associated with this reactor.
  302. * The timer enters not running state.
  303. *
  304. * @param bsys the object
  305. * @param bt timer to stop
  306. */
  307. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt);
  308. /**
  309. * Returns a {@link BPendingGroup} object that can be used to schedule jobs for
  310. * the reactor to execute. These jobs have complete priority over other events
  311. * (timers, file descriptors and Windows handles).
  312. * The returned pending group may only be used as an argument to {@link BPending_Init},
  313. * and must not be accessed by other means.
  314. * All {@link BPending} objects using this group must be freed before freeing
  315. * the reactor.
  316. *
  317. * @param bsys the object
  318. * @return pending group for scheduling jobs for the reactor to execute
  319. */
  320. BPendingGroup * BReactor_PendingGroup (BReactor *bsys);
  321. /**
  322. * Executes pending jobs until either:
  323. * - the reference job is reached, or
  324. * - {@link BReactor_Quit} is called.
  325. * The reference job must be reached before the job list empties.
  326. * The reference job will not be executed.
  327. *
  328. * WARNING: Use with care. This should only be used to to work around third-party software
  329. * that does not integrade into the jobs system. In particular, you should think about:
  330. * - the effects the jobs to be executed may have, and
  331. * - the environment those jobs expect to be executed in.
  332. *
  333. * @param bsys the object
  334. * @param ref reference job. It is not accessed in any way, only its address is compared to
  335. * pending jobs before they are executed.
  336. * @return 1 if the reference job was reached,
  337. * 0 if {@link BReactor_Quit} was called (either while executing a job, or before)
  338. */
  339. int BReactor_Synchronize (BReactor *bsys, BPending *ref);
  340. #ifdef BADVPN_USE_WINAPI
  341. /**
  342. * Registers a Windows handle to be monitored.
  343. *
  344. * @param bsys the object
  345. * @param bh handle object. Must have been initialized with {@link BHandle_Init}.
  346. * Must be in not active state. On success, the handle object enters
  347. * active state, associated with this reactor, and is initialized
  348. * to not monitored state.
  349. * @return 1 on success, 0 on failure
  350. */
  351. int BReactor_AddHandle (BReactor *bsys, BHandle *bh) WARN_UNUSED;
  352. /**
  353. * Unregisters a Windows handle.
  354. *
  355. * @param bsys the object
  356. * @param bh handle object. Must be in active state, associated with this reactor.
  357. * The handle object enters not active state.
  358. */
  359. void BReactor_RemoveHandle (BReactor *bsys, BHandle *bh);
  360. /**
  361. * Starts monitoring a Windows handle.
  362. *
  363. * @param bsys the object
  364. * @param bh handle object. Must be in active state, associated with this reactor.
  365. * Must be in not monitored state.
  366. * The handle object enters monitored state.
  367. */
  368. void BReactor_EnableHandle (BReactor *bsys, BHandle *bh);
  369. /**
  370. * Stops monitoring a Windows handle.
  371. *
  372. * @param bsys the object
  373. * @param bh handle object. Must be in active state, associated with this reactor.
  374. * Must be in monitored state.
  375. * The handle object enters not monitored state.
  376. */
  377. void BReactor_DisableHandle (BReactor *bsys, BHandle *bh);
  378. #else
  379. /**
  380. * Starts monitoring a file descriptor.
  381. *
  382. * @param bsys the object
  383. * @param bs file descriptor object. Must have been initialized with
  384. * {@link BFileDescriptor_Init} Must be in not active state.
  385. * On success, the file descriptor object enters active state,
  386. * associated with this reactor.
  387. * @return 1 on success, 0 on failure
  388. */
  389. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED;
  390. /**
  391. * Stops monitoring a file descriptor.
  392. *
  393. * @param bsys the object
  394. * @param bs {@link BFileDescriptor} object. Must be in active state,
  395. * associated with this reactor. The file descriptor object
  396. * enters not active state.
  397. */
  398. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs);
  399. /**
  400. * Sets monitored file descriptor events.
  401. *
  402. * @param bsys the object
  403. * @param bs {@link BFileDescriptor} object. Must be in active state,
  404. * associated with this reactor.
  405. * @param events events to watch for. Must not have any bits other than
  406. * BREACTOR_READ and BREACTOR_WRITE.
  407. * This overrides previosly monitored events.
  408. */
  409. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events);
  410. #endif
  411. #ifdef BADVPN_USE_KEVENT
  412. typedef void (*BReactorKEvent_handler) (void *user, u_int fflags, intptr_t data);
  413. typedef struct {
  414. BReactor *reactor;
  415. BReactorKEvent_handler handler;
  416. void *user;
  417. uintptr_t ident;
  418. short filter;
  419. int kevent_tag;
  420. int **kevent_returned_ptr;
  421. DebugObject d_obj;
  422. } BReactorKEvent;
  423. int BReactorKEvent_Init (BReactorKEvent *o, BReactor *reactor, BReactorKEvent_handler handler, void *user, uintptr_t ident, short filter, u_int fflags, intptr_t data);
  424. void BReactorKEvent_Free (BReactorKEvent *o);
  425. #endif
  426. #endif