eloop.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /** @file eloop.c
  2. * @brief Event loop.
  3. *
  4. * It's a class to organize main event loop. Class has unified interface to get
  5. * different types of events: signals, file descriptor events, scheduled time
  6. * events. User can register callbacks for interested events. Callback has
  7. * the same prototype for all types of events. Callback is called with
  8. * associated data. Assiciated data is user data, type of event and additional
  9. * data with information about things specific for current type. It's a number
  10. * of signal for signals, file descriptor and type of file event for file
  11. * descriptor events, event ID and pointer to special event object for scheduled
  12. * time events.
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif /* HAVE_CONFIG_H */
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <signal.h>
  29. #include <poll.h>
  30. #include <sys/signalfd.h>
  31. #include "faux/faux.h"
  32. #include "faux/str.h"
  33. #include "faux/net.h"
  34. #include "faux/sched.h"
  35. #include "faux/eloop.h"
  36. #include "private.h"
  37. #define TIMESPEC_TO_MILISECONDS(t) ((t.tv_sec * 1000) + (t.tv_nsec / 1000000l))
  38. #ifdef HAVE_SIGNALFD
  39. #define SIGNALFD_FLAGS (SFD_NONBLOCK | SFD_CLOEXEC)
  40. #else // Standard signals
  41. static void *faux_eloop_static_user_data = NULL;
  42. /** @brief Signal handler sends signal number to programm over pipe.
  43. *
  44. * Static service function. It's used for non-linux implementation on systems
  45. * that has no signalfd() function. The pipe pair is created. The write end is
  46. * used in signal handler to write signo to it. The read end of pipe is used
  47. * with poll()-like function to get signal number in main programm. It is
  48. * necessary to solve race problem with poll() function and signal handlers. See
  49. * manpage for select() and pselect().
  50. */
  51. static void faux_eloop_static_sighandler(int signo)
  52. {
  53. int pipe = -1;
  54. if (!faux_eloop_static_user_data)
  55. return;
  56. pipe = *((int *)faux_eloop_static_user_data);
  57. write(pipe, &signo, sizeof(signo));
  58. }
  59. #endif
  60. /** @brief Callback compare function for fd list.
  61. */
  62. static int faux_eloop_fd_compare(const void *first, const void *second)
  63. {
  64. const faux_eloop_fd_t *f = (const faux_eloop_fd_t *)first;
  65. const faux_eloop_fd_t *s = (const faux_eloop_fd_t *)second;
  66. return (f->fd - s->fd);
  67. }
  68. /** @brief Callback compare function for fd list to search by key.
  69. */
  70. static int faux_eloop_fd_kcompare(const void *key, const void *list_item)
  71. {
  72. int *f = (int *)key;
  73. const faux_eloop_fd_t *s = (const faux_eloop_fd_t *)list_item;
  74. return (*f - s->fd);
  75. }
  76. /** @brief Callback compare function for signal list.
  77. */
  78. static int faux_eloop_signal_compare(const void *first, const void *second)
  79. {
  80. const faux_eloop_signal_t *f = (const faux_eloop_signal_t *)first;
  81. const faux_eloop_signal_t *s = (const faux_eloop_signal_t *)second;
  82. return (f->signo - s->signo);
  83. }
  84. /** @brief Callback compare function for signal list to search by key.
  85. */
  86. static int faux_eloop_signal_kcompare(const void *key, const void *list_item)
  87. {
  88. int *f = (int *)key;
  89. const faux_eloop_signal_t *s = (const faux_eloop_signal_t *)list_item;
  90. return (*f - s->signo);
  91. }
  92. /** @brief Create new event loop object.
  93. *
  94. * Function gets default event callback as argument. It will be used for all
  95. * events if private callback for event is not specified.
  96. *
  97. * @param [in] default_event_cb Default event callback.
  98. * @return Allocated faux_eloop_t object or NULL on error.
  99. */
  100. faux_eloop_t *faux_eloop_new(faux_eloop_cb_f default_event_cb)
  101. {
  102. faux_eloop_t *eloop = NULL;
  103. eloop = faux_zmalloc(sizeof(*eloop));
  104. assert(eloop);
  105. if (!eloop)
  106. return NULL;
  107. // Init
  108. eloop->working = BOOL_FALSE;
  109. eloop->default_event_cb = default_event_cb;
  110. // Sched
  111. eloop->sched = faux_sched_new();
  112. assert(eloop->sched);
  113. // FD
  114. eloop->fds = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  115. faux_eloop_fd_compare, faux_eloop_fd_kcompare, faux_free);
  116. assert(eloop->fds);
  117. eloop->pollfds = faux_pollfd_new();
  118. assert(eloop->pollfds);
  119. // Signal
  120. eloop->signals = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  121. faux_eloop_signal_compare, faux_eloop_signal_kcompare, faux_free);
  122. assert(eloop->signals);
  123. sigemptyset(&eloop->sig_set);
  124. sigfillset(&eloop->sig_mask);
  125. #ifdef HAVE_SIGNALFD
  126. eloop->signal_fd = -1;
  127. #endif
  128. return eloop;
  129. }
  130. /** @brief Free event loop object.
  131. *
  132. * @param [in] Event loop object.
  133. */
  134. void faux_eloop_free(faux_eloop_t *eloop)
  135. {
  136. if (!eloop)
  137. return;
  138. faux_list_free(eloop->signals);
  139. faux_pollfd_free(eloop->pollfds);
  140. faux_list_free(eloop->fds);
  141. faux_sched_free(eloop->sched);
  142. faux_free(eloop);
  143. }
  144. /** @brief Event loop function.
  145. *
  146. * Function blocks and waits for registered events. When event occurs the
  147. * correspondent callback will be called. Callback returns bool_t value. If
  148. * callback returns BOOL_FALSE then loop will break and unblock the programm.
  149. * On BOOL_TRUE the loop will wait for the next event.
  150. *
  151. * @param [in] eloop Allocated and initialized event loop object.
  152. * @returns BOOL_TRUE - success, BOOL_FALSE - error.
  153. */
  154. bool_t faux_eloop_loop(faux_eloop_t *eloop)
  155. {
  156. bool_t retval = BOOL_TRUE;
  157. bool_t stop = BOOL_FALSE;
  158. sigset_t blocked_signals;
  159. sigset_t orig_sig_set;
  160. #ifdef HAVE_PPOLL
  161. sigset_t *sigset_for_ppoll = NULL;
  162. #endif // HAVE_PPOLL
  163. #ifndef HAVE_SIGNALFD
  164. int signal_pipe[2];
  165. int fflags = 0;
  166. #endif // not HAVE_SIGNALFD
  167. // If event loop is active already and we try to start nested loop
  168. // then return.
  169. if (eloop->working)
  170. return BOOL_FALSE;
  171. eloop->working = BOOL_TRUE;
  172. // Block signals to prevent race conditions while loop and ppoll()
  173. // Catch signals while ppoll() only
  174. sigfillset(&blocked_signals);
  175. sigprocmask(SIG_SETMASK, &blocked_signals, &orig_sig_set);
  176. #ifdef HAVE_SIGNALFD
  177. // Create Linux-specific signal file descriptor. Wait for signals.
  178. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  179. SIGNALFD_FLAGS);
  180. faux_pollfd_add(eloop->pollfds, eloop->signal_fd, POLLIN);
  181. #else // Standard signal processing
  182. #ifdef PPOLL
  183. sigset_for_ppoll = &eloop->sig_mask;
  184. #endif // HAVE_PPOLL
  185. // Create signal pipe pair to get signal number on pipe read end
  186. pipe(signal_pipe);
  187. fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC);
  188. fflags = fcntl(signal_pipe[0], F_GETFL);
  189. fcntl(signal_pipe[0], F_SETFL, fflags | O_NONBLOCK);
  190. fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC);
  191. fflags = fcntl(signal_pipe[1], F_GETFL);
  192. fcntl(signal_pipe[1], F_SETFL, fflags | O_NONBLOCK);
  193. faux_eloop_static_user_data = &signal_pipe[1];
  194. faux_pollfd_add(eloop->pollfds, signal_pipe[0], POLLIN);
  195. if (faux_list_len(eloop->signals) != 0) {
  196. faux_list_node_t *iter = faux_list_head(eloop->signals);
  197. faux_eloop_signal_t *sig = NULL;
  198. struct sigaction sig_act = {};
  199. sig_act.sa_flags = 0;
  200. sig_act.sa_mask = eloop->sig_set;
  201. sig_act.sa_handler = &faux_eloop_static_sighandler;
  202. while ((sig = (faux_eloop_signal_t *)faux_list_each(&iter)))
  203. sigaction(sig->signo, &sig_act, &sig->oldact);
  204. }
  205. #endif // HAVE_SIGNALFD
  206. // Main loop
  207. while (!stop) {
  208. int sn = 0;
  209. struct timespec *timeout = NULL;
  210. struct timespec next_interval = {};
  211. faux_pollfd_iterator_t pollfd_iter;
  212. struct pollfd *pollfd = NULL;
  213. // Find out next scheduled interval
  214. if (!faux_sched_next_interval(eloop->sched, &next_interval))
  215. timeout = NULL;
  216. else
  217. timeout = &next_interval;
  218. // Wait for events
  219. #ifdef HAVE_PPOLL
  220. sn = ppoll(faux_pollfd_vector(eloop->pollfds),
  221. faux_pollfd_len(eloop->pollfds), timeout, sigset_for_ppoll);
  222. #else // poll()
  223. sigprocmask(SIG_SETMASK, &eloop->sig_mask, NULL);
  224. sn = poll(faux_pollfd_vector(eloop->pollfds),
  225. faux_pollfd_len(eloop->pollfds),
  226. timeout ? TIMESPEC_TO_MILISECONDS(next_interval) : -1);
  227. sigprocmask(SIG_SETMASK, &blocked_signals, NULL);
  228. #endif // HAVE_PPOLL
  229. // Error or signal
  230. if (sn < 0) {
  231. // Let poll() read signal pipe or signalfd on next step
  232. if (EINTR == errno)
  233. continue;
  234. retval = BOOL_FALSE;
  235. break;
  236. }
  237. // Scheduled event
  238. if (0 == sn) {
  239. faux_ev_t *ev = NULL;
  240. // Some scheduled events
  241. while((ev = faux_sched_pop(eloop->sched))) {
  242. faux_eloop_info_sched_t info = {};
  243. bool_t r = BOOL_TRUE;
  244. int ev_id = faux_ev_id(ev);
  245. faux_eloop_context_t *context =
  246. (faux_eloop_context_t *)faux_ev_data(ev);
  247. faux_eloop_cb_f event_cb = context->event_cb;
  248. void *user_data = context->user_data;
  249. if (!faux_ev_is_busy(ev)) {
  250. faux_ev_free(ev);
  251. ev = NULL;
  252. }
  253. if (!event_cb)
  254. event_cb = eloop->default_event_cb;
  255. if (!event_cb) // Callback is not defined
  256. continue;
  257. info.ev_id = ev_id;
  258. // Callback will get only rescheduled event object.
  259. // If event is not scheduled, callback will get NULL.
  260. info.ev = ev;
  261. // Execute callback
  262. r = event_cb(eloop, FAUX_ELOOP_SCHED, &info,
  263. user_data);
  264. // BOOL_FALSE return value means "break the loop"
  265. if (!r)
  266. stop = BOOL_TRUE;
  267. }
  268. continue;
  269. }
  270. // File descriptor
  271. faux_pollfd_init_iterator(eloop->pollfds, &pollfd_iter);
  272. while ((pollfd = faux_pollfd_each_active(eloop->pollfds, &pollfd_iter))) {
  273. int fd = pollfd->fd;
  274. faux_eloop_info_fd_t info = {};
  275. faux_eloop_cb_f event_cb = NULL;
  276. faux_eloop_fd_t *entry = NULL;
  277. bool_t r = BOOL_TRUE;
  278. // Read special signal file descriptor
  279. #ifdef HAVE_SIGNALFD
  280. if (fd == eloop->signal_fd) {
  281. struct signalfd_siginfo signal_info = {};
  282. while (faux_read_block(fd, &signal_info,
  283. sizeof(signal_info)) == sizeof(signal_info)) {
  284. int signo = signal_info.ssi_signo;
  285. #else
  286. if (fd == signal_pipe[0]) {
  287. int tmp = 0;
  288. while (faux_read_block(fd, &tmp,
  289. sizeof(tmp)) == sizeof(tmp)) {
  290. int signo = tmp;
  291. #endif // HAVE_SIGNALFD
  292. faux_eloop_info_signal_t sinfo = {};
  293. faux_eloop_signal_t *sentry =
  294. (faux_eloop_signal_t *)faux_list_kfind(
  295. eloop->signals, &signo);
  296. if (!sentry) // Not registered signal. Drop it.
  297. continue;
  298. event_cb = sentry->context.event_cb;
  299. if (!event_cb)
  300. event_cb = eloop->default_event_cb;
  301. if (!event_cb) // Callback is not defined
  302. continue;
  303. sinfo.signo = signo;
  304. // Execute callback
  305. r = event_cb(eloop, FAUX_ELOOP_SIGNAL, &sinfo,
  306. sentry->context.user_data);
  307. // BOOL_FALSE return value means "break the loop"
  308. if (!r)
  309. stop = BOOL_TRUE;
  310. }
  311. continue; // Another fds are common, not signal
  312. }
  313. // File descriptor
  314. entry = (faux_eloop_fd_t *)faux_list_kfind(eloop->fds, &fd);
  315. assert(entry);
  316. if (!entry) // Something went wrong
  317. continue;
  318. event_cb = entry->context.event_cb;
  319. if (!event_cb)
  320. event_cb = eloop->default_event_cb;
  321. if (!event_cb) // Callback function is not defined for this event
  322. continue;
  323. info.fd = fd;
  324. info.revents = pollfd->revents;
  325. // Execute callback
  326. r = event_cb(eloop, FAUX_ELOOP_FD, &info, entry->context.user_data);
  327. // BOOL_FALSE return value means "break the loop"
  328. if (!r)
  329. stop = BOOL_TRUE;
  330. }
  331. } // Loop end
  332. #ifdef HAVE_SIGNALFD
  333. // Close signal file descriptor
  334. faux_pollfd_del_by_fd(eloop->pollfds, eloop->signal_fd);
  335. close(eloop->signal_fd);
  336. eloop->signal_fd = -1;
  337. #else // Standard signals. Restore signal handlers
  338. if (faux_list_len(eloop->signals) != 0) {
  339. faux_list_node_t *iter = faux_list_head(eloop->signals);
  340. faux_eloop_signal_t *sig = NULL;
  341. while ((sig = (faux_eloop_signal_t *)faux_list_each(&iter)))
  342. sigaction(sig->signo, &sig->oldact, NULL);
  343. }
  344. faux_pollfd_del_by_fd(eloop->pollfds, signal_pipe[0]);
  345. close(signal_pipe[0]);
  346. close(signal_pipe[1]);
  347. #endif
  348. // Unblock signals
  349. sigprocmask(SIG_SETMASK, &orig_sig_set, NULL);
  350. // Deactivate loop flag
  351. eloop->working = BOOL_FALSE;
  352. return retval;
  353. }
  354. /** @brief Registers file descriptor to wait for events.
  355. *
  356. * See poll() for explanation of possible file events ("events" argument).
  357. *
  358. * @param [in] eloop Allocated and initialized event loop object.
  359. * @param [in] fd File descriptor to wait on.
  360. * @param [in] events File events mask like POLLIN, POLLOUT.
  361. * @param [in] event_cb Callback for event.
  362. * @param [in] user_data User data to pass to callback.
  363. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  364. */
  365. bool_t faux_eloop_add_fd(faux_eloop_t *eloop, int fd, short events,
  366. faux_eloop_cb_f event_cb, void *user_data)
  367. {
  368. faux_eloop_fd_t *entry = NULL;
  369. faux_list_node_t *new_node = NULL;
  370. if (!eloop || (fd < 0))
  371. return BOOL_FALSE;
  372. entry = faux_zmalloc(sizeof(*entry));
  373. if (!entry)
  374. return BOOL_FALSE;
  375. entry->fd = fd;
  376. entry->events = events;
  377. entry->context.event_cb = event_cb;
  378. entry->context.user_data = user_data;
  379. if (!(new_node = faux_list_add(eloop->fds, entry))) {
  380. faux_free(entry);
  381. return BOOL_FALSE;
  382. }
  383. if (!faux_pollfd_add(eloop->pollfds, entry->fd, entry->events)) {
  384. faux_list_del(eloop->fds, new_node);
  385. faux_free(entry);
  386. return BOOL_FALSE;
  387. }
  388. return BOOL_TRUE;
  389. }
  390. /** @brief Unregisters file descriptor.
  391. *
  392. * @param [in] eloop Allocated and initialized event loop object.
  393. * @param [in] fd File descriptor to unregister.
  394. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  395. */
  396. bool_t faux_eloop_del_fd(faux_eloop_t *eloop, int fd)
  397. {
  398. if (!eloop || (fd < 0))
  399. return BOOL_FALSE;
  400. if (!faux_list_kdel(eloop->fds, &fd))
  401. return BOOL_FALSE;
  402. if (!faux_pollfd_del_by_fd(eloop->pollfds, fd))
  403. return BOOL_FALSE;
  404. return BOOL_TRUE;
  405. }
  406. /** @brief Registers signal to wait for.
  407. *
  408. * @param [in] eloop Allocated and initialized event loop object.
  409. * @param [in] signal Signal number to wait for.
  410. * @param [in] event_cb Callback for event.
  411. * @param [in] user_data User data to pass to callback.
  412. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  413. */
  414. bool_t faux_eloop_add_signal(faux_eloop_t *eloop, int signo,
  415. faux_eloop_cb_f event_cb, void *user_data)
  416. {
  417. faux_eloop_signal_t *entry = NULL;
  418. if (!eloop || (signo < 0))
  419. return BOOL_FALSE;
  420. if (sigismember(&eloop->sig_set, signo) == 1)
  421. return BOOL_FALSE; // Already exists
  422. // Firstly try to add signal to sigset. Library function will validate
  423. // signal number value.
  424. if (sigaddset(&eloop->sig_set, signo) < 0)
  425. return BOOL_FALSE; // Invalid signal number
  426. sigdelset(&eloop->sig_mask, signo);
  427. entry = faux_zmalloc(sizeof(*entry));
  428. if (!entry) {
  429. sigdelset(&eloop->sig_set, signo);
  430. sigaddset(&eloop->sig_mask, signo);
  431. return BOOL_FALSE;
  432. }
  433. entry->signo = signo;
  434. entry->context.event_cb = event_cb;
  435. entry->context.user_data = user_data;
  436. if (!faux_list_add(eloop->signals, entry)) {
  437. faux_free(entry);
  438. sigdelset(&eloop->sig_set, signo);
  439. sigaddset(&eloop->sig_mask, signo);
  440. return BOOL_FALSE;
  441. }
  442. if (eloop->working) { // Add signal on the fly
  443. #ifdef HAVE_SIGNALFD
  444. // Reattach signalfd handler with updated sig_set
  445. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  446. SIGNALFD_FLAGS);
  447. #else // Standard signals
  448. struct sigaction sig_act = {};
  449. sig_act.sa_flags = 0;
  450. sig_act.sa_mask = eloop->sig_set;
  451. sig_act.sa_handler = &faux_eloop_static_sighandler;
  452. sigaction(signo, &sig_act, &entry->oldact);
  453. #endif
  454. }
  455. return BOOL_TRUE;
  456. }
  457. /** @brief Unregisters signal to wait for.
  458. *
  459. * @param [in] eloop Allocated and initialized event loop object.
  460. * @param [in] signal Signal to unregister.
  461. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  462. */
  463. bool_t faux_eloop_del_signal(faux_eloop_t *eloop, int signo)
  464. {
  465. if (!eloop || (signo < 0))
  466. return BOOL_FALSE;
  467. if (sigismember(&eloop->sig_set, signo) != 1)
  468. return BOOL_FALSE; // Doesn't exist
  469. sigdelset(&eloop->sig_set, signo);
  470. sigaddset(&eloop->sig_mask, signo);
  471. if (eloop->working) { // Del signal on the fly
  472. #ifdef HAVE_SIGNALFD
  473. // Reattach signalfd handler with updated sig_set
  474. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  475. SIGNALFD_FLAGS);
  476. #else // Standard signals
  477. faux_eloop_signal_t *sig = faux_list_kfind(eloop->signals, &signo);
  478. sigaction(signo, &sig->oldact, NULL);
  479. #endif
  480. }
  481. faux_list_kdel(eloop->signals, &signo);
  482. return BOOL_TRUE;
  483. }
  484. /** @brief Service function to create new context for event.
  485. *
  486. * @param [in] event_cb Callback for event.
  487. * @param [in] data User data for event.
  488. * @return Allocated context structure or NULL on error.
  489. */
  490. static faux_eloop_context_t *faux_eloop_new_context(
  491. faux_eloop_cb_f event_cb, void *data)
  492. {
  493. faux_eloop_context_t *context = NULL;
  494. context = faux_zmalloc(sizeof(*context));
  495. assert(context);
  496. if (!context)
  497. return NULL;
  498. context->event_cb = event_cb;
  499. context->user_data = data;
  500. return context;
  501. }
  502. /** @brief Registers scheduled time event. See faux_sched_once().
  503. *
  504. * @param [in] eloop Allocated and initialized event loop object.
  505. * @param [in] time See faux_sched_once().
  506. * @param [in] ev_id See faux_sched_once().
  507. * @param [in] event_cb See faux_sched_once().
  508. * @param [in] data See faux_sched_once().
  509. * @return Pointer to created faux_ev_t object or NULL on error.
  510. */
  511. faux_ev_t *faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
  512. int ev_id, faux_eloop_cb_f event_cb, void *data)
  513. {
  514. faux_eloop_context_t *context = NULL;
  515. faux_ev_t *ev = NULL;
  516. assert(eloop);
  517. if (!eloop)
  518. return NULL;
  519. context = faux_eloop_new_context(event_cb, data);
  520. assert(context);
  521. if (!context)
  522. return NULL;
  523. if (!(ev = faux_sched_once(eloop->sched, time, ev_id, context))) {
  524. faux_free(context);
  525. return NULL;
  526. }
  527. faux_ev_set_free_data_cb(ev, faux_free);
  528. return ev;
  529. }
  530. /** @brief Registers scheduled time event. See faux_sched_once_delayed().
  531. *
  532. * @param [in] eloop Allocated and initialized event loop object.
  533. * @param [in] interval See faux_sched_once_delayed().
  534. * @param [in] ev_id See faux_sched_once_delayed().
  535. * @param [in] event_cb See faux_sched_once_delayed().
  536. * @param [in] data See faux_sched_once_delayed().
  537. * @return Pointer to created faux_ev_t object or NULL on error.
  538. */
  539. faux_ev_t *faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
  540. int ev_id, faux_eloop_cb_f event_cb, void *data)
  541. {
  542. faux_eloop_context_t *context = NULL;
  543. faux_ev_t *ev = NULL;
  544. assert(eloop);
  545. if (!eloop)
  546. return NULL;
  547. context = faux_eloop_new_context(event_cb, data);
  548. assert(context);
  549. if (!context)
  550. return NULL;
  551. if (!(ev = faux_sched_once_delayed(eloop->sched, interval, ev_id, context))) {
  552. faux_free(context);
  553. return NULL;
  554. }
  555. faux_ev_set_free_data_cb(ev, faux_free);
  556. return ev;
  557. }
  558. /** @brief Registers scheduled time event. See faux_sched_periodic().
  559. *
  560. * @param [in] eloop Allocated and initialized event loop object.
  561. * @param [in] time See faux_sched_periodic().
  562. * @param [in] ev_id See faux_sched_periodic().
  563. * @param [in] event_cb See faux_sched_periodic().
  564. * @param [in] data See faux_sched_periodic().
  565. * @param [in] period See faux_sched_periodic().
  566. * @param [in] cycle_num See faux_sched_periodic().
  567. * @return Pointer to created faux_ev_t object or NULL on error.
  568. */
  569. faux_ev_t *faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
  570. int ev_id, faux_eloop_cb_f event_cb, void *data,
  571. const struct timespec *period, unsigned int cycle_num)
  572. {
  573. faux_eloop_context_t *context = NULL;
  574. faux_ev_t *ev = NULL;
  575. assert(eloop);
  576. if (!eloop)
  577. return NULL;
  578. context = faux_eloop_new_context(event_cb, data);
  579. assert(context);
  580. if (!context)
  581. return NULL;
  582. if (!(ev = faux_sched_periodic(eloop->sched, time, ev_id, context,
  583. period, cycle_num))) {
  584. faux_free(context);
  585. return NULL;
  586. }
  587. faux_ev_set_free_data_cb(ev, faux_free);
  588. return ev;
  589. }
  590. /** @brief Registers scheduled time event. See faux_sched_periodic_delayed().
  591. *
  592. * @param [in] eloop Allocated and initialized event loop object.
  593. * @param [in] ev_id See faux_sched_periodic_delayed().
  594. * @param [in] event_cb See faux_sched_periodic_delayed().
  595. * @param [in] data See faux_sched_periodic_delayed().
  596. * @param [in] period See faux_sched_periodic_delayed().
  597. * @param [in] cycle_num See faux_sched_periodic_delayed().
  598. * @return Pointer to created faux_ev_t object or NULL on error.
  599. */
  600. faux_ev_t *faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
  601. int ev_id, faux_eloop_cb_f event_cb, void *data,
  602. const struct timespec *period, unsigned int cycle_num)
  603. {
  604. faux_eloop_context_t *context = NULL;
  605. faux_ev_t *ev = NULL;
  606. assert(eloop);
  607. if (!eloop)
  608. return NULL;
  609. context = faux_eloop_new_context(event_cb, data);
  610. assert(context);
  611. if (!context)
  612. return NULL;
  613. if (!(ev = faux_sched_periodic_delayed(eloop->sched, ev_id, context,
  614. period, cycle_num))) {
  615. faux_free(context);
  616. return NULL;
  617. }
  618. faux_ev_set_free_data_cb(ev, faux_free);
  619. return ev;
  620. }
  621. /** @brief Unregisters scheduled time event.
  622. *
  623. * @param [in] eloop Allocated and initialized event loop object.
  624. * @param [in] ev Event object to unregister.
  625. * @return Number of unregistered entries or < 0 on error.
  626. */
  627. ssize_t faux_eloop_del_sched(faux_eloop_t *eloop, faux_ev_t *ev)
  628. {
  629. assert(eloop);
  630. if (!eloop)
  631. return -1;
  632. return faux_sched_del(eloop->sched, ev);
  633. }
  634. /** @brief Unregisters scheduled time event by event ID.
  635. *
  636. * @param [in] eloop Allocated and initialized event loop object.
  637. * @param [in] ev_id Event ID to unregister.
  638. * @return Number of unregistered entries or < 0 on error.
  639. */
  640. ssize_t faux_eloop_del_sched_by_id(faux_eloop_t *eloop, int ev_id)
  641. {
  642. assert(eloop);
  643. if (!eloop)
  644. return -1;
  645. return faux_sched_del_by_id(eloop->sched, ev_id);
  646. }