-
Notifications
You must be signed in to change notification settings - Fork 4
/
multiq.c
278 lines (236 loc) · 7.66 KB
/
multiq.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* partr -- parallel tasks runtime
MultiQueues (http://arxiv.org/abs/1411.1209)
*/
#include <stdlib.h>
#include <pthread.h>
#include "partr.h"
#include "multiq.h"
#include "congrng.h"
#include "perfutil.h"
/* individual spin-lock synchronized task heap */
typedef struct taskheap_tag {
char lock;
ptask_t **tasks;
int16_t ntasks, prio;
} taskheap_t;
/* heap 'n'ary */
static const int16_t heap_d = 8;
/* the multiqueue itself is 'p' task heaps */
static taskheap_t *heaps;
static int16_t heap_p;
/* for atomic snapshot */
static uint64_t snapshot_owner = -1;
/* unbias state for the RNG */
static uint64_t cong_unbias;
/* state for sleep checking */
static const int16_t not_sleeping = 0;
static const int16_t checking_for_sleeping = 1;
static const int16_t sleeping = 2;
static int16_t sleep_check_state = not_sleeping;
/* multiq_init()
*/
void multiq_init()
{
heap_p = MULTIQ_HEAP_C * nthreads;
heaps = (taskheap_t *)calloc(heap_p, sizeof(taskheap_t));
for (int16_t i = 0; i < heap_p; ++i) {
__atomic_clear(&heaps[i].lock, __ATOMIC_RELAXED);
heaps[i].tasks = (ptask_t **)
calloc(MULTIQ_TASKS_PER_HEAP, sizeof(ptask_t *));
heaps[i].ntasks = 0;
heaps[i].prio = INT16_MAX;
}
unbias_cong(heap_p, &cong_unbias);
LOG_INFO(plog, " %d %d-ary heaps of %d tasks each\n",
heap_p, heap_d, MULTIQ_TASKS_PER_HEAP);
}
/* multiq_destroy()
*/
void multiq_destroy()
{
for (int16_t i = 0; i < heap_p; ++i)
free(heaps[i].tasks);
free(heaps);
}
/* sift_up()
*/
static void sift_up(taskheap_t *heap, int16_t idx)
{
if (idx > 0) {
int16_t parent = (idx-1)/heap_d;
if (heap->tasks[idx]->prio <= heap->tasks[parent]->prio) {
ptask_t *t = heap->tasks[parent];
heap->tasks[parent] = heap->tasks[idx];
heap->tasks[idx] = t;
sift_up(heap, parent);
}
}
}
/* sift_down()
*/
void sift_down(taskheap_t *heap, int16_t idx)
{
if (idx < heap->ntasks) {
for (int16_t child = heap_d*idx + 1;
child < MULTIQ_TASKS_PER_HEAP && child <= heap_d*idx + heap_d;
++child) {
if (heap->tasks[child]
&& heap->tasks[child]->prio <= heap->tasks[idx]->prio) {
ptask_t *t = heap->tasks[idx];
heap->tasks[idx] = heap->tasks[child];
heap->tasks[child] = t;
sift_down(heap, child);
}
}
}
}
/* multiq_insert()
*/
int multiq_insert(ptask_t *task, int16_t priority)
{
uint64_t rn;
task->prio = priority;
do {
rn = cong(heap_p, cong_unbias, &rngseed);
} while (__atomic_test_and_set(&heaps[rn].lock, __ATOMIC_ACQUIRE));
if (heaps[rn].ntasks >= MULTIQ_TASKS_PER_HEAP) {
LOG_ERR(plog, " heap %llu is full\n", rn);
__atomic_clear(&heaps[rn].lock, __ATOMIC_RELEASE);
return -1;
}
heaps[rn].tasks[heaps[rn].ntasks++] = task;
sift_up(&heaps[rn], heaps[rn].ntasks-1);
__atomic_clear(&heaps[rn].lock, __ATOMIC_RELEASE);
int16_t prio = __atomic_load_n(&heaps[rn].prio, __ATOMIC_SEQ_CST);
if (task->prio < prio)
__atomic_compare_exchange_n(&heaps[rn].prio, &prio, task->prio,
0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
return 0;
}
/* multiq_deletemin()
*/
ptask_t *multiq_deletemin()
{
uint64_t rn1, rn2;
int16_t i, prio1, prio2;
ptask_t *task;
for (i = 0; i < heap_p; ++i) {
rn1 = cong(heap_p, cong_unbias, &rngseed);
rn2 = cong(heap_p, cong_unbias, &rngseed);
prio1 = __atomic_load_n(&heaps[rn1].prio, __ATOMIC_SEQ_CST);
prio2 = __atomic_load_n(&heaps[rn2].prio, __ATOMIC_SEQ_CST);
if (prio1 > prio2) {
prio1 = prio2;
rn1 = rn2;
}
else if (prio1 == prio2 && prio1 == INT16_MAX)
continue;
if (!__atomic_test_and_set(&heaps[rn1].lock, __ATOMIC_ACQUIRE)) {
if (prio1 == heaps[rn1].prio)
break;
__atomic_clear(&heaps[rn1].lock, __ATOMIC_RELEASE);
}
}
if (i == heap_p)
return NULL;
task = heaps[rn1].tasks[0];
heaps[rn1].tasks[0] = heaps[rn1].tasks[--heaps[rn1].ntasks];
heaps[rn1].tasks[heaps[rn1].ntasks] = NULL;
prio1 = INT16_MAX;
if (heaps[rn1].ntasks > 0) {
sift_down(&heaps[rn1], 0);
prio1 = heaps[rn1].tasks[0]->prio;
}
__atomic_store_n(&heaps[rn1].prio, prio1, __ATOMIC_SEQ_CST);
__atomic_clear(&heaps[rn1].lock, __ATOMIC_RELEASE);
return task;
}
/* multiq_minprio()
*/
int16_t multiq_minprio()
{
uint64_t rn1, rn2;
int16_t prio1, prio2;
rn1 = cong(heap_p, cong_unbias, &rngseed);
rn2 = cong(heap_p, cong_unbias, &rngseed);
prio1 = __atomic_load_n(&heaps[rn1].prio, __ATOMIC_SEQ_CST);
prio2 = __atomic_load_n(&heaps[rn2].prio, __ATOMIC_SEQ_CST);
if (prio2 < prio1)
return prio2;
return prio1;
}
/* just_sleep()
*/
static void just_sleep(pthread_mutex_t *lock, pthread_cond_t *wakeup)
{
pthread_mutex_lock(lock);
if (__atomic_load_n(&sleep_check_state, __ATOMIC_SEQ_CST) == sleeping)
pthread_cond_wait(wakeup, lock);
else
pthread_mutex_unlock(lock);
}
/* snapshot_and_sleep()
*/
static void snapshot_and_sleep(pthread_mutex_t *lock, pthread_cond_t *wakeup)
{
uint64_t snapshot_id = cong(UINT64_MAX, UINT64_MAX, &rngseed), previous = -1;
if (!__atomic_compare_exchange_n(&snapshot_owner, &previous, snapshot_id, 0,
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
LOG_ERR(plog, " snapshot has previous owner!\n");
return;
}
int16_t i;
for (i = 0; i < heap_p; ++i) {
if (heaps[i].ntasks != 0)
break;
}
if (i != heap_p) {
LOG_INFO(plog, " heap has tasks, snapshot aborted\n");
return;
}
if (!__atomic_compare_exchange_n(&snapshot_owner, &snapshot_id, previous, 0,
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
LOG_INFO(plog, " snapshot owner changed, snapshot aborted\n");
return;
}
if (!__atomic_compare_exchange_n(&sleep_check_state, (int16_t *)&checking_for_sleeping,
sleeping, 0,
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
LOG_ERR(plog, " sleep aborted at snapshot end\n");
return;
}
just_sleep(lock, wakeup);
}
/* multiq_sleep_if_empty()
*/
void multiq_sleep_if_empty(pthread_mutex_t *lock, pthread_cond_t *wakeup)
{
int16_t state;
sleep_start:
state = __atomic_load_n(&sleep_check_state, __ATOMIC_SEQ_CST);
if (state == checking_for_sleeping) {
for (; ;) {
cpu_pause();
state = __atomic_load_n(&sleep_check_state, __ATOMIC_SEQ_CST);
if (state == not_sleeping)
break;
else if (state == sleeping) {
just_sleep(lock, wakeup);
break;
}
}
}
else if (state == not_sleeping) {
if (!__atomic_compare_exchange_n(&sleep_check_state, (int16_t *)¬_sleeping,
checking_for_sleeping, 0,
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED))
goto sleep_start;
snapshot_and_sleep(lock, wakeup);
if (!__atomic_compare_exchange_n(&sleep_check_state, (int16_t *)&sleeping,
not_sleeping, 0,
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED))
LOG_ERR(plog, " sleep check state update failed\n");
}
else /* state == sleeping */
just_sleep(lock, wakeup);
}