BReactor.h 14 KB

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