aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/lfmpscq.c
blob: 0f7efa7315ed1db6d3a3f80765463f45c64d0fd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "lfmpscq.h"
#include "foundation/atomics.h"

void mpscq_init(mpscq_t* self)
{
	self->head = &self->stub;
	self->tail = &self->stub;
	self->stub.Next = 0;
}

int mpscq_push(mpscq_t *self, queue_node_t *n)
{
	queue_node_t *prev;
	n->Next = 0;
	prev = nx_atomic_swap_pointer(n, (void * volatile *)&self->head);
	//(*)
	prev->Next = n;
	return prev!=&self->stub;
}

queue_node_t *mpscq_pop(mpscq_t *self)
{
	queue_node_t* tail = self->tail;
	queue_node_t* next = tail->Next;
	queue_node_t* head;
	if (tail == &self->stub)
	{
		if (0 == next)
			return 0;
		self->tail = next;
		tail = next;
		next = next->Next;
	}
	if (next)
	{
		self->tail = next;
		return tail;
	}
	head = self->head;
	if (tail != head)
		return (queue_node_t *)1;
	mpscq_push(self, &self->stub);
	next = tail->Next;
	if (next)
	{
		self->tail = next;
		return tail;
	}
	return 0;
}