-
Notifications
You must be signed in to change notification settings - Fork 0
/
xrcu.cpp
454 lines (360 loc) · 8.64 KB
/
xrcu.cpp
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
/* Definitions for the RCU API.
This file is part of xrcu.
xrcu 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 "xrcu.hpp"
#include "xatomic.hpp"
#include "version.hpp"
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <functional>
#if defined (__MINGW32__) || defined (__MINGW64__)
#include <pthread.h>
static void tl_set (void *);
#else
# define tl_set(p)
#endif
namespace xrcu
{
struct td_link
{
td_link *next;
td_link *prev;
void init_head ()
{
this->next = this->prev = this;
}
void add (td_link *headp)
{ // Add self to HEADP.
this->next = headp->next;
this->prev = headp;
headp->next->prev = this;
headp->next = this;
}
void del ()
{ // Unlink self.
this->next->prev = this->prev;
this->prev->next = this->next;
}
bool empty_p () const
{
return (this == this->next);
}
bool linked_p () const
{
return (this->next != nullptr);
}
void splice (td_link *dst)
{
if (this->empty_p ())
return;
this->next->prev = dst;
this->prev->next = dst->next;
dst->next->prev = this->prev;
dst->next = this->next;
}
};
static const uintptr_t GP_PHASE_BIT =
(uintptr_t)1 << (sizeof (uintptr_t) * 8 - 1);
static const uintptr_t GP_NEST_MASK = GP_PHASE_BIT - 1;
// Possible states for a reader thread.
enum
{
rd_active,
rd_inactive,
rd_old
};
struct registry
{
std::atomic_uintptr_t counter;
td_link root;
std::mutex td_mtx;
std::mutex gp_mtx;
static const unsigned int QS_ATTEMPTS = 1000;
registry () : counter (1)
{
this->root.init_head ();
}
void add_tdata (td_link *lp)
{
tl_set (lp);
this->td_mtx.lock ();
lp->add (&this->root);
this->td_mtx.unlock ();
}
uintptr_t get_ctr () const
{
return (this->counter.load (std::memory_order_relaxed));
}
void poll_readers (td_link *, td_link *, td_link *);
void sync ();
};
static registry global_reg;
// Maximum number of pending finalizers before flushing.
#ifndef XRCU_MAX_FINS
# define XRCU_MAX_FINS 1000
#endif
static const unsigned int MAX_FINS = XRCU_MAX_FINS;
struct tl_data : public td_link
{
bool must_flush;
unsigned int n_fins;
std::atomic_uintptr_t counter;
size_t xrand_val;
finalizable *fin_objs;
uintptr_t get_ctr () const
{
return (this->counter.load (std::memory_order_relaxed));
}
int state () const
{
auto val = this->counter.load (std::memory_order_acquire);
if (!(val & GP_NEST_MASK))
return (rd_inactive);
else if (!((val ^ global_reg.get_ctr ()) & GP_PHASE_BIT))
return (rd_active);
else
return (rd_old);
}
bool in_cs () const
{
return ((this->get_ctr () & GP_NEST_MASK) != 0);
}
bool flush_all ()
{
if (!sync ())
return (false);
for (auto f = this->fin_objs; f != nullptr; )
{
auto next = f->_Fin_next;
f->safe_destroy ();
f = next;
}
this->fin_objs = nullptr;
this->n_fins = 0;
this->must_flush = false;
return (true);
}
void finalize (finalizable *finp)
{
finp->_Fin_next = this->fin_objs;
this->fin_objs = finp;
if (++this->n_fins < MAX_FINS)
;
else if (!this->flush_all ())
/* Couldn't reclaim memory since we are in a critical section.
* Set the flag to do it ASAP. */
this->must_flush = true;
}
~tl_data ()
{
if (!this->linked_p ())
return;
this->counter.store (0, std::memory_order_release);
if (this->n_fins > 0)
this->flush_all ();
global_reg.td_mtx.lock ();
this->del ();
global_reg.td_mtx.unlock ();
}
};
#if defined (__MINGW32__) || defined (__MINGW64__)
// Mingw has problems with thread_local destructors.
struct key_handler
{
pthread_key_t key;
static void fini (void *ptr)
{
((tl_data *)ptr)->~tl_data ();
}
key_handler ()
{
if (pthread_key_create (&this->key, key_handler::fini) != 0)
throw "failed to create thread key";
}
void set (void *ptr)
{
pthread_setspecific (this->key, ptr);
}
};
struct alignas (alignof (tl_data)) tl_buf
{
unsigned char data[sizeof (tl_data)];
};
static thread_local tl_buf tlbuf;
#define tldata (*(tl_data *)&tlbuf)
#else
static thread_local tl_data tldata {};
#endif
static inline tl_data*
local_data ()
{
auto self = &tldata;
if (!self->linked_p ())
global_reg.add_tdata (self);
return (self);
}
void enter_cs ()
{
auto self = local_data ();
auto val = self->get_ctr ();
val = (val & GP_NEST_MASK) == 0 ? global_reg.get_ctr () : val + 1;
self->counter.store (val, std::memory_order_release);
}
void exit_cs ()
{
auto self = local_data ();
auto val = self->get_ctr ();
self->counter.store (val - 1, std::memory_order_release);
if (self->must_flush && !self->in_cs ())
self->flush_all ();
}
bool in_cs ()
{
auto self = &tldata;
return (self->linked_p () && self->in_cs ());
}
void registry::poll_readers (td_link *readers, td_link *outp, td_link *qsp)
{
for (unsigned int loops = 0 ; ; ++loops)
{
td_link *next, *runp = readers->next;
for (; runp != readers; runp = next)
{
next = runp->next;
switch (((tl_data *)runp)->state ())
{
case rd_active:
if (outp != nullptr)
{
runp->del ();
runp->add (outp);
break;
}
// Fallthrough.
case rd_inactive:
runp->del ();
runp->add (qsp);
break;
default:
break;
}
}
if (readers->empty_p ())
break;
this->td_mtx.unlock ();
if (loops < QS_ATTEMPTS)
xatomic_spin_nop ();
else
std::this_thread::sleep_for (std::chrono::milliseconds (1));
this->td_mtx.lock ();
}
}
void registry::sync ()
{
this->gp_mtx.lock ();
this->td_mtx.lock ();
if (this->root.empty_p ())
{
this->td_mtx.unlock ();
this->gp_mtx.unlock ();
return;
}
td_link out, qs;
qs.init_head ();
out.init_head ();
std::atomic_thread_fence (std::memory_order_acq_rel);
poll_readers (&this->root, &out, &qs);
this->counter.store (this->get_ctr () ^ GP_PHASE_BIT,
std::memory_order_relaxed);
poll_readers (&out, nullptr, &qs);
qs.splice (&this->root);
this->td_mtx.unlock ();
this->gp_mtx.unlock ();
}
bool sync ()
{
if (in_cs ())
return (false);
global_reg.sync ();
return (true);
}
void finalize (finalizable *finp)
{
if (finp)
local_data()->finalize (finp);
}
bool flush_finalizers ()
{
auto tld = local_data ();
bool ret = tld->flush_all ();
if (!ret)
tld->must_flush = true;
return (ret);
}
unsigned int xrand ()
{
auto self = &tldata; // Avoid local_data ()
if (!self->xrand_val)
self->xrand_val = (unsigned int)(time (nullptr) ^
std::hash<std::thread::id>() (std::this_thread::get_id ()));
self->xrand_val = self->xrand_val * 1103515245 + 12345;
return (self->xrand_val >> 16);
}
static void
atfork_prepare ()
{
global_reg.gp_mtx.lock ();
global_reg.td_mtx.lock ();
}
static void
atfork_parent ()
{
global_reg.td_mtx.unlock ();
global_reg.gp_mtx.unlock ();
}
static void
atfork_child ()
{
atfork_parent ();
// Reset the registry
global_reg.root.init_head ();
auto self = &tldata;
if (!self->linked_p ())
return;
// Manually add ourselves to the registry without locking.
self->add (&global_reg.root);
}
atfork atfork_data ()
{
atfork ret;
ret.prepare = atfork_prepare;
ret.parent = atfork_parent;
ret.child = atfork_child;
return (ret);
}
void library_version (int& major, int& minor)
{
major = MAJOR, minor = MINOR;
}
} // namespace rcu
#if defined (__MINGW32__) || defined (__MINGW64__)
static void
tl_set (void *ptr)
{
static xrcu::key_handler handler;
handler.set (ptr);
}
#endif