hloop.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifndef HW_LOOP_H_
  2. #define HW_LOOP_H_
  3. #include "hdef.h"
  4. BEGIN_EXTERN_C
  5. #include "htime.h"
  6. #include "array.h"
  7. #include "list.h"
  8. #include "heap.h"
  9. #include "queue.h"
  10. #include "hbuf.h"
  11. typedef struct hloop_s hloop_t;
  12. typedef struct hevent_s hevent_t;
  13. typedef struct hidle_s hidle_t;
  14. typedef struct htimer_s htimer_t;
  15. typedef struct htimeout_s htimeout_t;
  16. typedef struct hperiod_s hperiod_t;
  17. typedef struct hio_s hio_t;
  18. typedef void (*hevent_cb) (hevent_t* ev);
  19. typedef void (*hidle_cb) (hidle_t* idle);
  20. typedef void (*htimer_cb) (htimer_t* timer);
  21. typedef void (*hio_cb) (hio_t* io);
  22. typedef void (*haccept_cb) (hio_t* io, int connfd);
  23. typedef void (*hconnect_cb) (hio_t* io, int state);
  24. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  25. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  26. typedef void (*hclose_cb) (hio_t* io);
  27. typedef enum {
  28. HEVENT_TYPE_NONE = 0,
  29. HEVENT_TYPE_IDLE = 0x00000010,
  30. HEVENT_TYPE_TIMEOUT = 0x00000100,
  31. HEVENT_TYPE_PERIOD = 0x00000200,
  32. HEVENT_TYPE_TIMER = HEVENT_TYPE_TIMEOUT|HEVENT_TYPE_PERIOD,
  33. HEVENT_TYPE_IO = 0x00001000,
  34. } hevent_type_e;
  35. #define HEVENT_LOWEST_PRIORITY -5
  36. #define HEVENT_LOW_PRIORITY -3
  37. #define HEVENT_NORMAL_PRIORITY 0
  38. #define HEVENT_HIGH_PRIORITY 3
  39. #define HEVENT_HIGHEST_PRIORITY 5
  40. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  41. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  42. #define HEVENT_FLAGS \
  43. unsigned destroy :1; \
  44. unsigned active :1; \
  45. unsigned pending :1;
  46. #define HEVENT_FIELDS \
  47. hloop_t* loop; \
  48. hevent_type_e event_type; \
  49. uint64_t event_id; \
  50. hevent_cb cb; \
  51. void* userdata; \
  52. int priority; \
  53. struct hevent_s* pending_next; \
  54. HEVENT_FLAGS
  55. struct hevent_s {
  56. HEVENT_FIELDS
  57. };
  58. struct hidle_s {
  59. HEVENT_FIELDS
  60. uint32_t repeat;
  61. //private:
  62. struct list_node node;
  63. };
  64. #define HTIMER_FIELDS \
  65. HEVENT_FIELDS \
  66. uint32_t repeat; \
  67. uint64_t next_timeout; \
  68. struct heap_node node;
  69. struct htimer_s {
  70. HTIMER_FIELDS
  71. };
  72. struct htimeout_s {
  73. HTIMER_FIELDS
  74. uint32_t timeout; \
  75. };
  76. struct hperiod_s {
  77. HTIMER_FIELDS
  78. int8_t minute;
  79. int8_t hour;
  80. int8_t day;
  81. int8_t week;
  82. int8_t month;
  83. };
  84. QUEUE_DECL(offset_buf_t, write_queue);
  85. struct hio_s {
  86. HEVENT_FIELDS
  87. unsigned accept :1;
  88. unsigned connect :1;
  89. unsigned closed :1;
  90. int fd;
  91. int error;
  92. int events;
  93. int revents;
  94. hbuf_t readbuf;
  95. struct write_queue write_queue;
  96. // callbacks
  97. hread_cb read_cb;
  98. hwrite_cb write_cb;
  99. hclose_cb close_cb;
  100. haccept_cb accept_cb;
  101. hconnect_cb connect_cb;
  102. //private:
  103. int event_index[2];
  104. };
  105. typedef enum {
  106. HLOOP_STATUS_STOP,
  107. HLOOP_STATUS_RUNNING,
  108. HLOOP_STATUS_PAUSE
  109. } hloop_status_e;
  110. ARRAY_DECL(hio_t*, io_array);
  111. struct hloop_s {
  112. hloop_status_e status;
  113. time_t start_time;
  114. // hrtime: us
  115. uint64_t start_hrtime;
  116. uint64_t end_hrtime;
  117. uint64_t cur_hrtime;
  118. uint64_t loop_cnt;
  119. void* userdata;
  120. //private:
  121. // events
  122. uint64_t event_counter;
  123. uint32_t nactives;
  124. uint32_t npendings;
  125. // pendings: with priority as array.index
  126. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  127. // idles
  128. struct list_head idles;
  129. uint32_t nidles;
  130. // timers
  131. struct heap timers;
  132. uint32_t ntimers;
  133. // ios: with fd as array.index
  134. struct io_array ios;
  135. uint32_t nios;
  136. void* iowatcher;
  137. };
  138. // loop
  139. int hloop_init(hloop_t* loop);
  140. //void hloop_cleanup(hloop_t* loop);
  141. int hloop_run(hloop_t* loop);
  142. int hloop_stop(hloop_t* loop);
  143. int hloop_pause(hloop_t* loop);
  144. int hloop_resume(hloop_t* loop);
  145. static inline void hloop_update_time(hloop_t* loop) {
  146. loop->cur_hrtime = gethrtime();
  147. }
  148. static inline time_t hloop_now(hloop_t* loop) {
  149. return loop->start_time + (loop->cur_hrtime - loop->start_hrtime) / 1000000;
  150. }
  151. static inline uint64_t hloop_now_hrtime(hloop_t* loop) {
  152. return loop->start_time*1e6 + (loop->cur_hrtime - loop->start_hrtime);
  153. }
  154. // idle
  155. hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  156. void hidle_del(hidle_t* idle);
  157. // timer
  158. // @param timeout: unit(ms)
  159. htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat DEFAULT(INFINITE));
  160. /*
  161. * minute hour day week month cb
  162. * 0~59 0~23 1~31 0~6 1~12
  163. * 30 -1 -1 -1 -1 cron.hourly
  164. * 30 1 -1 -1 -1 cron.daily
  165. * 30 1 15 -1 -1 cron.monthly
  166. * 30 1 -1 7 -1 cron.weekly
  167. * 30 1 1 -1 10 cron.yearly
  168. */
  169. htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  170. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  171. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  172. void htimer_del(htimer_t* timer);
  173. // io
  174. // frist level apis
  175. #define READ_EVENT 0x0001
  176. #define WRITE_EVENT 0x0004
  177. #define ALL_EVENTS READ_EVENT|WRITE_EVENT
  178. hio_t* hio_add(hloop_t* loop, hio_cb cb, int fd, int events DEFAULT(READ_EVENT));
  179. void hio_del(hio_t* io, int events DEFAULT(ALL_EVENTS));
  180. // second level apis
  181. hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  182. hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  183. hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  184. hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  185. void hclose (hio_t* io);
  186. END_EXTERN_C
  187. #endif // HW_LOOP_H_