hevent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #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. hbuf_t readbuf; // for hread
  44. void* iowatcher;
  45. // custom_events
  46. int sockpair[2];
  47. event_queue custom_events;
  48. hmutex_t custom_events_mutex;
  49. };
  50. struct hidle_s {
  51. HEVENT_FIELDS
  52. uint32_t repeat;
  53. //private:
  54. struct list_node node;
  55. };
  56. #define HTIMER_FIELDS \
  57. HEVENT_FIELDS \
  58. uint32_t repeat; \
  59. uint64_t next_timeout; \
  60. struct heap_node node;
  61. struct htimer_s {
  62. HTIMER_FIELDS
  63. };
  64. struct htimeout_s {
  65. HTIMER_FIELDS
  66. uint32_t timeout; \
  67. };
  68. struct hperiod_s {
  69. HTIMER_FIELDS
  70. int8_t minute;
  71. int8_t hour;
  72. int8_t day;
  73. int8_t week;
  74. int8_t month;
  75. };
  76. QUEUE_DECL(offset_buf_t, write_queue);
  77. struct hio_s {
  78. HEVENT_FIELDS
  79. unsigned ready :1;
  80. unsigned closed :1;
  81. unsigned accept :1;
  82. unsigned connect :1;
  83. unsigned connectex :1; // for ConnectEx/DisconnectEx
  84. unsigned recv :1;
  85. unsigned send :1;
  86. unsigned recvfrom :1;
  87. unsigned sendto :1;
  88. int fd;
  89. hio_type_e io_type;
  90. int error;
  91. int events;
  92. int revents;
  93. struct sockaddr* localaddr;
  94. struct sockaddr* peeraddr;
  95. hbuf_t readbuf; // for hread
  96. struct write_queue write_queue; // for hwrite
  97. // callbacks
  98. hread_cb read_cb;
  99. hwrite_cb write_cb;
  100. hclose_cb close_cb;
  101. haccept_cb accept_cb;
  102. hconnect_cb connect_cb;
  103. //private:
  104. int event_index[2]; // for poll,kqueue
  105. void* hovlp; // for iocp/overlapio
  106. void* ssl; // for SSL
  107. };
  108. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  109. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  110. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  111. #define EVENT_ACTIVE(ev) \
  112. if (!ev->active) {\
  113. ev->active = 1;\
  114. ev->loop->nactives++;\
  115. }\
  116. #define EVENT_INACTIVE(ev) \
  117. if (ev->active) {\
  118. ev->active = 0;\
  119. ev->loop->nactives--;\
  120. }\
  121. #define EVENT_PENDING(ev) \
  122. do {\
  123. if (!ev->pending) {\
  124. ev->pending = 1;\
  125. ev->loop->npendings++;\
  126. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  127. ev->pending_next = *phead;\
  128. *phead = (hevent_t*)ev;\
  129. }\
  130. } while(0)
  131. #define EVENT_ADD(loop, ev, cb) \
  132. do {\
  133. ev->loop = loop;\
  134. ev->event_id = ++loop->event_counter;\
  135. ev->cb = (hevent_cb)cb;\
  136. EVENT_ACTIVE(ev);\
  137. } while(0)
  138. #define EVENT_DEL(ev) \
  139. do {\
  140. EVENT_INACTIVE(ev);\
  141. if (!ev->pending) {\
  142. SAFE_FREE(ev);\
  143. }\
  144. } while(0)
  145. #define EVENT_RESET(ev) \
  146. do {\
  147. ev->destroy = 0;\
  148. ev->active = 1;\
  149. ev->pending = 0;\
  150. } while(0)
  151. #endif // HV_EVENT_H_