-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmsgq.c
202 lines (178 loc) · 4.42 KB
/
cmsgq.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*********************************************************************
######################################################################
##
## Created by: Denis Filatov
## Date : 10.11.2005
##
## Copyleft (c) 2003 - 2020
## This code is provided under the MIT license agreement.
######################################################################
*********************************************************************/
#include "cmsgq.h"
#include <stdio.h>
#include <assert.h>
#define _RECEIVER ((void*)-1)
void cmsgq_init_queue(cmsgq_queue_t* const q)
{
memset(q, 0, sizeof(*q));
}
void cmsgq_cleanup_queue(cmsgq_queue_t* const q)
{
cmsgq_t *m;
do {
m = q->t;
} while (m != (cmsgq_t*)ctest_and_swap(&q->t, m, NULL));
q->h = NULL;
while (m) {
cmsgq_t* o = m;
m = m->prev;
o->destructor(o);
}
}
void _cmsgq_enqueue(cmsgq_queue_t* const q, cmsgq_t* const m, void* const receiver, cmsgq_handler_fn* const h)
{
m->handler = h;
m->receiver = receiver ? receiver : _RECEIVER;
cinc_and_fetch(&q->size);
m->next = NULL;
for (;;) {
m->prev = q->t;
cmsgq_t* o = (cmsgq_t*)ctest_and_swap(&q->t, m->prev, m);
if (o == m->prev) {
break;
}
cinc_and_fetch(&q->collisions);
}
}
#ifdef CMSGQ_TEST
void print_reindex(cmsgq_queue_t * q);
#endif
static cmsgq_t* _reindex(cmsgq_queue_t* const q, cmsgq_t* const r)
{
#ifdef CMSGQ_TEST
size_t count = 0;
print_reindex(q);
#endif
cmsgq_t* m = q->t;
if (m == r) {
m = NULL;
}
else {
for (; m->prev != r; m = m->prev) {
m->prev->next = m;
#ifdef CMSGQ_TEST
if (count > q->size) {
assert(NULL == "reindex problem");
}
count++;
#endif
}
}
return m;
}
#ifdef CMSGQ_MULTITHREAD
intptr_t getThreadIdentifier();
int cmsgq_loop(cmsgq_queue_t* const q)
{
cmsgq_t* m, * o, * f;
void* receiver;
for (;;) {
o = m = q->h;
if (m == NULL) {
m = q->t;
if (m == NULL) {
return 0;
}
// reindex
m = _reindex(q, NULL);
}
if (o != (cmsgq_t*)ctest_and_swap(&q->h, o, m->next)) {
cinc_and_fetch(&q->collisions);
continue;
}
f = (cmsgq_t*)ctest_and_swap(&q->t, m, NULL);
if (f == NULL) {
// another thread taken the m
cinc_and_fetch(&q->collisions);
continue;
}
if (m != f) {
// other items exists in the queue
o = m->next;
if (o == NULL) {
_reindex(q, NULL);
o = m->next;
}
if (m != (cmsgq_t*)ctest_and_swap(&o->prev, m, NULL)) {
// another thread taken m
cinc_and_fetch(&q->collisions);
continue;
}
}
break;
}
cdec_and_fetch(&q->size);
receiver = m->receiver == _RECEIVER ? NULL : m->receiver;
m->receiver = NULL;
m->handler(receiver, m);
if (m->receiver == NULL) {
// message was not re-sent
cmsgq_free(m);
}
return 1;
}
#else
int cmsgq_loop(cmsgq_queue_t* const q)
{
cmsgq_t* m;
void* receiver;
m = q->h;
if (m == NULL) {
m = q->t;
if (m == NULL) {
return 0;
}
// reindex
m = _reindex(q, NULL);
}
q->h = m->next;
if (m->next == NULL) {
cmsgq_t* o = (cmsgq_t*)ctest_and_swap(&q->t, m, NULL);
if (o != m) {
o = _reindex(q, m);
q->h = o;
}
}
cdec_and_fetch(&q->size);
receiver = m->receiver == _RECEIVER ? NULL : m->receiver;
m->receiver = NULL;
m->handler(receiver, m);
if (m->receiver == NULL) {
// message was not re-sent
cmsgq_free(m);
}
return 1;
}
#endif
static void _cmsgq_destructor(cmsgq_t* const m){
// default destructor does nothing!
}
cmsgq_t* _cmsgq_init(cmsgq_t* const m, cmsgq_destructor_fn * const destructor)
{
if(m){
memset(m, 0, sizeof(*m));
m->destructor = destructor ? destructor : _cmsgq_destructor;
}
return m;
}
cmsgq_t* _cmsgq_alloc(size_t size, cmsgq_destructor_fn* const destructor)
{
return _cmsgq_init(callocate(size), destructor);
}
void cmsgq_t_free(cmsgq_t* const m) {
free(m);
}
void cmsgq_free(cmsgq_t* const m)
{
m->destructor(m);
}