]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovs-thread.c
1a463a727b2be36d661f3410184fa2c75cd4a8a6
[mirror_ovs.git] / lib / ovs-thread.c
1 /*
2 * Copyright (c) 2013, 2014, 2015, 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "ovs-thread.h"
19 #include <errno.h>
20 #include <poll.h>
21 #ifndef _WIN32
22 #include <signal.h>
23 #endif
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include "compiler.h"
27 #include "fatal-signal.h"
28 #include "hash.h"
29 #include "list.h"
30 #include "netdev-dpdk.h"
31 #include "ovs-rcu.h"
32 #include "poll-loop.h"
33 #include "seq.h"
34 #include "socket-util.h"
35 #include "util.h"
36
37 #ifdef __CHECKER__
38 /* Omit the definitions in this file because they are somewhat difficult to
39 * write without prompting "sparse" complaints, without ugliness or
40 * cut-and-paste. Since "sparse" is just a checker, not a compiler, it
41 * doesn't matter that we don't define them. */
42 #else
43 #include "openvswitch/vlog.h"
44
45 VLOG_DEFINE_THIS_MODULE(ovs_thread);
46
47 /* If there is a reason that we cannot fork anymore (unless the fork will be
48 * immediately followed by an exec), then this points to a string that
49 * explains why. */
50 static const char *must_not_fork;
51
52 /* True if we created any threads beyond the main initial thread. */
53 static bool multithreaded;
54
55 #define LOCK_FUNCTION(TYPE, FUN) \
56 void \
57 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
58 const char *where) \
59 OVS_NO_THREAD_SAFETY_ANALYSIS \
60 { \
61 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
62 int error; \
63 \
64 /* Verify that 'l' was initialized. */ \
65 if (OVS_UNLIKELY(!l->where)) { \
66 ovs_abort(0, "%s: %s() passed uninitialized ovs_"#TYPE, \
67 where, __func__); \
68 } \
69 \
70 error = pthread_##TYPE##_##FUN(&l->lock); \
71 if (OVS_UNLIKELY(error)) { \
72 ovs_abort(error, "%s: pthread_%s_%s failed", where, #TYPE, #FUN); \
73 } \
74 l->where = where; \
75 }
76 LOCK_FUNCTION(mutex, lock);
77 LOCK_FUNCTION(rwlock, rdlock);
78 LOCK_FUNCTION(rwlock, wrlock);
79
80 #define TRY_LOCK_FUNCTION(TYPE, FUN) \
81 int \
82 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
83 const char *where) \
84 OVS_NO_THREAD_SAFETY_ANALYSIS \
85 { \
86 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
87 int error; \
88 \
89 /* Verify that 'l' was initialized. */ \
90 if (OVS_UNLIKELY(!l->where)) { \
91 ovs_abort(0, "%s: %s() passed uninitialized ovs_"#TYPE, \
92 where, __func__); \
93 } \
94 \
95 error = pthread_##TYPE##_##FUN(&l->lock); \
96 if (OVS_UNLIKELY(error) && error != EBUSY) { \
97 ovs_abort(error, "%s: pthread_%s_%s failed", where, #TYPE, #FUN); \
98 } \
99 if (!error) { \
100 l->where = where; \
101 } \
102 return error; \
103 }
104 TRY_LOCK_FUNCTION(mutex, trylock);
105 TRY_LOCK_FUNCTION(rwlock, tryrdlock);
106 TRY_LOCK_FUNCTION(rwlock, trywrlock);
107
108 #define UNLOCK_FUNCTION(TYPE, FUN, WHERE) \
109 void \
110 ovs_##TYPE##_##FUN(const struct ovs_##TYPE *l_) \
111 OVS_NO_THREAD_SAFETY_ANALYSIS \
112 { \
113 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
114 int error; \
115 \
116 /* Verify that 'l' was initialized. */ \
117 ovs_assert(l->where); \
118 \
119 l->where = WHERE; \
120 error = pthread_##TYPE##_##FUN(&l->lock); \
121 if (OVS_UNLIKELY(error)) { \
122 ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
123 } \
124 }
125 UNLOCK_FUNCTION(mutex, unlock, "<unlocked>");
126 UNLOCK_FUNCTION(mutex, destroy, NULL);
127 UNLOCK_FUNCTION(rwlock, unlock, "<unlocked>");
128 UNLOCK_FUNCTION(rwlock, destroy, NULL);
129
130 #define XPTHREAD_FUNC1(FUNCTION, PARAM1) \
131 void \
132 x##FUNCTION(PARAM1 arg1) \
133 { \
134 int error = FUNCTION(arg1); \
135 if (OVS_UNLIKELY(error)) { \
136 ovs_abort(error, "%s failed", #FUNCTION); \
137 } \
138 }
139 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2) \
140 void \
141 x##FUNCTION(PARAM1 arg1, PARAM2 arg2) \
142 { \
143 int error = FUNCTION(arg1, arg2); \
144 if (OVS_UNLIKELY(error)) { \
145 ovs_abort(error, "%s failed", #FUNCTION); \
146 } \
147 }
148 #define XPTHREAD_FUNC3(FUNCTION, PARAM1, PARAM2, PARAM3)\
149 void \
150 x##FUNCTION(PARAM1 arg1, PARAM2 arg2, PARAM3 arg3) \
151 { \
152 int error = FUNCTION(arg1, arg2, arg3); \
153 if (OVS_UNLIKELY(error)) { \
154 ovs_abort(error, "%s failed", #FUNCTION); \
155 } \
156 }
157
158 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
159 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
160 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
161 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
162 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
163 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
164
165 XPTHREAD_FUNC1(pthread_rwlockattr_init, pthread_rwlockattr_t *);
166 XPTHREAD_FUNC1(pthread_rwlockattr_destroy, pthread_rwlockattr_t *);
167 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
168 XPTHREAD_FUNC2(pthread_rwlockattr_setkind_np, pthread_rwlockattr_t *, int);
169 #endif
170
171 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
172 XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *);
173 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
174 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
175
176 XPTHREAD_FUNC2(pthread_join, pthread_t, void **);
177
178 typedef void destructor_func(void *);
179 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
180 XPTHREAD_FUNC1(pthread_key_delete, pthread_key_t);
181 XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *);
182
183 #ifndef _WIN32
184 XPTHREAD_FUNC3(pthread_sigmask, int, const sigset_t *, sigset_t *);
185 #endif
186
187 static void
188 ovs_mutex_init__(const struct ovs_mutex *l_, int type)
189 {
190 struct ovs_mutex *l = CONST_CAST(struct ovs_mutex *, l_);
191 pthread_mutexattr_t attr;
192 int error;
193
194 l->where = "<unlocked>";
195 xpthread_mutexattr_init(&attr);
196 xpthread_mutexattr_settype(&attr, type);
197 error = pthread_mutex_init(&l->lock, &attr);
198 if (OVS_UNLIKELY(error)) {
199 ovs_abort(error, "pthread_mutex_init failed");
200 }
201 xpthread_mutexattr_destroy(&attr);
202 }
203
204 /* Initializes 'mutex' as a normal (non-recursive) mutex. */
205 void
206 ovs_mutex_init(const struct ovs_mutex *mutex)
207 {
208 ovs_mutex_init__(mutex, PTHREAD_MUTEX_ERRORCHECK);
209 }
210
211 /* Initializes 'mutex' as a recursive mutex. */
212 void
213 ovs_mutex_init_recursive(const struct ovs_mutex *mutex)
214 {
215 ovs_mutex_init__(mutex, PTHREAD_MUTEX_RECURSIVE);
216 }
217
218 /* Initializes 'mutex' as a recursive mutex. */
219 void
220 ovs_mutex_init_adaptive(const struct ovs_mutex *mutex)
221 {
222 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
223 ovs_mutex_init__(mutex, PTHREAD_MUTEX_ADAPTIVE_NP);
224 #else
225 ovs_mutex_init(mutex);
226 #endif
227 }
228
229 void
230 ovs_rwlock_init(const struct ovs_rwlock *l_)
231 {
232 struct ovs_rwlock *l = CONST_CAST(struct ovs_rwlock *, l_);
233 pthread_rwlockattr_t attr;
234 int error;
235
236 l->where = "<unlocked>";
237
238 xpthread_rwlockattr_init(&attr);
239 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
240 xpthread_rwlockattr_setkind_np(
241 &attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
242 #endif
243 error = pthread_rwlock_init(&l->lock, NULL);
244 if (OVS_UNLIKELY(error)) {
245 ovs_abort(error, "pthread_rwlock_init failed");
246 }
247 xpthread_rwlockattr_destroy(&attr);
248 }
249
250 void
251 ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_)
252 {
253 struct ovs_mutex *mutex = CONST_CAST(struct ovs_mutex *, mutex_);
254 int error;
255
256 ovsrcu_quiesce_start();
257 error = pthread_cond_wait(cond, &mutex->lock);
258 ovsrcu_quiesce_end();
259
260 if (OVS_UNLIKELY(error)) {
261 ovs_abort(error, "pthread_cond_wait failed");
262 }
263 }
264
265 /* Initializes the 'barrier'. 'size' is the number of threads
266 * expected to hit the barrier. */
267 void
268 ovs_barrier_init(struct ovs_barrier *barrier, uint32_t size)
269 {
270 barrier->size = size;
271 atomic_count_init(&barrier->count, 0);
272 barrier->seq = seq_create();
273 }
274
275 /* Destroys the 'barrier'. */
276 void
277 ovs_barrier_destroy(struct ovs_barrier *barrier)
278 {
279 seq_destroy(barrier->seq);
280 }
281
282 /* Makes the calling thread block on the 'barrier' until all
283 * 'barrier->size' threads hit the barrier.
284 * ovs_barrier provides the necessary acquire-release semantics to make
285 * the effects of prior memory accesses of all the participating threads
286 * visible on return and to prevent the following memory accesses to be
287 * reordered before the ovs_barrier_block(). */
288 void
289 ovs_barrier_block(struct ovs_barrier *barrier)
290 {
291 uint64_t seq = seq_read(barrier->seq);
292 uint32_t orig;
293
294 orig = atomic_count_inc(&barrier->count);
295 if (orig + 1 == barrier->size) {
296 atomic_count_set(&barrier->count, 0);
297 /* seq_change() serves as a release barrier against the other threads,
298 * so the zeroed count is visible to them as they continue. */
299 seq_change(barrier->seq);
300 } else {
301 /* To prevent thread from waking up by other event,
302 * keeps waiting for the change of 'barrier->seq'. */
303 while (seq == seq_read(barrier->seq)) {
304 seq_wait(barrier->seq, seq);
305 poll_block();
306 }
307 }
308 }
309 \f
310 DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, 0);
311
312 struct ovsthread_aux {
313 void *(*start)(void *);
314 void *arg;
315 char name[16];
316 };
317
318 static void *
319 ovsthread_wrapper(void *aux_)
320 {
321 static atomic_count next_id = ATOMIC_COUNT_INIT(1);
322
323 struct ovsthread_aux *auxp = aux_;
324 struct ovsthread_aux aux;
325 unsigned int id;
326
327 id = atomic_count_inc(&next_id);
328 *ovsthread_id_get() = id;
329
330 aux = *auxp;
331 free(auxp);
332
333 /* The order of the following calls is important, because
334 * ovsrcu_quiesce_end() saves a copy of the thread name. */
335 char *subprogram_name = xasprintf("%s%u", aux.name, id);
336 set_subprogram_name(subprogram_name);
337 free(subprogram_name);
338 ovsrcu_quiesce_end();
339
340 return aux.start(aux.arg);
341 }
342
343 static void
344 set_min_stack_size(pthread_attr_t *attr, size_t min_stacksize)
345 {
346 size_t stacksize;
347 int error;
348
349 error = pthread_attr_getstacksize(attr, &stacksize);
350 if (error) {
351 ovs_abort(error, "pthread_attr_getstacksize failed");
352 }
353
354 if (stacksize < min_stacksize) {
355 error = pthread_attr_setstacksize(attr, min_stacksize);
356 if (error) {
357 ovs_abort(error, "pthread_attr_setstacksize failed");
358 }
359 }
360 }
361
362 /* Starts a thread that calls 'start(arg)'. Sets the thread's name to 'name'
363 * (suffixed by its ovsthread_id()). Returns the new thread's pthread_t. */
364 pthread_t
365 ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
366 {
367 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
368 struct ovsthread_aux *aux;
369 pthread_t thread;
370 int error;
371
372 forbid_forking("multiple threads exist");
373 multithreaded = true;
374
375 if (ovsthread_once_start(&once)) {
376 /* The first call to this function has to happen in the main thread.
377 * Before the process becomes multithreaded we make sure that the
378 * main thread is considered non quiescent.
379 *
380 * For other threads this is done in ovs_thread_wrapper(), but the
381 * main thread has no such wrapper.
382 *
383 * There's no reason to call ovsrcu_quiesce_end() in subsequent
384 * invocations of this function and it might introduce problems
385 * for other threads. */
386 ovsrcu_quiesce_end();
387 ovsthread_once_done(&once);
388 }
389
390 aux = xmalloc(sizeof *aux);
391 aux->start = start;
392 aux->arg = arg;
393 ovs_strlcpy(aux->name, name, sizeof aux->name);
394
395 /* Some small systems use a default stack size as small as 80 kB, but OVS
396 * requires approximately 384 kB according to the following analysis:
397 * http://openvswitch.org/pipermail/dev/2016-January/065049.html
398 *
399 * We use 512 kB to give us some margin of error. */
400 pthread_attr_t attr;
401 pthread_attr_init(&attr);
402 set_min_stack_size(&attr, 512 * 1024);
403
404 error = pthread_create(&thread, &attr, ovsthread_wrapper, aux);
405 if (error) {
406 ovs_abort(error, "pthread_create failed");
407 }
408 pthread_attr_destroy(&attr);
409 return thread;
410 }
411 \f
412 bool
413 ovsthread_once_start__(struct ovsthread_once *once)
414 {
415 ovs_mutex_lock(&once->mutex);
416 /* Mutex synchronizes memory, so we get the current value of 'done'. */
417 if (!once->done) {
418 return true;
419 }
420 ovs_mutex_unlock(&once->mutex);
421 return false;
422 }
423
424 void
425 ovsthread_once_done(struct ovsthread_once *once)
426 {
427 /* We need release semantics here, so that the following store may not
428 * be moved ahead of any of the preceding initialization operations.
429 * A release atomic_thread_fence provides that prior memory accesses
430 * will not be reordered to take place after the following store. */
431 atomic_thread_fence(memory_order_release);
432 once->done = true;
433 ovs_mutex_unlock(&once->mutex);
434 }
435 \f
436 bool
437 single_threaded(void)
438 {
439 return !multithreaded;
440 }
441
442 /* Asserts that the process has not yet created any threads (beyond the initial
443 * thread).
444 *
445 * ('where' is used in logging. Commonly one would use
446 * assert_single_threaded() to automatically provide the caller's source file
447 * and line number for 'where'.) */
448 void
449 assert_single_threaded_at(const char *where)
450 {
451 if (multithreaded) {
452 VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
453 where);
454 }
455 }
456
457 #ifndef _WIN32
458 /* Forks the current process (checking that this is allowed). Aborts with
459 * VLOG_FATAL if fork() returns an error, and otherwise returns the value
460 * returned by fork().
461 *
462 * ('where' is used in logging. Commonly one would use xfork() to
463 * automatically provide the caller's source file and line number for
464 * 'where'.) */
465 pid_t
466 xfork_at(const char *where)
467 {
468 pid_t pid;
469
470 if (must_not_fork) {
471 VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
472 where, must_not_fork);
473 }
474
475 pid = fork();
476 if (pid < 0) {
477 VLOG_FATAL("%s: fork failed (%s)", where, ovs_strerror(errno));
478 }
479 return pid;
480 }
481 #endif
482
483 /* Notes that the process must not call fork() from now on, for the specified
484 * 'reason'. (The process may still fork() if it execs itself immediately
485 * afterward.) */
486 void
487 forbid_forking(const char *reason)
488 {
489 ovs_assert(reason != NULL);
490 must_not_fork = reason;
491 }
492
493 /* Returns true if the process is allowed to fork, false otherwise. */
494 bool
495 may_fork(void)
496 {
497 return !must_not_fork;
498 }
499 \f
500 /* ovsthread_stats. */
501
502 void
503 ovsthread_stats_init(struct ovsthread_stats *stats)
504 {
505 int i;
506
507 ovs_mutex_init(&stats->mutex);
508 for (i = 0; i < ARRAY_SIZE(stats->buckets); i++) {
509 stats->buckets[i] = NULL;
510 }
511 }
512
513 void
514 ovsthread_stats_destroy(struct ovsthread_stats *stats)
515 {
516 ovs_mutex_destroy(&stats->mutex);
517 }
518
519 void *
520 ovsthread_stats_bucket_get(struct ovsthread_stats *stats,
521 void *(*new_bucket)(void))
522 {
523 unsigned int idx = ovsthread_id_self() & (ARRAY_SIZE(stats->buckets) - 1);
524 void *bucket = stats->buckets[idx];
525 if (!bucket) {
526 ovs_mutex_lock(&stats->mutex);
527 bucket = stats->buckets[idx];
528 if (!bucket) {
529 bucket = stats->buckets[idx] = new_bucket();
530 }
531 ovs_mutex_unlock(&stats->mutex);
532 }
533 return bucket;
534 }
535
536 size_t
537 ovs_thread_stats_next_bucket(const struct ovsthread_stats *stats, size_t i)
538 {
539 for (; i < ARRAY_SIZE(stats->buckets); i++) {
540 if (stats->buckets[i]) {
541 break;
542 }
543 }
544 return i;
545 }
546
547 \f
548 /* Parses /proc/cpuinfo for the total number of physical cores on this system
549 * across all CPU packages, not counting hyper-threads.
550 *
551 * Sets *n_cores to the total number of cores on this system, or 0 if the
552 * number cannot be determined. */
553 static void
554 parse_cpuinfo(long int *n_cores)
555 {
556 static const char file_name[] = "/proc/cpuinfo";
557 char line[128];
558 uint64_t cpu = 0; /* Support up to 64 CPU packages on a single system. */
559 long int cores = 0;
560 FILE *stream;
561
562 stream = fopen(file_name, "r");
563 if (!stream) {
564 VLOG_DBG("%s: open failed (%s)", file_name, ovs_strerror(errno));
565 return;
566 }
567
568 while (fgets(line, sizeof line, stream)) {
569 unsigned int id;
570
571 /* Find the next CPU package. */
572 if (ovs_scan(line, "physical id%*[^:]: %u", &id)) {
573 if (id > 63) {
574 VLOG_WARN("Counted over 64 CPU packages on this system. "
575 "Parsing %s for core count may be inaccurate.",
576 file_name);
577 cores = 0;
578 break;
579 }
580
581 if (cpu & (1ULL << id)) {
582 /* We've already counted this package's cores. */
583 continue;
584 }
585 cpu |= 1ULL << id;
586
587 /* Find the number of cores for this package. */
588 while (fgets(line, sizeof line, stream)) {
589 int count;
590
591 if (ovs_scan(line, "cpu cores%*[^:]: %u", &count)) {
592 cores += count;
593 break;
594 }
595 }
596 }
597 }
598 fclose(stream);
599
600 *n_cores = cores;
601 }
602
603 /* Returns the total number of cores on this system, or 0 if the number cannot
604 * be determined.
605 *
606 * Tries not to count hyper-threads, but may be inaccurate - particularly on
607 * platforms that do not provide /proc/cpuinfo, but also if /proc/cpuinfo is
608 * formatted different to the layout that parse_cpuinfo() expects. */
609 int
610 count_cpu_cores(void)
611 {
612 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
613 static long int n_cores;
614
615 if (ovsthread_once_start(&once)) {
616 #ifndef _WIN32
617 parse_cpuinfo(&n_cores);
618 if (!n_cores) {
619 n_cores = sysconf(_SC_NPROCESSORS_ONLN);
620 }
621 #else
622 SYSTEM_INFO sysinfo;
623 GetSystemInfo(&sysinfo);
624 n_cores = sysinfo.dwNumberOfProcessors;
625 #endif
626 ovsthread_once_done(&once);
627 }
628
629 return n_cores > 0 ? n_cores : 0;
630 }
631
632 /* Returns 'true' if current thread is PMD thread. */
633 bool
634 thread_is_pmd(void)
635 {
636 const char *name = get_subprogram_name();
637 return !strncmp(name, "pmd", 3);
638 }
639
640 \f
641 /* ovsthread_key. */
642
643 #define L1_SIZE 1024
644 #define L2_SIZE 1024
645 #define MAX_KEYS (L1_SIZE * L2_SIZE)
646
647 /* A piece of thread-specific data. */
648 struct ovsthread_key {
649 struct ovs_list list_node; /* In 'inuse_keys' or 'free_keys'. */
650 void (*destructor)(void *); /* Called at thread exit. */
651
652 /* Indexes into the per-thread array in struct ovsthread_key_slots.
653 * This key's data is stored in p1[index / L2_SIZE][index % L2_SIZE]. */
654 unsigned int index;
655 };
656
657 /* Per-thread data structure. */
658 struct ovsthread_key_slots {
659 struct ovs_list list_node; /* In 'slots_list'. */
660 void **p1[L1_SIZE];
661 };
662
663 /* Contains "struct ovsthread_key_slots *". */
664 static pthread_key_t tsd_key;
665
666 /* Guards data structures below. */
667 static struct ovs_mutex key_mutex = OVS_MUTEX_INITIALIZER;
668
669 /* 'inuse_keys' holds "struct ovsthread_key"s that have been created and not
670 * yet destroyed.
671 *
672 * 'free_keys' holds "struct ovsthread_key"s that have been deleted and are
673 * ready for reuse. (We keep them around only to be able to easily locate
674 * free indexes.)
675 *
676 * Together, 'inuse_keys' and 'free_keys' hold an ovsthread_key for every index
677 * from 0 to n_keys - 1, inclusive. */
678 static struct ovs_list inuse_keys OVS_GUARDED_BY(key_mutex)
679 = OVS_LIST_INITIALIZER(&inuse_keys);
680 static struct ovs_list free_keys OVS_GUARDED_BY(key_mutex)
681 = OVS_LIST_INITIALIZER(&free_keys);
682 static unsigned int n_keys OVS_GUARDED_BY(key_mutex);
683
684 /* All existing struct ovsthread_key_slots. */
685 static struct ovs_list slots_list OVS_GUARDED_BY(key_mutex)
686 = OVS_LIST_INITIALIZER(&slots_list);
687
688 static void *
689 clear_slot(struct ovsthread_key_slots *slots, unsigned int index)
690 {
691 void **p2 = slots->p1[index / L2_SIZE];
692 if (p2) {
693 void **valuep = &p2[index % L2_SIZE];
694 void *value = *valuep;
695 *valuep = NULL;
696 return value;
697 } else {
698 return NULL;
699 }
700 }
701
702 static void
703 ovsthread_key_destruct__(void *slots_)
704 {
705 struct ovsthread_key_slots *slots = slots_;
706 struct ovsthread_key *key;
707 unsigned int n;
708 int i;
709
710 ovs_mutex_lock(&key_mutex);
711 list_remove(&slots->list_node);
712 LIST_FOR_EACH (key, list_node, &inuse_keys) {
713 void *value = clear_slot(slots, key->index);
714 if (value && key->destructor) {
715 key->destructor(value);
716 }
717 }
718 n = n_keys;
719 ovs_mutex_unlock(&key_mutex);
720
721 for (i = 0; i < DIV_ROUND_UP(n, L2_SIZE); i++) {
722 free(slots->p1[i]);
723 }
724 free(slots);
725 }
726
727 /* Cancels the callback to ovsthread_key_destruct__().
728 *
729 * Cancelling the call to the destructor during the main thread exit
730 * is needed while using pthreads-win32 library in Windows. It has been
731 * observed that in pthreads-win32, a call to the destructor during
732 * main thread exit causes undefined behavior. */
733 static void
734 ovsthread_cancel_ovsthread_key_destruct__(void *aux OVS_UNUSED)
735 {
736 pthread_setspecific(tsd_key, NULL);
737 }
738
739 /* Initializes '*keyp' as a thread-specific data key. The data items are
740 * initially null in all threads.
741 *
742 * If a thread exits with non-null data, then 'destructor', if nonnull, will be
743 * called passing the final data value as its argument. 'destructor' must not
744 * call any thread-specific data functions in this API.
745 *
746 * This function is similar to xpthread_key_create(). */
747 void
748 ovsthread_key_create(ovsthread_key_t *keyp, void (*destructor)(void *))
749 {
750 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
751 struct ovsthread_key *key;
752
753 if (ovsthread_once_start(&once)) {
754 xpthread_key_create(&tsd_key, ovsthread_key_destruct__);
755 fatal_signal_add_hook(ovsthread_cancel_ovsthread_key_destruct__,
756 NULL, NULL, true);
757 ovsthread_once_done(&once);
758 }
759
760 ovs_mutex_lock(&key_mutex);
761 if (list_is_empty(&free_keys)) {
762 key = xmalloc(sizeof *key);
763 key->index = n_keys++;
764 if (key->index >= MAX_KEYS) {
765 abort();
766 }
767 } else {
768 key = CONTAINER_OF(list_pop_back(&free_keys),
769 struct ovsthread_key, list_node);
770 }
771 list_push_back(&inuse_keys, &key->list_node);
772 key->destructor = destructor;
773 ovs_mutex_unlock(&key_mutex);
774
775 *keyp = key;
776 }
777
778 /* Frees 'key'. The destructor supplied to ovsthread_key_create(), if any, is
779 * not called.
780 *
781 * This function is similar to xpthread_key_delete(). */
782 void
783 ovsthread_key_delete(ovsthread_key_t key)
784 {
785 struct ovsthread_key_slots *slots;
786
787 ovs_mutex_lock(&key_mutex);
788
789 /* Move 'key' from 'inuse_keys' to 'free_keys'. */
790 list_remove(&key->list_node);
791 list_push_back(&free_keys, &key->list_node);
792
793 /* Clear this slot in all threads. */
794 LIST_FOR_EACH (slots, list_node, &slots_list) {
795 clear_slot(slots, key->index);
796 }
797
798 ovs_mutex_unlock(&key_mutex);
799 }
800
801 static void **
802 ovsthread_key_lookup__(const struct ovsthread_key *key)
803 {
804 struct ovsthread_key_slots *slots;
805 void **p2;
806
807 slots = pthread_getspecific(tsd_key);
808 if (!slots) {
809 slots = xzalloc(sizeof *slots);
810
811 ovs_mutex_lock(&key_mutex);
812 pthread_setspecific(tsd_key, slots);
813 list_push_back(&slots_list, &slots->list_node);
814 ovs_mutex_unlock(&key_mutex);
815 }
816
817 p2 = slots->p1[key->index / L2_SIZE];
818 if (!p2) {
819 p2 = xzalloc(L2_SIZE * sizeof *p2);
820 slots->p1[key->index / L2_SIZE] = p2;
821 }
822
823 return &p2[key->index % L2_SIZE];
824 }
825
826 /* Sets the value of thread-specific data item 'key', in the current thread, to
827 * 'value'.
828 *
829 * This function is similar to pthread_setspecific(). */
830 void
831 ovsthread_setspecific(ovsthread_key_t key, const void *value)
832 {
833 *ovsthread_key_lookup__(key) = CONST_CAST(void *, value);
834 }
835
836 /* Returns the value of thread-specific data item 'key' in the current thread.
837 *
838 * This function is similar to pthread_getspecific(). */
839 void *
840 ovsthread_getspecific(ovsthread_key_t key)
841 {
842 return *ovsthread_key_lookup__(key);
843 }
844 #endif