ovs-rcu: Log a helpful warning when ovsrcu_synchronize() stalls.
[sliver-openvswitch.git] / lib / ovs-thread.c
1 /*
2  * Copyright (c) 2013, 2014 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 #include <stdlib.h>
22 #include <unistd.h>
23 #include "compiler.h"
24 #include "hash.h"
25 #include "ovs-rcu.h"
26 #include "poll-loop.h"
27 #include "socket-util.h"
28 #include "util.h"
29
30 #ifdef __CHECKER__
31 /* Omit the definitions in this file because they are somewhat difficult to
32  * write without prompting "sparse" complaints, without ugliness or
33  * cut-and-paste.  Since "sparse" is just a checker, not a compiler, it
34  * doesn't matter that we don't define them. */
35 #else
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(ovs_thread);
39
40 /* If there is a reason that we cannot fork anymore (unless the fork will be
41  * immediately followed by an exec), then this points to a string that
42  * explains why. */
43 static const char *must_not_fork;
44
45 /* True if we created any threads beyond the main initial thread. */
46 static bool multithreaded;
47
48 #define LOCK_FUNCTION(TYPE, FUN) \
49     void \
50     ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
51                             const char *where) \
52         OVS_NO_THREAD_SAFETY_ANALYSIS \
53     { \
54         struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
55         int error = pthread_##TYPE##_##FUN(&l->lock); \
56         if (OVS_UNLIKELY(error)) { \
57             ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
58         } \
59         l->where = where; \
60     }
61 LOCK_FUNCTION(mutex, lock);
62 LOCK_FUNCTION(rwlock, rdlock);
63 LOCK_FUNCTION(rwlock, wrlock);
64
65 #define TRY_LOCK_FUNCTION(TYPE, FUN) \
66     int \
67     ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
68                             const char *where) \
69         OVS_NO_THREAD_SAFETY_ANALYSIS \
70     { \
71         struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
72         int error = pthread_##TYPE##_##FUN(&l->lock); \
73         if (OVS_UNLIKELY(error) && error != EBUSY) { \
74             ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
75         } \
76         if (!error) { \
77             l->where = where; \
78         } \
79         return error; \
80     }
81 TRY_LOCK_FUNCTION(mutex, trylock);
82 TRY_LOCK_FUNCTION(rwlock, tryrdlock);
83 TRY_LOCK_FUNCTION(rwlock, trywrlock);
84
85 #define UNLOCK_FUNCTION(TYPE, FUN) \
86     void \
87     ovs_##TYPE##_##FUN(const struct ovs_##TYPE *l_) \
88         OVS_NO_THREAD_SAFETY_ANALYSIS \
89     { \
90         struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
91         int error; \
92         l->where = NULL; \
93         error = pthread_##TYPE##_##FUN(&l->lock); \
94         if (OVS_UNLIKELY(error)) { \
95             ovs_abort(error, "pthread_%s_%sfailed", #TYPE, #FUN); \
96         } \
97     }
98 UNLOCK_FUNCTION(mutex, unlock);
99 UNLOCK_FUNCTION(mutex, destroy);
100 UNLOCK_FUNCTION(rwlock, unlock);
101 UNLOCK_FUNCTION(rwlock, destroy);
102
103 #define XPTHREAD_FUNC1(FUNCTION, PARAM1)                \
104     void                                                \
105     x##FUNCTION(PARAM1 arg1)                            \
106     {                                                   \
107         int error = FUNCTION(arg1);                     \
108         if (OVS_UNLIKELY(error)) {                      \
109             ovs_abort(error, "%s failed", #FUNCTION);   \
110         }                                               \
111     }
112 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2)        \
113     void                                                \
114     x##FUNCTION(PARAM1 arg1, PARAM2 arg2)               \
115     {                                                   \
116         int error = FUNCTION(arg1, arg2);               \
117         if (OVS_UNLIKELY(error)) {                      \
118             ovs_abort(error, "%s failed", #FUNCTION);   \
119         }                                               \
120     }
121 #define XPTHREAD_FUNC3(FUNCTION, PARAM1, PARAM2, PARAM3)\
122     void                                                \
123     x##FUNCTION(PARAM1 arg1, PARAM2 arg2, PARAM3 arg3)  \
124     {                                                   \
125         int error = FUNCTION(arg1, arg2, arg3);         \
126         if (OVS_UNLIKELY(error)) {                      \
127             ovs_abort(error, "%s failed", #FUNCTION);   \
128         }                                               \
129     }
130
131 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
132 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
133 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
134 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
135 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
136 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
137
138 XPTHREAD_FUNC1(pthread_rwlockattr_init, pthread_rwlockattr_t *);
139 XPTHREAD_FUNC1(pthread_rwlockattr_destroy, pthread_rwlockattr_t *);
140 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
141 XPTHREAD_FUNC2(pthread_rwlockattr_setkind_np, pthread_rwlockattr_t *, int);
142 #endif
143
144 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
145 XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *);
146 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
147 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
148
149 XPTHREAD_FUNC3(pthread_barrier_init, pthread_barrier_t *,
150                pthread_barrierattr_t *, unsigned int);
151 XPTHREAD_FUNC1(pthread_barrier_destroy, pthread_barrier_t *);
152
153 XPTHREAD_FUNC2(pthread_join, pthread_t, void **);
154
155 typedef void destructor_func(void *);
156 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
157 XPTHREAD_FUNC1(pthread_key_delete, pthread_key_t);
158 XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *);
159
160 static void
161 ovs_mutex_init__(const struct ovs_mutex *l_, int type)
162 {
163     struct ovs_mutex *l = CONST_CAST(struct ovs_mutex *, l_);
164     pthread_mutexattr_t attr;
165     int error;
166
167     l->where = NULL;
168     xpthread_mutexattr_init(&attr);
169     xpthread_mutexattr_settype(&attr, type);
170     error = pthread_mutex_init(&l->lock, &attr);
171     if (OVS_UNLIKELY(error)) {
172         ovs_abort(error, "pthread_mutex_init failed");
173     }
174     xpthread_mutexattr_destroy(&attr);
175 }
176
177 /* Initializes 'mutex' as a normal (non-recursive) mutex. */
178 void
179 ovs_mutex_init(const struct ovs_mutex *mutex)
180 {
181     ovs_mutex_init__(mutex, PTHREAD_MUTEX_ERRORCHECK);
182 }
183
184 /* Initializes 'mutex' as a recursive mutex. */
185 void
186 ovs_mutex_init_recursive(const struct ovs_mutex *mutex)
187 {
188     ovs_mutex_init__(mutex, PTHREAD_MUTEX_RECURSIVE);
189 }
190
191 /* Initializes 'mutex' as a recursive mutex. */
192 void
193 ovs_mutex_init_adaptive(const struct ovs_mutex *mutex)
194 {
195 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
196     ovs_mutex_init__(mutex, PTHREAD_MUTEX_ADAPTIVE_NP);
197 #else
198     ovs_mutex_init(mutex);
199 #endif
200 }
201
202 void
203 ovs_rwlock_init(const struct ovs_rwlock *l_)
204 {
205     struct ovs_rwlock *l = CONST_CAST(struct ovs_rwlock *, l_);
206     pthread_rwlockattr_t attr;
207     int error;
208
209     l->where = NULL;
210
211     xpthread_rwlockattr_init(&attr);
212 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
213     xpthread_rwlockattr_setkind_np(
214         &attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
215 #endif
216     error = pthread_rwlock_init(&l->lock, NULL);
217     if (OVS_UNLIKELY(error)) {
218         ovs_abort(error, "pthread_rwlock_init failed");
219     }
220     xpthread_rwlockattr_destroy(&attr);
221 }
222
223 void
224 ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_)
225 {
226     struct ovs_mutex *mutex = CONST_CAST(struct ovs_mutex *, mutex_);
227     int error;
228
229     ovsrcu_quiesce_start();
230     error = pthread_cond_wait(cond, &mutex->lock);
231     ovsrcu_quiesce_end();
232
233     if (OVS_UNLIKELY(error)) {
234         ovs_abort(error, "pthread_cond_wait failed");
235     }
236 }
237
238 int
239 xpthread_barrier_wait(pthread_barrier_t *barrier)
240 {
241     int error;
242
243     ovsrcu_quiesce_start();
244     error = pthread_barrier_wait(barrier);
245     ovsrcu_quiesce_end();
246
247     if (error && OVS_UNLIKELY(error != PTHREAD_BARRIER_SERIAL_THREAD)) {
248         ovs_abort(error, "pthread_barrier_wait failed");
249     }
250
251     return error;
252 }
253 \f
254 DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, 0);
255
256 struct ovsthread_aux {
257     void *(*start)(void *);
258     void *arg;
259     char name[16];
260 };
261
262 static void *
263 ovsthread_wrapper(void *aux_)
264 {
265     static atomic_uint next_id = ATOMIC_VAR_INIT(1);
266
267     struct ovsthread_aux *auxp = aux_;
268     struct ovsthread_aux aux;
269     unsigned int id;
270
271     atomic_add(&next_id, 1, &id);
272     *ovsthread_id_get() = id;
273
274     aux = *auxp;
275     free(auxp);
276
277     /* The order of the following calls is important, because
278      * ovsrcu_quiesce_end() saves a copy of the thread name. */
279     set_subprogram_name("%s%u", aux.name, id);
280     ovsrcu_quiesce_end();
281
282     return aux.start(aux.arg);
283 }
284
285 /* Starts a thread that calls 'start(arg)'.  Sets the thread's name to 'name'
286  * (suffixed by its ovsthread_id()).  Returns the new thread's pthread_t. */
287 pthread_t
288 ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
289 {
290     struct ovsthread_aux *aux;
291     pthread_t thread;
292     int error;
293
294     forbid_forking("multiple threads exist");
295     multithreaded = true;
296     ovsrcu_quiesce_end();
297
298     aux = xmalloc(sizeof *aux);
299     aux->start = start;
300     aux->arg = arg;
301     ovs_strlcpy(aux->name, name, sizeof aux->name);
302
303     error = pthread_create(&thread, NULL, ovsthread_wrapper, aux);
304     if (error) {
305         ovs_abort(error, "pthread_create failed");
306     }
307     return thread;
308 }
309 \f
310 bool
311 ovsthread_once_start__(struct ovsthread_once *once)
312 {
313     ovs_mutex_lock(&once->mutex);
314     if (!ovsthread_once_is_done__(once)) {
315         return false;
316     }
317     ovs_mutex_unlock(&once->mutex);
318     return true;
319 }
320
321 void
322 ovsthread_once_done(struct ovsthread_once *once)
323 {
324     atomic_store(&once->done, true);
325     ovs_mutex_unlock(&once->mutex);
326 }
327 \f
328 bool
329 single_threaded(void)
330 {
331     return !multithreaded;
332 }
333
334 /* Asserts that the process has not yet created any threads (beyond the initial
335  * thread).
336  *
337  * ('where' is used in logging.  Commonly one would use
338  * assert_single_threaded() to automatically provide the caller's source file
339  * and line number for 'where'.) */
340 void
341 assert_single_threaded_at(const char *where)
342 {
343     if (multithreaded) {
344         VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
345                    where);
346     }
347 }
348
349 #ifndef _WIN32
350 /* Forks the current process (checking that this is allowed).  Aborts with
351  * VLOG_FATAL if fork() returns an error, and otherwise returns the value
352  * returned by fork().
353  *
354  * ('where' is used in logging.  Commonly one would use xfork() to
355  * automatically provide the caller's source file and line number for
356  * 'where'.) */
357 pid_t
358 xfork_at(const char *where)
359 {
360     pid_t pid;
361
362     if (must_not_fork) {
363         VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
364                    where, must_not_fork);
365     }
366
367     pid = fork();
368     if (pid < 0) {
369         VLOG_FATAL("%s: fork failed (%s)", where, ovs_strerror(errno));
370     }
371     return pid;
372 }
373 #endif
374
375 /* Notes that the process must not call fork() from now on, for the specified
376  * 'reason'.  (The process may still fork() if it execs itself immediately
377  * afterward.) */
378 void
379 forbid_forking(const char *reason)
380 {
381     ovs_assert(reason != NULL);
382     must_not_fork = reason;
383 }
384
385 /* Returns true if the process is allowed to fork, false otherwise. */
386 bool
387 may_fork(void)
388 {
389     return !must_not_fork;
390 }
391 \f
392 /* ovsthread_stats. */
393
394 void
395 ovsthread_stats_init(struct ovsthread_stats *stats)
396 {
397     int i;
398
399     ovs_mutex_init(&stats->mutex);
400     for (i = 0; i < ARRAY_SIZE(stats->buckets); i++) {
401         stats->buckets[i] = NULL;
402     }
403 }
404
405 void
406 ovsthread_stats_destroy(struct ovsthread_stats *stats)
407 {
408     ovs_mutex_destroy(&stats->mutex);
409 }
410
411 void *
412 ovsthread_stats_bucket_get(struct ovsthread_stats *stats,
413                            void *(*new_bucket)(void))
414 {
415     unsigned int idx = ovsthread_id_self() & (ARRAY_SIZE(stats->buckets) - 1);
416     void *bucket = stats->buckets[idx];
417     if (!bucket) {
418         ovs_mutex_lock(&stats->mutex);
419         bucket = stats->buckets[idx];
420         if (!bucket) {
421             bucket = stats->buckets[idx] = new_bucket();
422         }
423         ovs_mutex_unlock(&stats->mutex);
424     }
425     return bucket;
426 }
427
428 size_t
429 ovs_thread_stats_next_bucket(const struct ovsthread_stats *stats, size_t i)
430 {
431     for (; i < ARRAY_SIZE(stats->buckets); i++) {
432         if (stats->buckets[i]) {
433             break;
434         }
435     }
436     return i;
437 }
438
439 \f
440 /* Parses /proc/cpuinfo for the total number of physical cores on this system
441  * across all CPU packages, not counting hyper-threads.
442  *
443  * Sets *n_cores to the total number of cores on this system, or 0 if the
444  * number cannot be determined. */
445 static void
446 parse_cpuinfo(long int *n_cores)
447 {
448     static const char file_name[] = "/proc/cpuinfo";
449     char line[128];
450     uint64_t cpu = 0; /* Support up to 64 CPU packages on a single system. */
451     long int cores = 0;
452     FILE *stream;
453
454     stream = fopen(file_name, "r");
455     if (!stream) {
456         VLOG_DBG("%s: open failed (%s)", file_name, ovs_strerror(errno));
457         return;
458     }
459
460     while (fgets(line, sizeof line, stream)) {
461         unsigned int id;
462
463         /* Find the next CPU package. */
464         if (ovs_scan(line, "physical id%*[^:]: %u", &id)) {
465             if (id > 63) {
466                 VLOG_WARN("Counted over 64 CPU packages on this system. "
467                           "Parsing %s for core count may be inaccurate.",
468                           file_name);
469                 cores = 0;
470                 break;
471             }
472
473             if (cpu & (1 << id)) {
474                 /* We've already counted this package's cores. */
475                 continue;
476             }
477             cpu |= 1 << id;
478
479             /* Find the number of cores for this package. */
480             while (fgets(line, sizeof line, stream)) {
481                 int count;
482
483                 if (ovs_scan(line, "cpu cores%*[^:]: %u", &count)) {
484                     cores += count;
485                     break;
486                 }
487             }
488         }
489     }
490     fclose(stream);
491
492     *n_cores = cores;
493 }
494
495 /* Returns the total number of cores on this system, or 0 if the number cannot
496  * be determined.
497  *
498  * Tries not to count hyper-threads, but may be inaccurate - particularly on
499  * platforms that do not provide /proc/cpuinfo, but also if /proc/cpuinfo is
500  * formatted different to the layout that parse_cpuinfo() expects. */
501 int
502 count_cpu_cores(void)
503 {
504     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
505     static long int n_cores;
506
507     if (ovsthread_once_start(&once)) {
508 #ifndef _WIN32
509         parse_cpuinfo(&n_cores);
510         if (!n_cores) {
511             n_cores = sysconf(_SC_NPROCESSORS_ONLN);
512         }
513 #else
514         SYSTEM_INFO sysinfo;
515         GetSystemInfo(&sysinfo);
516         n_cores = sysinfo.dwNumberOfProcessors;
517 #endif
518         ovsthread_once_done(&once);
519     }
520
521     return n_cores > 0 ? n_cores : 0;
522 }
523 \f
524 /* ovsthread_key. */
525
526 #define L1_SIZE 1024
527 #define L2_SIZE 1024
528 #define MAX_KEYS (L1_SIZE * L2_SIZE)
529
530 /* A piece of thread-specific data. */
531 struct ovsthread_key {
532     struct list list_node;      /* In 'inuse_keys' or 'free_keys'. */
533     void (*destructor)(void *); /* Called at thread exit. */
534
535     /* Indexes into the per-thread array in struct ovsthread_key_slots.
536      * This key's data is stored in p1[index / L2_SIZE][index % L2_SIZE]. */
537     unsigned int index;
538 };
539
540 /* Per-thread data structure. */
541 struct ovsthread_key_slots {
542     struct list list_node;      /* In 'slots_list'. */
543     void **p1[L1_SIZE];
544 };
545
546 /* Contains "struct ovsthread_key_slots *". */
547 static pthread_key_t tsd_key;
548
549 /* Guards data structures below. */
550 static struct ovs_mutex key_mutex = OVS_MUTEX_INITIALIZER;
551
552 /* 'inuse_keys' holds "struct ovsthread_key"s that have been created and not
553  * yet destroyed.
554  *
555  * 'free_keys' holds "struct ovsthread_key"s that have been deleted and are
556  * ready for reuse.  (We keep them around only to be able to easily locate
557  * free indexes.)
558  *
559  * Together, 'inuse_keys' and 'free_keys' hold an ovsthread_key for every index
560  * from 0 to n_keys - 1, inclusive. */
561 static struct list inuse_keys OVS_GUARDED_BY(key_mutex)
562     = LIST_INITIALIZER(&inuse_keys);
563 static struct list free_keys OVS_GUARDED_BY(key_mutex)
564     = LIST_INITIALIZER(&free_keys);
565 static unsigned int n_keys OVS_GUARDED_BY(key_mutex);
566
567 /* All existing struct ovsthread_key_slots. */
568 static struct list slots_list OVS_GUARDED_BY(key_mutex)
569     = LIST_INITIALIZER(&slots_list);
570
571 static void *
572 clear_slot(struct ovsthread_key_slots *slots, unsigned int index)
573 {
574     void **p2 = slots->p1[index / L2_SIZE];
575     if (p2) {
576         void **valuep = &p2[index % L2_SIZE];
577         void *value = *valuep;
578         *valuep = NULL;
579         return value;
580     } else {
581         return NULL;
582     }
583 }
584
585 static void
586 ovsthread_key_destruct__(void *slots_)
587 {
588     struct ovsthread_key_slots *slots = slots_;
589     struct ovsthread_key *key;
590     unsigned int n;
591     int i;
592
593     ovs_mutex_lock(&key_mutex);
594     list_remove(&slots->list_node);
595     LIST_FOR_EACH (key, list_node, &inuse_keys) {
596         void *value = clear_slot(slots, key->index);
597         if (value && key->destructor) {
598             key->destructor(value);
599         }
600     }
601     n = n_keys;
602     ovs_mutex_unlock(&key_mutex);
603
604     for (i = 0; i < n / L2_SIZE; i++) {
605         free(slots->p1[i]);
606     }
607     free(slots);
608 }
609
610 /* Initializes '*keyp' as a thread-specific data key.  The data items are
611  * initially null in all threads.
612  *
613  * If a thread exits with non-null data, then 'destructor', if nonnull, will be
614  * called passing the final data value as its argument.  'destructor' must not
615  * call any thread-specific data functions in this API.
616  *
617  * This function is similar to xpthread_key_create(). */
618 void
619 ovsthread_key_create(ovsthread_key_t *keyp, void (*destructor)(void *))
620 {
621     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
622     struct ovsthread_key *key;
623
624     if (ovsthread_once_start(&once)) {
625         xpthread_key_create(&tsd_key, ovsthread_key_destruct__);
626         ovsthread_once_done(&once);
627     }
628
629     ovs_mutex_lock(&key_mutex);
630     if (list_is_empty(&free_keys)) {
631         key = xmalloc(sizeof *key);
632         key->index = n_keys++;
633         if (key->index >= MAX_KEYS) {
634             abort();
635         }
636     } else {
637         key = CONTAINER_OF(list_pop_back(&free_keys),
638                             struct ovsthread_key, list_node);
639     }
640     list_push_back(&inuse_keys, &key->list_node);
641     key->destructor = destructor;
642     ovs_mutex_unlock(&key_mutex);
643
644     *keyp = key;
645 }
646
647 /* Frees 'key'.  The destructor supplied to ovsthread_key_create(), if any, is
648  * not called.
649  *
650  * This function is similar to xpthread_key_delete(). */
651 void
652 ovsthread_key_delete(ovsthread_key_t key)
653 {
654     struct ovsthread_key_slots *slots;
655
656     ovs_mutex_lock(&key_mutex);
657
658     /* Move 'key' from 'inuse_keys' to 'free_keys'. */
659     list_remove(&key->list_node);
660     list_push_back(&free_keys, &key->list_node);
661
662     /* Clear this slot in all threads. */
663     LIST_FOR_EACH (slots, list_node, &slots_list) {
664         clear_slot(slots, key->index);
665     }
666
667     ovs_mutex_unlock(&key_mutex);
668 }
669
670 static void **
671 ovsthread_key_lookup__(const struct ovsthread_key *key)
672 {
673     struct ovsthread_key_slots *slots;
674     void **p2;
675
676     slots = pthread_getspecific(tsd_key);
677     if (!slots) {
678         slots = xzalloc(sizeof *slots);
679
680         ovs_mutex_lock(&key_mutex);
681         pthread_setspecific(tsd_key, slots);
682         list_push_back(&slots_list, &slots->list_node);
683         ovs_mutex_unlock(&key_mutex);
684     }
685
686     p2 = slots->p1[key->index / L2_SIZE];
687     if (!p2) {
688         p2 = xzalloc(L2_SIZE * sizeof *p2);
689         slots->p1[key->index / L2_SIZE] = p2;
690     }
691
692     return &p2[key->index % L2_SIZE];
693 }
694
695 /* Sets the value of thread-specific data item 'key', in the current thread, to
696  * 'value'.
697  *
698  * This function is similar to pthread_setspecific(). */
699 void
700 ovsthread_setspecific(ovsthread_key_t key, const void *value)
701 {
702     *ovsthread_key_lookup__(key) = CONST_CAST(void *, value);
703 }
704
705 /* Returns the value of thread-specific data item 'key' in the current thread.
706  *
707  * This function is similar to pthread_getspecific(). */
708 void *
709 ovsthread_getspecific(ovsthread_key_t key)
710 {
711     return *ovsthread_key_lookup__(key);
712 }
713 #endif