hevent.h 4.1 KB

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