forked from schani/michael-alloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-queue.c
More file actions
53 lines (45 loc) · 1.13 KB
/
test-queue.c
File metadata and controls
53 lines (45 loc) · 1.13 KB
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
51
52
53
#include "atomic.h"
#include "test-queue.h"
void
mono_lock_free_queue_init (MonoLockFreeQueue *q)
{
int i;
for (i = 0; i < MONO_LOCK_FREE_QUEUE_SIZE; ++i)
q->entries [i] = NULL;
q->index = 0;
}
void
mono_lock_free_queue_enqueue (MonoLockFreeQueue *q, MonoLockFreeQueueNode *node)
{
int i, j;
g_assert (!node->in_queue);
node->in_queue = TRUE;
j = 0;
for (i = q->index; ; i = (i + 1) % MONO_LOCK_FREE_QUEUE_SIZE) {
if (!q->entries [i]) {
if (InterlockedCompareExchangePointer ((gpointer volatile*)&q->entries [i], node, NULL) == NULL) {
if (j > MONO_LOCK_FREE_QUEUE_SIZE)
g_print ("queue iterations: %d\n", j);
return;
}
}
++j;
}
}
MonoLockFreeQueueNode*
mono_lock_free_queue_dequeue (MonoLockFreeQueue *q)
{
int index = q->index;
int i;
for (i = (index + 1) % MONO_LOCK_FREE_QUEUE_SIZE; i != index; i = (i + 1) % MONO_LOCK_FREE_QUEUE_SIZE) {
MonoLockFreeQueueNode *node = q->entries [i];
if (node) {
if (InterlockedCompareExchangePointer ((gpointer volatile*)&q->entries [i], NULL, node) == node) {
g_assert (node->in_queue);
node->in_queue = FALSE;
return node;
}
}
}
return NULL;
}