hloop.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 hio_s hio_t;
  16. typedef void (*hevent_cb) (hevent_t* ev);
  17. typedef void (*hidle_cb) (hidle_t* idle);
  18. typedef void (*htimer_cb) (htimer_t* timer);
  19. typedef void (*hio_cb) (hio_t* io);
  20. typedef void (*haccept_cb) (hio_t* io, int connfd);
  21. typedef void (*hconnect_cb) (hio_t* io, int state);
  22. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  23. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  24. typedef void (*hclose_cb) (hio_t* io);
  25. typedef enum {
  26. HEVENT_TYPE_NONE = 0,
  27. HEVENT_TYPE_IDLE = 0x0001,
  28. HEVENT_TYPE_TIMER = 0x0002,
  29. HEVENT_TYPE_IO = 0x0004,
  30. } hevent_type_e;
  31. #define HEVENT_LOWEST_PRIORITY -10
  32. #define HEVENT_LOW_PRIORITY -5
  33. #define HEVENT_NORMAL_PRIORITY 0
  34. #define HEVENT_HIGH_PRIORITY 5
  35. #define HEVENT_HIGHEST_PRIORITY 10
  36. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  37. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  38. #define HEVENT_FLAGS \
  39. unsigned destroy :1; \
  40. unsigned active :1; \
  41. unsigned pending :1;
  42. #define HEVENT_FIELDS \
  43. hloop_t* loop; \
  44. hevent_type_e event_type; \
  45. uint64_t event_id; \
  46. hevent_cb cb; \
  47. void* userdata; \
  48. int priority; \
  49. struct hevent_s* pending_next; \
  50. HEVENT_FLAGS
  51. struct hevent_s {
  52. HEVENT_FIELDS
  53. };
  54. struct hidle_s {
  55. HEVENT_FIELDS
  56. uint32_t repeat;
  57. //private:
  58. struct list_node node;
  59. };
  60. struct htimer_s {
  61. HEVENT_FIELDS
  62. uint32_t repeat;
  63. uint32_t timeout;
  64. //private:
  65. uint64_t next_timeout;
  66. struct list_node node;
  67. struct heap_node hnode;
  68. };
  69. QUEUE_DECL(offset_buf_t, write_queue);
  70. struct hio_s {
  71. HEVENT_FIELDS
  72. unsigned accept :1;
  73. unsigned connect :1;
  74. unsigned closed :1;
  75. int fd;
  76. int error;
  77. int events;
  78. int revents;
  79. hbuf_t readbuf;
  80. struct write_queue write_queue;
  81. // callbacks
  82. hread_cb read_cb;
  83. hwrite_cb write_cb;
  84. hclose_cb close_cb;
  85. haccept_cb accept_cb;
  86. hconnect_cb connect_cb;
  87. //private:
  88. int event_index[2];
  89. };
  90. typedef enum {
  91. HLOOP_STATUS_STOP,
  92. HLOOP_STATUS_RUNNING,
  93. HLOOP_STATUS_PAUSE
  94. } hloop_status_e;
  95. ARRAY_DECL(hio_t*, io_array);
  96. struct hloop_s {
  97. hloop_status_e status;
  98. time_t start_time;
  99. // hrtime: us
  100. uint64_t start_hrtime;
  101. uint64_t end_hrtime;
  102. uint64_t cur_hrtime;
  103. uint64_t loop_cnt;
  104. void* userdata;
  105. //private:
  106. // events
  107. uint64_t event_counter;
  108. uint32_t nevents;
  109. uint32_t nactives;
  110. uint32_t npendings;
  111. // pendings: with priority as array.index
  112. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  113. // idles
  114. struct list_head idles;
  115. uint64_t idle_time;
  116. uint64_t last_idle_hrtime;
  117. // timers
  118. struct list_head timers;
  119. struct heap timer_minheap;
  120. // ios: with fd as array.index
  121. struct io_array ios;
  122. void* iowatcher;
  123. };
  124. // loop
  125. int hloop_init(hloop_t* loop);
  126. //void hloop_cleanup(hloop_t* loop);
  127. int hloop_run(hloop_t* loop);
  128. int hloop_stop(hloop_t* loop);
  129. int hloop_pause(hloop_t* loop);
  130. int hloop_resume(hloop_t* loop);
  131. static inline void hloop_update_time(hloop_t* loop) {
  132. loop->cur_hrtime = gethrtime();
  133. }
  134. static inline time_t hloop_now(hloop_t* loop) {
  135. return loop->start_time + (loop->cur_hrtime - loop->start_hrtime) / 1000000;
  136. }
  137. // idle
  138. hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  139. void hidle_del(hidle_t* idle);
  140. // timer
  141. // @param timeout: unit(ms)
  142. htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat DEFAULT(INFINITE));
  143. void htimer_del(htimer_t* timer);
  144. // io
  145. // frist level apis
  146. #define READ_EVENT 0x0001
  147. #define WRITE_EVENT 0x0004
  148. #define ALL_EVENTS READ_EVENT|WRITE_EVENT
  149. hio_t* hio_add(hloop_t* loop, hio_cb cb, int fd, int events DEFAULT(READ_EVENT));
  150. void hio_del(hio_t* io, int events DEFAULT(ALL_EVENTS));
  151. // second level apis
  152. hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  153. hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  154. hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  155. hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  156. void hclose (hio_t* io);
  157. END_EXTERN_C
  158. #endif // HW_LOOP_H_