aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/win-x86/LockFreeLIFO.c
blob: ecffde9a7b8bd157bb5afafb7fa62e600e32ce1f (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
#include "LockFreeLIFO.h"
#include "foundation/atomics.h"

/* win32 implementation */

void lifo_init(lifo_t *lifo)
{
	lifo->head = 0;
	lifo->aba = 0;
}

#if 0 // defined in LockFreeLIFO.asm
void lifo_push(lifo_t *lifo, queue_node_t *cl)
{
	queue_node_t *new_head = cl;
	queue_node_t *old_head = 0;
	do
	{
		old_head = (queue_node_t *)lifo->head;
		new_head->Next = old_head;
	} while (!nx_atomic_cmpxchg_pointer(old_head, new_head, (void * volatile *)&lifo->head));
}

queue_node_t *lifo_pop(lifo_t *lifo)
{
	lifo_t old_head, new_head;
	do
	{
		old_head = *lifo;
		if (!old_head.head)
			return 0;

		new_head.head = old_head.head->Next;
		new_head.aba = old_head.aba+1;
	} while (!nx_atomic_cmpxchg2(*(int64_t *)&old_head, *(int64_t *)&new_head, (volatile int64_t *)&lifo->head));
	return (queue_node_t *)old_head.head;
}
#endif

queue_node_t *lifo_malloc(size_t bytes)
{
	return _aligned_malloc(bytes, MEMORY_ALLOCATION_ALIGNMENT);
}

void lifo_free(queue_node_t *ptr)
{
	_aligned_free(ptr);
}