skynet_mq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "skynet.h"
  2. #include "skynet_mq.h"
  3. #include "skynet_handle.h"
  4. #include "spinlock.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <stdbool.h>
  10. #define DEFAULT_QUEUE_SIZE 64
  11. #define MAX_GLOBAL_MQ 0x10000
  12. // 0 means mq is not in global mq.
  13. // 1 means mq is in global mq , or the message is dispatching.
  14. #define MQ_IN_GLOBAL 1
  15. #define MQ_OVERLOAD 1024
  16. struct message_queue {
  17. struct spinlock lock;
  18. uint32_t handle;
  19. int cap;
  20. int head;
  21. int tail;
  22. int release;
  23. int in_global;
  24. int overload;
  25. int overload_threshold;
  26. struct skynet_message *queue;
  27. struct message_queue *next;
  28. };
  29. struct global_queue {
  30. struct message_queue *head;
  31. struct message_queue *tail;
  32. struct spinlock lock;
  33. };
  34. static struct global_queue *Q = NULL;
  35. void
  36. skynet_globalmq_push(struct message_queue * queue) {
  37. struct global_queue *q= Q;
  38. SPIN_LOCK(q)
  39. assert(queue->next == NULL);
  40. if(q->tail) {
  41. q->tail->next = queue;
  42. q->tail = queue;
  43. } else {
  44. q->head = q->tail = queue;
  45. }
  46. SPIN_UNLOCK(q)
  47. }
  48. struct message_queue *
  49. skynet_globalmq_pop() {
  50. struct global_queue *q = Q;
  51. SPIN_LOCK(q)
  52. struct message_queue *mq = q->head;
  53. if(mq) {
  54. q->head = mq->next;
  55. if(q->head == NULL) {
  56. assert(mq == q->tail);
  57. q->tail = NULL;
  58. }
  59. mq->next = NULL;
  60. }
  61. SPIN_UNLOCK(q)
  62. return mq;
  63. }
  64. struct message_queue *
  65. skynet_mq_create(uint32_t handle) {
  66. struct message_queue *q = skynet_malloc(sizeof(*q));
  67. q->handle = handle;
  68. q->cap = DEFAULT_QUEUE_SIZE;
  69. q->head = 0;
  70. q->tail = 0;
  71. SPIN_INIT(q)
  72. // When the queue is create (always between service create and service init) ,
  73. // set in_global flag to avoid push it to global queue .
  74. // If the service init success, skynet_context_new will call skynet_mq_push to push it to global queue.
  75. q->in_global = MQ_IN_GLOBAL;
  76. q->release = 0;
  77. q->overload = 0;
  78. q->overload_threshold = MQ_OVERLOAD;
  79. q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
  80. q->next = NULL;
  81. return q;
  82. }
  83. static void
  84. _release(struct message_queue *q) {
  85. assert(q->next == NULL);
  86. SPIN_DESTROY(q)
  87. skynet_free(q->queue);
  88. skynet_free(q);
  89. }
  90. uint32_t
  91. skynet_mq_handle(struct message_queue *q) {
  92. return q->handle;
  93. }
  94. int
  95. skynet_mq_length(struct message_queue *q) {
  96. int head, tail,cap;
  97. SPIN_LOCK(q)
  98. head = q->head;
  99. tail = q->tail;
  100. cap = q->cap;
  101. SPIN_UNLOCK(q)
  102. if (head <= tail) {
  103. return tail - head;
  104. }
  105. return tail + cap - head;
  106. }
  107. int
  108. skynet_mq_overload(struct message_queue *q) {
  109. if (q->overload) {
  110. int overload = q->overload;
  111. q->overload = 0;
  112. return overload;
  113. }
  114. return 0;
  115. }
  116. int
  117. skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
  118. int ret = 1;
  119. SPIN_LOCK(q)
  120. if (q->head != q->tail) {
  121. *message = q->queue[q->head++];
  122. ret = 0;
  123. int head = q->head;
  124. int tail = q->tail;
  125. int cap = q->cap;
  126. if (head >= cap) {
  127. q->head = head = 0;
  128. }
  129. int length = tail - head;
  130. if (length < 0) {
  131. length += cap;
  132. }
  133. while (length > q->overload_threshold) {
  134. q->overload = length;
  135. q->overload_threshold *= 2;
  136. }
  137. } else {
  138. // reset overload_threshold when queue is empty
  139. q->overload_threshold = MQ_OVERLOAD;
  140. }
  141. if (ret) {
  142. q->in_global = 0;
  143. }
  144. SPIN_UNLOCK(q)
  145. return ret;
  146. }
  147. static void
  148. expand_queue(struct message_queue *q) {
  149. struct skynet_message *new_queue = skynet_malloc(sizeof(struct skynet_message) * q->cap * 2);
  150. int i;
  151. for (i=0;i<q->cap;i++) {
  152. new_queue[i] = q->queue[(q->head + i) % q->cap];
  153. }
  154. q->head = 0;
  155. q->tail = q->cap;
  156. q->cap *= 2;
  157. skynet_free(q->queue);
  158. q->queue = new_queue;
  159. }
  160. void
  161. skynet_mq_push(struct message_queue *q, struct skynet_message *message) {
  162. assert(message);
  163. SPIN_LOCK(q)
  164. q->queue[q->tail] = *message;
  165. if (++ q->tail >= q->cap) {
  166. q->tail = 0;
  167. }
  168. if (q->head == q->tail) {
  169. expand_queue(q);
  170. }
  171. if (q->in_global == 0) {
  172. q->in_global = MQ_IN_GLOBAL;
  173. skynet_globalmq_push(q);
  174. }
  175. SPIN_UNLOCK(q)
  176. }
  177. void
  178. skynet_mq_init() {
  179. struct global_queue *q = skynet_malloc(sizeof(*q));
  180. memset(q,0,sizeof(*q));
  181. SPIN_INIT(q);
  182. Q=q;
  183. }
  184. void
  185. skynet_mq_mark_release(struct message_queue *q) {
  186. SPIN_LOCK(q)
  187. assert(q->release == 0);
  188. q->release = 1;
  189. if (q->in_global != MQ_IN_GLOBAL) {
  190. skynet_globalmq_push(q);
  191. }
  192. SPIN_UNLOCK(q)
  193. }
  194. static void
  195. _drop_queue(struct message_queue *q, message_drop drop_func, void *ud) {
  196. struct skynet_message msg;
  197. while(!skynet_mq_pop(q, &msg)) {
  198. drop_func(&msg, ud);
  199. }
  200. _release(q);
  201. }
  202. void
  203. skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) {
  204. SPIN_LOCK(q)
  205. if (q->release) {
  206. SPIN_UNLOCK(q)
  207. _drop_queue(q, drop_func, ud);
  208. } else {
  209. skynet_globalmq_push(q);
  210. SPIN_UNLOCK(q)
  211. }
  212. }