hevent.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "hloop.h"
  4. #include "hbuf.h"
  5. #include "hmutex.h"
  6. #include "array.h"
  7. #include "list.h"
  8. #include "heap.h"
  9. #include "queue.h"
  10. #define HLOOP_READ_BUFSIZE 8192
  11. typedef enum {
  12. HLOOP_STATUS_STOP,
  13. HLOOP_STATUS_RUNNING,
  14. HLOOP_STATUS_PAUSE
  15. } hloop_status_e;
  16. ARRAY_DECL(hio_t*, io_array);
  17. QUEUE_DECL(hevent_t, event_queue);
  18. struct hloop_s {
  19. uint32_t flags;
  20. hloop_status_e status;
  21. uint64_t start_ms; // ms
  22. uint64_t start_hrtime; // us
  23. uint64_t end_hrtime;
  24. uint64_t cur_hrtime;
  25. uint64_t loop_cnt;
  26. void* userdata;
  27. //private:
  28. // events
  29. uint64_t event_counter;
  30. uint32_t nactives;
  31. uint32_t npendings;
  32. // pendings: with priority as array.index
  33. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  34. // idles
  35. struct list_head idles;
  36. uint32_t nidles;
  37. // timers
  38. struct heap timers;
  39. uint32_t ntimers;
  40. // ios: with fd as array.index
  41. struct io_array ios;
  42. uint32_t nios;
  43. // one loop per thread, so one readbuf per loop is OK.
  44. hbuf_t readbuf;
  45. void* iowatcher;
  46. // custom_events
  47. int sockpair[2];
  48. event_queue custom_events;
  49. hmutex_t custom_events_mutex;
  50. };
  51. struct hidle_s {
  52. HEVENT_FIELDS
  53. uint32_t repeat;
  54. //private:
  55. struct list_node node;
  56. };
  57. #define HTIMER_FIELDS \
  58. HEVENT_FIELDS \
  59. uint32_t repeat; \
  60. uint64_t next_timeout; \
  61. struct heap_node node;
  62. struct htimer_s {
  63. HTIMER_FIELDS
  64. };
  65. struct htimeout_s {
  66. HTIMER_FIELDS
  67. uint32_t timeout; \
  68. };
  69. struct hperiod_s {
  70. HTIMER_FIELDS
  71. int8_t minute;
  72. int8_t hour;
  73. int8_t day;
  74. int8_t week;
  75. int8_t month;
  76. };
  77. QUEUE_DECL(offset_buf_t, write_queue);
  78. struct hio_s {
  79. HEVENT_FIELDS
  80. unsigned ready :1;
  81. unsigned closed :1;
  82. unsigned accept :1;
  83. unsigned connect :1;
  84. unsigned connectex :1; // for ConnectEx/DisconnectEx
  85. unsigned recv :1;
  86. unsigned send :1;
  87. unsigned recvfrom :1;
  88. unsigned sendto :1;
  89. int fd;
  90. hio_type_e io_type;
  91. int error;
  92. int events;
  93. int revents;
  94. struct sockaddr* localaddr;
  95. struct sockaddr* peeraddr;
  96. hbuf_t readbuf; // for hread
  97. struct write_queue write_queue; // for hwrite
  98. // callbacks
  99. hread_cb read_cb;
  100. hwrite_cb write_cb;
  101. hclose_cb close_cb;
  102. haccept_cb accept_cb;
  103. hconnect_cb connect_cb;
  104. //private:
  105. int event_index[2]; // for poll,kqueue
  106. void* hovlp; // for iocp/overlapio
  107. void* ssl; // for SSL
  108. htimer_t* timer; // for io timeout
  109. };
  110. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  111. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  112. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  113. #define EVENT_ACTIVE(ev) \
  114. if (!ev->active) {\
  115. ev->active = 1;\
  116. ev->loop->nactives++;\
  117. }\
  118. #define EVENT_INACTIVE(ev) \
  119. if (ev->active) {\
  120. ev->active = 0;\
  121. ev->loop->nactives--;\
  122. }\
  123. #define EVENT_PENDING(ev) \
  124. do {\
  125. if (!ev->pending) {\
  126. ev->pending = 1;\
  127. ev->loop->npendings++;\
  128. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  129. ev->pending_next = *phead;\
  130. *phead = (hevent_t*)ev;\
  131. }\
  132. } while(0)
  133. #define EVENT_ADD(loop, ev, cb) \
  134. do {\
  135. ev->loop = loop;\
  136. ev->event_id = ++loop->event_counter;\
  137. ev->cb = (hevent_cb)cb;\
  138. EVENT_ACTIVE(ev);\
  139. } while(0)
  140. #define EVENT_DEL(ev) \
  141. do {\
  142. EVENT_INACTIVE(ev);\
  143. if (!ev->pending) {\
  144. HV_FREE(ev);\
  145. }\
  146. } while(0)
  147. #define EVENT_RESET(ev) \
  148. do {\
  149. ev->destroy = 0;\
  150. ev->active = 1;\
  151. ev->pending = 0;\
  152. } while(0)
  153. #endif // HV_EVENT_H_