-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsref.c
More file actions
624 lines (519 loc) · 13.4 KB
/
sref.c
File metadata and controls
624 lines (519 loc) · 13.4 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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* Definitions for the sref API.
This file is part of libsref.
libsref is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include "sref.h"
#include "compat.h"
#include "version.h"
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
typedef struct
{
void *ptr;
intptr_t delta;
} SrefDelta;
#ifndef SREF_NDELTAS
# define SREF_NDELTAS 128
#endif
#if (SREF_NDELTAS & (SREF_NDELTAS - 1)) != 0
# error "number of deltas must be a power of 2"
#endif
#ifndef SREF_NMAXOPS
# define SREF_NMAXOPS 1024
#endif
/* Mapping of pointers to deltas. */
typedef struct
{
SrefDelta deltas[SREF_NDELTAS];
unsigned int n_used;
} SrefTable;
static int
sref_add (SrefTable *tp, void *ptr, intptr_t add, uintptr_t *outp)
{
uintptr_t idx = ((uintptr_t)ptr >> 3) % SREF_NDELTAS;
uintptr_t nprobe = 1;
assert (tp->n_used < SREF_NDELTAS);
for ( ; ; ++nprobe)
{
SrefDelta *dp = tp->deltas + idx;
if (!dp->ptr)
{
dp->ptr = ptr;
dp->delta = add;
*outp = idx;
return (++tp->n_used * 100 >= SREF_NDELTAS * 75);
}
else if (dp->ptr == ptr)
{
dp->delta += add;
return (0);
}
idx = (idx + nprobe) % SREF_NDELTAS;
}
}
static void
sref_merge (SrefTable *dst, SrefTable *src)
{
uintptr_t idx;
for (unsigned int i = 0, j = 0; j < src->n_used; ++i)
{
SrefDelta *dp = src->deltas + i;
if (!dp->ptr)
continue;
int rv = sref_add (dst, dp->ptr, dp->delta, &idx);
dp->ptr = NULL;
--src->n_used;
if (rv)
break;
}
}
typedef struct Dlist
{
struct Dlist *prev;
struct Dlist *next;
} Dlist;
static inline void
dlist_init_head (Dlist *dlp)
{
dlp->next = dlp->prev = dlp;
}
static inline void
dlist_add (Dlist *head, Dlist *node)
{
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
static inline void
dlist_del (Dlist *node)
{
node->next->prev = node->prev;
node->prev->next = node->next;
}
static inline int
dlist_empty_p (const Dlist *head)
{
return (head == head->next);
}
static inline int
dlist_linked_p (const Dlist *node)
{
return (node->next != NULL);
}
static void
dlist_splice (Dlist *head, Dlist *dst)
{
if (dlist_empty_p (head))
return;
head->next->prev = dst;
head->prev->next = dst->next;
dst->next->prev = head->prev;
dst->next = head->next;
}
/*
* Thread registry.
*
* Once a thread uses the sref API, it's lazily added to the registry, and
* will be inspected when a grace period elapses.
*
* The registry has 2 locks: One to serialize a thread being registered,
* and one to serialize thread processing on each grace period.
*/
static const uintptr_t GP_PHASE_BIT = 1;
typedef struct
{
uintptr_t counter;
Dlist root;
Sref *review;
xmutex_t td_lock;
xmutex_t gp_lock;
} SrefRegistry;
typedef struct
{
SrefTable refs;
SrefTable unrefs;
int flush;
} SrefCache;
/* Global variables initialized in 'sref_init'. */
static SrefRegistry registry;
static xkey_t reg_key;
/*
* Thread data.
*
* Each thread maintains 2 tables: One for reference count increments,
* and another for decrements. They are separated so that checks for liveness -
* i.e: refcount != 0 - can be made only when decrementing instead of every
* time.
*
* We further need 2 sets of these tables so that acquiring and releasing an
* sref pointer can be done concurrently with registry synchronization that
* occurs at a different window.
*/
typedef struct
{
Dlist link;
uintptr_t counter;
uintptr_t n_ops;
SrefCache cache[2];
} SrefData;
/* Thread-specific descriptor for sref operations. */
static xthread_local SrefData local_data;
static void
registry_add (SrefRegistry *regp, SrefData *dp)
{
xkey_set (reg_key, dp);
xmutex_lock (®p->td_lock);
dlist_add (®p->root, &dp->link);
xmutex_unlock (®p->td_lock);
}
static uintptr_t
registry_counter (void)
{
return (xatomic_load_rlx (®istry.counter));
}
static SrefData*
sref_local (void)
{
SrefData *ret = &local_data;
if (!dlist_linked_p (&ret->link))
registry_add (®istry, ret);
return (ret);
}
static uintptr_t
local_counter (const SrefData *dp)
{
return (xatomic_load_rlx (&dp->counter));
}
#define sref_table_process(table, dec) \
do \
{ \
for (unsigned int i = 0, j = 0; j < (table)->n_used; ++i) \
{ \
SrefDelta *dep = &(table)->deltas[i]; \
if (!dep->ptr) \
continue; \
\
Sref *p = (Sref *)dep->ptr; \
p->refcnt += dep->delta; \
assert (p->refcnt >= 0); \
if (dec && !p->refcnt && p->fini) \
p->fini (p); \
\
dep->ptr = NULL; \
dep->delta = 0; \
++j; \
} \
\
(table)->n_used = 0; \
} \
while (0)
static void
sref_process_inc (SrefData *dp, uintptr_t idx)
{
sref_table_process (&dp->cache[idx].refs, 0);
}
static void
sref_process_dec (SrefData *dp, uintptr_t idx)
{
sref_table_process (&dp->cache[idx].unrefs, 1);
}
#undef sref_table_process
#define STATE_ACTIVE 0
#define STATE_INACTIVE 1
#define STATE_OLD 2
static inline int
local_state (SrefData *dp)
{
uintptr_t val = xatomic_load_acq (&dp->counter);
if (!(val >> GP_PHASE_BIT))
return (STATE_INACTIVE);
else if (!((val ^ registry_counter ()) & GP_PHASE_BIT))
return (STATE_ACTIVE);
else
return (STATE_OLD);
}
static void
registry_poll (SrefRegistry *regp, Dlist *readers, Dlist *outp, Dlist *qsp)
{
for (unsigned int loops = 0 ; ; ++loops)
{
Dlist *next, *runp = readers->next;
for (; runp != readers; runp = next)
{
next = runp->next;
switch (local_state ((SrefData *)runp))
{
case STATE_ACTIVE:
if (outp)
{
dlist_del (runp);
dlist_add (outp, runp);
break;
}
/* FALLTHROUGH. */
case STATE_INACTIVE:
dlist_del (runp);
dlist_add (qsp, runp);
break;
case STATE_OLD:
break;
default:
assert ("invalid state");
}
}
if (dlist_empty_p (readers))
break;
xmutex_unlock (®p->td_lock);
if (loops < 1000)
xatomic_mfence_acq ();
else
{
xthread_sleep (1);
loops = 0;
}
xmutex_lock (®p->td_lock);
}
}
static void
registry_lock (SrefRegistry *rp)
{
xmutex_lock (&rp->gp_lock);
xmutex_lock (&rp->td_lock);
}
static void
registry_unlock (SrefRegistry *rp)
{
xmutex_unlock (&rp->td_lock);
xmutex_unlock (&rp->gp_lock);
}
static void
registry_sync (int acquire)
{
SrefRegistry *rp = ®istry;
if (acquire)
registry_lock (rp);
if (dlist_empty_p (&rp->root))
{
if (acquire)
registry_unlock (rp);
return;
}
Dlist out, qs;
dlist_init_head (&qs);
dlist_init_head (&out);
xatomic_mfence_full ();
registry_poll (rp, &rp->root, &out, &qs);
uintptr_t prev_idx = xatomic_load_rlx (&rp->counter);
xatomic_store_rel (&rp->counter, prev_idx ^ GP_PHASE_BIT);
registry_poll (rp, &out, NULL, &qs);
dlist_splice (&qs, &rp->root);
/* Now process increments first, and then decrements, after checking
* for any object whose refcount is zero, so that it's destroyed timely. */
for (Dlist *qp = rp->root.next; qp != &rp->root; qp = qp->next)
sref_process_inc ((SrefData *)qp, prev_idx);
for (Dlist *qp = rp->root.next; qp != &rp->root; qp = qp->next)
sref_process_dec ((SrefData *)qp, prev_idx);
for (Sref *sp = rp->review; sp ; )
{
Sref *next = sp->next;
if (sp->refcnt)
/* Still live - Clear the review link. */
sp->next = NULL;
else
sp->fini (sp);
sp = next;
}
rp->review = NULL;
if (acquire)
registry_unlock (rp);
}
void sref_read_enter (void)
{
SrefData *self = sref_local ();
uintptr_t value = local_counter (self);
if (!(value >> GP_PHASE_BIT))
{ /* A grace period has elapsed, so we can reset the 'flush' flag. */
value = registry_counter ();
self->cache[value & GP_PHASE_BIT].flush = 0;
self->n_ops = 0;
}
uintptr_t nval = value + (1 << GP_PHASE_BIT);
assert (nval > value);
xatomic_store_rel (&self->counter, nval);
}
static int
sref_flush_impl (SrefData *self, uintptr_t value)
{
if (value >> GP_PHASE_BIT)
/* We are currently in a critical section, and can't flush our deltas. */
return (-1);
self->cache[value & GP_PHASE_BIT].flush = 0;
self->n_ops = 0;
registry_sync (1);
return (0);
}
void sref_read_exit (void)
{
SrefData *self = sref_local ();
uintptr_t value = local_counter (self);
assert (value >= (1 << GP_PHASE_BIT));
value -= 1 << GP_PHASE_BIT;
xatomic_store_rel (&self->counter, value);
if (self->cache[value & GP_PHASE_BIT].flush)
sref_flush_impl (self, value);
}
static void
sref_update_nops (SrefData *self, SrefCache *cache)
{
if (++self->n_ops >= SREF_NMAXOPS && cache->flush < 2)
++cache->flush;
}
static void
sref_acq_rel (void *refptr, intptr_t delta, size_t off)
{
assert (refptr);
SrefData *self = sref_local ();
uintptr_t idx = registry_counter () & GP_PHASE_BIT;
SrefCache *cache = &self->cache[idx];
SrefTable *tp = (SrefTable *)((char *)cache + off);
sref_update_nops (self, cache);
cache->flush += sref_add (tp, refptr, delta, &idx);
if (cache->flush > 1 && sref_flush_impl (self, local_counter (self)) < 0)
{ /* This is an emergency situation. Our cache is full, and we are inside
* a read-side critical section, and thus can't flush deltas. So we have
* to resort to adding this sref pointer to the review list. We use the
* thread registry lock to act as a serialization barrier. */
Sref *sp = (Sref *)refptr;
assert (sp == tp->deltas[idx].ptr);
tp->deltas[idx].ptr = NULL;
tp->deltas[idx].delta = 0;
--tp->n_used;
xmutex_lock (®istry.td_lock);
sp->refcnt += delta;
if (!sp->next)
{
sp->next = registry.review;
registry.review = sp;
}
xmutex_unlock (®istry.td_lock);
}
}
void* sref_acquire (void *refptr)
{
sref_acq_rel (refptr, +1, offsetof (SrefCache, refs));
return (refptr);
}
void sref_release (void *refptr)
{
sref_acq_rel (refptr, -1, offsetof (SrefCache, unrefs));
}
int sref_flush (void)
{
SrefData *self = sref_local ();
uintptr_t value = local_counter (self);
int ret = sref_flush_impl (self, value);
if (ret < 0)
/* If we didn't manage to flush, set the flag to do it ASAP. */
self->cache[value & GP_PHASE_BIT].flush = 1;
return (ret);
}
#ifndef XKEY_ARG
# define XKEY_ARG(arg) arg
# define XKEY_LOCAL(x, y) y
#endif
static void
sref_data_fini (XKEY_ARG (void *ptr))
{
SrefData *self = XKEY_LOCAL (&local_data, ptr);
if (!dlist_linked_p (&self->link))
return;
xatomic_store_rel (&self->counter, 0);
registry_lock (®istry);
uintptr_t idx = registry_counter () & GP_PHASE_BIT;
SrefCache *cache = self->cache;
sref_merge (&cache[idx].refs, &cache[idx ^ GP_PHASE_BIT].refs);
sref_merge (&cache[idx].unrefs, &cache[idx ^ GP_PHASE_BIT].unrefs);
if (cache[idx].refs.n_used || cache[idx].unrefs.n_used)
registry_sync (0);
idx ^= GP_PHASE_BIT;
if (cache[idx].refs.n_used || cache[idx].unrefs.n_used)
registry_sync (0);
dlist_del (&self->link);
registry_unlock (®istry);
}
static void
sref_atexit (void)
{
sref_data_fini (XKEY_ARG (&local_data)); /* avoid sref_local. */
}
static int sref_initialized;
int sref_lib_init (void)
{
if (sref_initialized)
return (0);
else if (xkey_create (®_key, sref_data_fini) < 0)
return (-1);
else if (xmutex_init (®istry.td_lock) < 0)
{
xkey_delete (reg_key);
return (-1);
}
else if (xmutex_init (®istry.gp_lock) < 0)
{
xkey_delete (reg_key);
xmutex_destroy (®istry.td_lock);
return (-1);
}
else if (atexit (sref_atexit) != 0)
{
xkey_delete (reg_key);
xmutex_destroy (®istry.td_lock);
xmutex_destroy (®istry.gp_lock);
return (-1);
}
dlist_init_head (®istry.root);
sref_initialized = 1;
return (0);
}
static void
sref_atfork_prepare (void)
{
registry_lock (®istry);
}
static void
sref_atfork_parent (void)
{
registry_unlock (®istry);
}
static void
sref_atfork_child (void)
{
registry_unlock (®istry);
dlist_init_head (®istry.root);
SrefData *self = &local_data;
if (dlist_linked_p (&self->link))
dlist_add (®istry.root, &self->link);
}
SrefAtFork sref_atfork (void)
{
SrefAtFork ret;
ret.prepare = sref_atfork_prepare;
ret.parent = sref_atfork_parent;
ret.child = sref_atfork_child;
return (ret);
}
void sref_lib_version (int *major, int *minor)
{
*major = MAJOR;
*minor = MINOR;
}