uloop.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * uloop - event loop implementation
  3. *
  4. * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef _ULOOP_H__
  19. #define _ULOOP_H__
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <signal.h>
  25. #if defined(__APPLE__) || defined(__FreeBSD__)
  26. #define USE_KQUEUE
  27. #else
  28. #define USE_EPOLL
  29. #endif
  30. #include "list.h"
  31. struct uloop_fd;
  32. struct uloop_timeout;
  33. struct uloop_process;
  34. typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events);
  35. typedef void (*uloop_timeout_handler)(struct uloop_timeout *t);
  36. typedef void (*uloop_process_handler)(struct uloop_process *c, int ret);
  37. #define ULOOP_READ (1 << 0)
  38. #define ULOOP_WRITE (1 << 1)
  39. #define ULOOP_EDGE_TRIGGER (1 << 2)
  40. #define ULOOP_BLOCKING (1 << 3)
  41. #define ULOOP_EVENT_MASK (ULOOP_READ | ULOOP_WRITE)
  42. /* internal flags */
  43. #define ULOOP_EVENT_BUFFERED (1 << 4)
  44. #ifdef USE_KQUEUE
  45. #define ULOOP_EDGE_DEFER (1 << 5)
  46. #endif
  47. #define ULOOP_ERROR_CB (1 << 6)
  48. struct uloop_fd
  49. {
  50. uloop_fd_handler cb;
  51. int fd;
  52. bool eof;
  53. bool error;
  54. bool registered;
  55. uint8_t flags;
  56. };
  57. struct uloop_timeout
  58. {
  59. struct list_head list;
  60. bool pending;
  61. uloop_timeout_handler cb;
  62. struct timeval time;
  63. };
  64. struct uloop_process
  65. {
  66. struct list_head list;
  67. bool pending;
  68. uloop_process_handler cb;
  69. pid_t pid;
  70. };
  71. extern bool uloop_cancelled;
  72. extern bool uloop_handle_sigchld;
  73. int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
  74. int uloop_fd_delete(struct uloop_fd *sock);
  75. int uloop_timeout_add(struct uloop_timeout *timeout);
  76. int uloop_timeout_set(struct uloop_timeout *timeout, int msecs);
  77. int uloop_timeout_cancel(struct uloop_timeout *timeout);
  78. int uloop_timeout_remaining(struct uloop_timeout *timeout);
  79. int uloop_process_add(struct uloop_process *p);
  80. int uloop_process_delete(struct uloop_process *p);
  81. bool uloop_cancelling(void);
  82. static inline void uloop_end(void)
  83. {
  84. uloop_cancelled = true;
  85. }
  86. int uloop_init(void);
  87. int uloop_run_timeout(int timeout);
  88. static inline int uloop_run(void)
  89. {
  90. return uloop_run_timeout(-1);
  91. }
  92. void uloop_done(void);
  93. #endif