global mq return NULL when it's not avaliable

This commit is contained in:
云风
2012-10-12 12:16:12 +08:00
parent a93e6b8868
commit e8afe6db20

View File

@@ -43,11 +43,8 @@ static void
skynet_globalmq_push(struct message_queue * queue) { skynet_globalmq_push(struct message_queue * queue) {
struct global_queue *q= Q; struct global_queue *q= Q;
uint32_t head = q->head; uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1));
__sync_synchronize(); assert(!q->flag[tail]);
uint32_t tail = q->tail;
assert(GP(tail+1) != GP(head));
tail = GP(__sync_fetch_and_add(&q->tail,1));
q->queue[tail] = queue; q->queue[tail] = queue;
__sync_synchronize(); __sync_synchronize();
q->flag[tail] = true; q->flag[tail] = true;
@@ -56,27 +53,23 @@ skynet_globalmq_push(struct message_queue * queue) {
struct message_queue * struct message_queue *
skynet_globalmq_pop() { skynet_globalmq_pop() {
struct global_queue *q = Q; struct global_queue *q = Q;
uint32_t head = 0; uint32_t head = q->head;
for(;;) { uint32_t head_ptr = GP(head);
head = q->head; if (head_ptr == GP(q->tail)) {
if (GP(head) == GP(q->tail)) { return NULL;
return NULL;
}
assert(head < q->tail);
if (__sync_bool_compare_and_swap(&q->head, head, head+1)) {
break;
}
} }
head = GP(head); if(!q->flag[head_ptr]) {
return NULL;
while(!q->flag[head]) {
__sync_synchronize();
} }
q->flag[head] = false; struct message_queue * mq = q->queue[head_ptr];
if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) {
return NULL;
}
q->flag[head_ptr] = false;
return q->queue[head]; return mq;
} }
struct message_queue * struct message_queue *