skynet源码学习 - 从全局队列中弹出/压入一个消息队列过程

来源:互联网 发布:网络远程教育学费 编辑:程序博客网 时间:2024/05/17 04:34


学习云风的skynet源码,简单记录下。

void skynet_globalmq_push(struct message_queue * queue) {struct global_queue *q= Q;uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1));// only one thread can set the slot (change q->queue[tail] from NULL to queue)if (!__sync_bool_compare_and_swap(&q->queue[tail], NULL, queue)) {// The queue may full seldom, save queue in list// 如果swap失败说明queue[] 满了,达到了64K个队列,出现的几率很小// 如果这样的话,就把其保存在Q的list中assert(queue->next == NULL);struct message_queue * last;do {last = q->list;queue->next = last;} while(!__sync_bool_compare_and_swap(&q->list, last, queue));return;}}// 结构体global_queue中的 head, tail 字段分别控制着Q的取,存过程// GP呢可以看做是一个hash的过程,以此来确定其维护的queues的indexstruct message_queue * skynet_globalmq_pop() {struct global_queue *q = Q;uint32_t head =  q->head;if (head == q->tail) {// The queue is empty.return NULL;}uint32_t head_ptr = GP(head);struct message_queue * list = q->list;// 如果list非空,说明Q->queue曾经满过,就把他们转移回queue[]中,因为那里速度更快if (list) {// If q->list is not empty, try to load it back to the queuestruct message_queue *newhead = list->next;if (__sync_bool_compare_and_swap(&q->list, list, newhead)) {// try load list only once, if success , push it back to the queue.list->next = NULL;skynet_globalmq_push(list);}}// 从头取一个消息队列struct message_queue * mq = q->queue[head_ptr];if (mq == NULL) {// globalmq push not completereturn NULL;}// 取走一个消息后自然要将index往后移动一个位置,并且刚那个position置为空if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) {return NULL;}// only one thread can get the slot (change q->queue[head_ptr] to NULL)if (!__sync_bool_compare_and_swap(&q->queue[head_ptr], mq, NULL)) {return NULL;}return mq;}


1 0
原创粉丝点击