-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlock-free-alloc.c
596 lines (505 loc) · 16.3 KB
/
lock-free-alloc.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* lock-free-alloc.c: Lock free allocator.
*
* (C) Copyright 2011 Novell, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This is a simplified version of the lock-free allocator described in
*
* Scalable Lock-Free Dynamic Memory Allocation
* Maged M. Michael, PLDI 2004
*
* I could not get Michael's allocator working bug free under heavy
* stress tests. The paper doesn't provide correctness proof and after
* failing to formalize the ownership of descriptors I devised this
* simpler allocator.
*
* Allocation within superblocks proceeds exactly like in Michael's
* allocator. The simplification is that a thread has to "acquire" a
* descriptor before it can allocate from its superblock. While it owns
* the descriptor no other thread can acquire and hence allocate from
* it. A consequence of this is that the ABA problem cannot occur, so
* we don't need the tag field and don't have to use 64 bit CAS.
*
* Descriptors are stored in two locations: The partial queue and the
* active field. They can only be in at most one of those at one time.
* If a thread wants to allocate, it needs to get a descriptor. It
* tries the active descriptor first, CASing it to NULL. If that
* doesn't work, it gets a descriptor out of the partial queue. Once it
* has the descriptor it owns it because it is not referenced anymore.
* It allocates a slot and then gives the descriptor back (unless it is
* FULL).
*
* Note that it is still possible that a slot is freed while an
* allocation is in progress from the same superblock. Ownership in
* this case is not complicated, though. If the block was FULL and the
* free set it to PARTIAL, the free now owns the block (because FULL
* blocks are not referenced from partial and active) and has to give it
* back. If the block was PARTIAL then the free doesn't own the block
* (because it's either still referenced, or an alloc owns it). A
* special case of this is that it has changed from PARTIAL to EMPTY and
* now needs to be retired. Technically, the free wouldn't have to do
* anything in this case because the first thing an alloc does when it
* gets ownership of a descriptor is to check whether it is EMPTY and
* retire it if that is the case. As an optimization, our free does try
* to acquire the descriptor (by CASing the active field, which, if it
* is lucky, points to that descriptor) and if it can do so, retire it.
* If it can't, it tries to retire other descriptors from the partial
* queue, so that we can be sure that even if no more allocations
* happen, descriptors are still retired. This is analogous to what
* Michael's allocator does.
*
* Another difference to Michael's allocator is not related to
* concurrency, however: We don't point from slots to descriptors.
* Instead we allocate superblocks aligned and point from the start of
* the superblock to the descriptor, so we only need one word of
* metadata per superblock.
*
* FIXME: Having more than one allocator per size class is probably
* buggy because it was never tested.
*/
#include "fake-glib.h"
#include <stdlib.h>
#include "mono-mmap.h"
#include "mono-membar.h"
#include "hazard-pointer.h"
#include "atomic.h"
#include "lock-free-queue.h"
#include "sgen-gc.h"
#include "lock-free-alloc.h"
//#define DESC_AVAIL_DUMMY
enum {
STATE_FULL,
STATE_PARTIAL,
STATE_EMPTY
};
typedef union {
gint32 value;
struct {
guint32 avail : 15;
guint32 count : 15;
guint32 state : 2;
} data;
} Anchor;
typedef struct _MonoLockFreeAllocDescriptor Descriptor;
struct _MonoLockFreeAllocDescriptor {
MonoLockFreeQueueNode node;
MonoLockFreeAllocator *heap;
volatile Anchor anchor;
unsigned int slot_size;
unsigned int max_count;
gpointer sb;
#ifndef DESC_AVAIL_DUMMY
Descriptor * volatile next;
#endif
gboolean in_use; /* used for debugging only */
};
#define NUM_DESC_BATCH 64
#define SB_SIZE 16384
#define SB_HEADER_SIZE 16
#define SB_USABLE_SIZE (SB_SIZE - SB_HEADER_SIZE)
#define SB_HEADER_FOR_ADDR(a) ((gpointer)((gulong)(a) & ~(gulong)(SB_SIZE-1)))
#define DESCRIPTOR_FOR_ADDR(a) (*(Descriptor**)SB_HEADER_FOR_ADDR (a))
static gpointer
alloc_sb (Descriptor *desc)
{
gpointer sb_header = mono_sgen_alloc_os_memory_aligned (SB_SIZE, SB_SIZE, TRUE);
g_assert (sb_header == SB_HEADER_FOR_ADDR (sb_header));
DESCRIPTOR_FOR_ADDR (sb_header) = desc;
//g_print ("sb %p for %p\n", sb_header, desc);
return (char*)sb_header + SB_HEADER_SIZE;
}
static void
free_sb (gpointer sb)
{
gpointer sb_header = SB_HEADER_FOR_ADDR (sb);
g_assert ((char*)sb_header + SB_HEADER_SIZE == sb);
mono_sgen_free_os_memory (sb_header, SB_SIZE);
//g_print ("free sb %p\n", sb_header);
}
#ifndef DESC_AVAIL_DUMMY
static Descriptor * volatile desc_avail;
static Descriptor*
desc_alloc (void)
{
MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
Descriptor *desc;
for (;;) {
gboolean success;
desc = get_hazardous_pointer ((gpointer * volatile)&desc_avail, hp, 1);
if (desc) {
Descriptor *next = desc->next;
success = (InterlockedCompareExchangePointer ((gpointer * volatile)&desc_avail, next, desc) == desc);
} else {
size_t desc_size = sizeof (Descriptor);
Descriptor *d;
int i;
desc = mono_sgen_alloc_os_memory (desc_size * NUM_DESC_BATCH, TRUE);
/* Organize into linked list. */
d = desc;
for (i = 0; i < NUM_DESC_BATCH; ++i) {
Descriptor *next = (i == (NUM_DESC_BATCH - 1)) ? NULL : (Descriptor*)((char*)desc + ((i + 1) * desc_size));
d->next = next;
mono_lock_free_queue_node_init (&d->node, TRUE);
d = next;
}
mono_memory_write_barrier ();
success = (InterlockedCompareExchangePointer ((gpointer * volatile)&desc_avail, desc->next, NULL) == NULL);
if (!success)
mono_sgen_free_os_memory (desc, desc_size * NUM_DESC_BATCH);
}
mono_hazard_pointer_clear (hp, 1);
if (success)
break;
}
g_assert (!desc->in_use);
desc->in_use = TRUE;
return desc;
}
static void
desc_enqueue_avail (gpointer _desc)
{
Descriptor *desc = _desc;
Descriptor *old_head;
g_assert (desc->anchor.data.state == STATE_EMPTY);
g_assert (!desc->in_use);
do {
old_head = desc_avail;
desc->next = old_head;
mono_memory_write_barrier ();
} while (InterlockedCompareExchangePointer ((gpointer * volatile)&desc_avail, desc, old_head) != old_head);
}
static void
desc_retire (Descriptor *desc)
{
g_assert (desc->anchor.data.state == STATE_EMPTY);
g_assert (desc->in_use);
desc->in_use = FALSE;
free_sb (desc->sb);
mono_thread_hazardous_free_or_queue (desc, desc_enqueue_avail, FALSE, TRUE);
}
#else
MonoLockFreeQueue available_descs;
static Descriptor*
desc_alloc (void)
{
Descriptor *desc = (Descriptor*)mono_lock_free_queue_dequeue (&available_descs);
if (desc)
return desc;
return calloc (1, sizeof (Descriptor));
}
static void
desc_retire (Descriptor *desc)
{
free_sb (desc->sb);
mono_lock_free_queue_enqueue (&available_descs, &desc->node);
}
#endif
static Descriptor*
list_get_partial (MonoLockFreeAllocSizeClass *sc)
{
for (;;) {
Descriptor *desc = (Descriptor*) mono_lock_free_queue_dequeue (&sc->partial);
if (!desc)
return NULL;
if (desc->anchor.data.state != STATE_EMPTY)
return desc;
desc_retire (desc);
}
}
static void
desc_put_partial (gpointer _desc)
{
Descriptor *desc = _desc;
g_assert (desc->anchor.data.state != STATE_FULL);
mono_lock_free_queue_node_free (&desc->node);
mono_lock_free_queue_enqueue (&desc->heap->sc->partial, &desc->node);
}
static void
list_put_partial (Descriptor *desc)
{
g_assert (desc->anchor.data.state != STATE_FULL);
mono_thread_hazardous_free_or_queue (desc, desc_put_partial, FALSE, TRUE);
}
static void
list_remove_empty_desc (MonoLockFreeAllocSizeClass *sc)
{
int num_non_empty = 0;
for (;;) {
Descriptor *desc = (Descriptor*) mono_lock_free_queue_dequeue (&sc->partial);
if (!desc)
return;
/*
* We don't need to read atomically because we're the
* only thread that references this descriptor.
*/
if (desc->anchor.data.state == STATE_EMPTY) {
desc_retire (desc);
} else {
g_assert (desc->heap->sc == sc);
mono_thread_hazardous_free_or_queue (desc, desc_put_partial, FALSE, TRUE);
if (++num_non_empty >= 2)
return;
}
}
}
static Descriptor*
heap_get_partial (MonoLockFreeAllocator *heap)
{
return list_get_partial (heap->sc);
}
static void
heap_put_partial (Descriptor *desc)
{
list_put_partial (desc);
}
static gboolean
set_anchor (Descriptor *desc, Anchor old_anchor, Anchor new_anchor)
{
if (old_anchor.data.state == STATE_EMPTY)
g_assert (new_anchor.data.state == STATE_EMPTY);
return InterlockedCompareExchange (&desc->anchor.value, new_anchor.value, old_anchor.value) == old_anchor.value;
}
static gpointer
alloc_from_active_or_partial (MonoLockFreeAllocator *heap)
{
Descriptor *desc;
Anchor old_anchor, new_anchor;
gpointer addr;
retry:
desc = heap->active;
if (desc) {
if (InterlockedCompareExchangePointer ((gpointer * volatile)&heap->active, NULL, desc) != desc)
goto retry;
} else {
desc = heap_get_partial (heap);
if (!desc)
return NULL;
}
/* Now we own the desc. */
do {
unsigned int next;
new_anchor = old_anchor = *(volatile Anchor*)&desc->anchor.value;
if (old_anchor.data.state == STATE_EMPTY) {
/* We must free it because we own it. */
desc_retire (desc);
goto retry;
}
g_assert (old_anchor.data.state == STATE_PARTIAL);
g_assert (old_anchor.data.count > 0);
addr = (char*)desc->sb + old_anchor.data.avail * desc->slot_size;
mono_memory_read_barrier ();
next = *(unsigned int*)addr;
g_assert (next < SB_USABLE_SIZE / desc->slot_size);
new_anchor.data.avail = next;
--new_anchor.data.count;
if (new_anchor.data.count == 0)
new_anchor.data.state = STATE_FULL;
} while (!set_anchor (desc, old_anchor, new_anchor));
/* If the desc is partial we have to give it back. */
if (new_anchor.data.state == STATE_PARTIAL) {
if (InterlockedCompareExchangePointer ((gpointer * volatile)&heap->active, desc, NULL) != NULL)
heap_put_partial (desc);
}
return addr;
}
static gpointer
alloc_from_new_sb (MonoLockFreeAllocator *heap)
{
unsigned int slot_size, count, i;
Descriptor *desc = desc_alloc ();
desc->sb = alloc_sb (desc);
slot_size = desc->slot_size = heap->sc->slot_size;
count = SB_USABLE_SIZE / slot_size;
/* Organize blocks into linked list. */
for (i = 1; i < count - 1; ++i)
*(unsigned int*)((char*)desc->sb + i * slot_size) = i + 1;
desc->heap = heap;
/*
* Setting avail to 1 because 0 is the block we're allocating
* right away.
*/
desc->anchor.data.avail = 1;
desc->slot_size = heap->sc->slot_size;
desc->max_count = count;
desc->anchor.data.count = desc->max_count - 1;
desc->anchor.data.state = STATE_PARTIAL;
mono_memory_write_barrier ();
/* Make it active or free it again. */
if (InterlockedCompareExchangePointer ((gpointer * volatile)&heap->active, desc, NULL) == NULL) {
return desc->sb;
} else {
desc->anchor.data.state = STATE_EMPTY;
desc_retire (desc);
return NULL;
}
}
gpointer
mono_lock_free_alloc (MonoLockFreeAllocator *heap)
{
gpointer addr;
for (;;) {
addr = alloc_from_active_or_partial (heap);
if (addr)
break;
addr = alloc_from_new_sb (heap);
if (addr)
break;
}
return addr;
}
void
mono_lock_free_free (gpointer ptr)
{
Anchor old_anchor, new_anchor;
Descriptor *desc;
gpointer sb;
MonoLockFreeAllocator *heap = NULL;
desc = DESCRIPTOR_FOR_ADDR (ptr);
sb = desc->sb;
g_assert (SB_HEADER_FOR_ADDR (ptr) == SB_HEADER_FOR_ADDR (sb));
do {
new_anchor = old_anchor = *(volatile Anchor*)&desc->anchor.value;
*(unsigned int*)ptr = old_anchor.data.avail;
new_anchor.data.avail = ((char*)ptr - (char*)sb) / desc->slot_size;
g_assert (new_anchor.data.avail < SB_USABLE_SIZE / desc->slot_size);
if (old_anchor.data.state == STATE_FULL)
new_anchor.data.state = STATE_PARTIAL;
if (++new_anchor.data.count == desc->max_count) {
heap = desc->heap;
new_anchor.data.state = STATE_EMPTY;
}
} while (!set_anchor (desc, old_anchor, new_anchor));
if (new_anchor.data.state == STATE_EMPTY) {
g_assert (old_anchor.data.state != STATE_EMPTY);
if (InterlockedCompareExchangePointer ((gpointer * volatile)&heap->active, NULL, desc) == desc) {
/* We own it, so we free it. */
desc_retire (desc);
} else {
/*
* Somebody else must free it, so we do some
* freeing for others.
*/
list_remove_empty_desc (heap->sc);
}
} else if (old_anchor.data.state == STATE_FULL) {
/*
* Nobody owned it, now we do, so we need to give it
* back.
*/
g_assert (new_anchor.data.state == STATE_PARTIAL);
if (InterlockedCompareExchangePointer ((gpointer * volatile)&desc->heap->active, desc, NULL) != NULL)
heap_put_partial (desc);
}
}
#define g_assert_OR_PRINT(c, format, ...) do { \
if (!(c)) { \
if (print) \
g_print ((format), ## __VA_ARGS__); \
else \
g_assert (FALSE); \
} \
} while (0)
static void
descriptor_check_consistency (Descriptor *desc, gboolean print)
{
int count = desc->anchor.data.count;
int max_count = SB_USABLE_SIZE / desc->slot_size;
#if _MSC_VER
gboolean* linked = alloca(max_count*sizeof(gboolean));
#else
gboolean linked [max_count];
#endif
int i, last;
unsigned int index;
#ifndef DESC_AVAIL_DUMMY
Descriptor *avail;
for (avail = desc_avail; avail; avail = avail->next)
g_assert_OR_PRINT (desc != avail, "descriptor is in the available list\n");
#endif
g_assert_OR_PRINT (desc->slot_size == desc->heap->sc->slot_size, "slot size doesn't match size class\n");
if (print)
g_print ("descriptor %p is ", desc);
switch (desc->anchor.data.state) {
case STATE_FULL:
if (print)
g_print ("full\n");
g_assert_OR_PRINT (count == 0, "count is not zero: %d\n", count);
break;
case STATE_PARTIAL:
if (print)
g_print ("partial\n");
g_assert_OR_PRINT (count < max_count, "count too high: is %d but must be below %d\n", count, max_count);
break;
case STATE_EMPTY:
if (print)
g_print ("empty\n");
g_assert_OR_PRINT (count == max_count, "count is wrong: is %d but should be %d\n", count, max_count);
break;
default:
g_assert_OR_PRINT (FALSE, "invalid state\n");
}
for (i = 0; i < max_count; ++i)
linked [i] = FALSE;
index = desc->anchor.data.avail;
last = -1;
for (i = 0; i < count; ++i) {
gpointer addr = (char*)desc->sb + index * desc->slot_size;
g_assert_OR_PRINT (index >= 0 && index < max_count,
"index %d for %dth available slot, linked from %d, not in range [0 .. %d)\n",
index, i, last, max_count);
g_assert_OR_PRINT (!linked [index], "%dth available slot %d linked twice\n", i, index);
if (linked [index])
break;
linked [index] = TRUE;
last = index;
index = *(unsigned int*)addr;
}
}
gboolean
mono_lock_free_allocator_check_consistency (MonoLockFreeAllocator *heap)
{
Descriptor *active = heap->active;
Descriptor *desc;
if (active) {
g_assert (active->anchor.data.state == STATE_PARTIAL);
descriptor_check_consistency (active, FALSE);
}
while ((desc = (Descriptor*)mono_lock_free_queue_dequeue (&heap->sc->partial))) {
g_assert (desc->anchor.data.state == STATE_PARTIAL || desc->anchor.data.state == STATE_EMPTY);
descriptor_check_consistency (desc, FALSE);
}
return TRUE;
}
void
mono_lock_free_allocator_init_size_class (MonoLockFreeAllocSizeClass *sc, unsigned int slot_size)
{
g_assert (slot_size <= SB_USABLE_SIZE / 2);
mono_lock_free_queue_init (&sc->partial);
sc->slot_size = slot_size;
}
void
mono_lock_free_allocator_init_allocator (MonoLockFreeAllocator *heap, MonoLockFreeAllocSizeClass *sc)
{
heap->sc = sc;
heap->active = NULL;
}