BReactor.h 13 KB

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