1 #include "pthread_impl.h"
2 #include "fork_impl.h"
3 #include "pthread_ffrt.h"
4 #include <stdbool.h>
5
6 volatile size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
7 void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
8
9 static void (*keys[PTHREAD_KEYS_MAX])(void *);
10
11 static pthread_rwlock_t key_lock = PTHREAD_RWLOCK_INITIALIZER;
12
13 static pthread_key_t next_key;
14
nodtor(void *dummy)15 static void nodtor(void *dummy)
16 {
17 }
18
dummy_0(void)19 static void dummy_0(void)
20 {
21 }
22
23 weak_alias(dummy_0, __tl_lock);
24 weak_alias(dummy_0, __tl_unlock);
25
__pthread_key_atfork(int who)26 void __pthread_key_atfork(int who)
27 {
28 if (who<0) __pthread_rwlock_rdlock(&key_lock);
29 else if (!who) __pthread_rwlock_unlock(&key_lock);
30 else key_lock = (pthread_rwlock_t)PTHREAD_RWLOCK_INITIALIZER;
31 }
32
__pthread_key_create(pthread_key_t *k, void (*dtor)(void *))33 int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
34 {
35 pthread_t self = __pthread_self();
36
37 /* This can only happen in the main thread before
38 * pthread_create has been called. */
39 if (!self->tsd) self->tsd = __pthread_tsd_main;
40
41 /* Purely a sentinel value since null means slot is free. */
42 if (!dtor) dtor = nodtor;
43
44 __pthread_rwlock_wrlock(&key_lock);
45 pthread_key_t j = next_key;
46 do {
47 if (!keys[j]) {
48 keys[next_key = *k = j] = dtor;
49 __pthread_rwlock_unlock(&key_lock);
50 return 0;
51 }
52 } while ((j=(j+1)%PTHREAD_KEYS_MAX) != next_key);
53
54 __pthread_rwlock_unlock(&key_lock);
55 return EAGAIN;
56 }
57
__pthread_key_delete(pthread_key_t k)58 int __pthread_key_delete(pthread_key_t k)
59 {
60 sigset_t set;
61 pthread_t self = __pthread_self(), td=self;
62
63 __block_app_sigs(&set);
64 __pthread_rwlock_wrlock(&key_lock);
65
66 __tl_lock();
67 do td->tsd[k] = 0;
68 while ((td=td->next)!=self);
69 __tl_unlock();
70
71 keys[k] = 0;
72
73 __pthread_rwlock_unlock(&key_lock);
74 __restore_sigs(&set);
75
76 return 0;
77 }
78
__pthread_tsd_run_dtorsnull79 void __pthread_tsd_run_dtors()
80 {
81 pthread_t self = __pthread_self();
82 int i, j;
83 for (j=0; self->tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
84 __pthread_rwlock_rdlock(&key_lock);
85 self->tsd_used = 0;
86 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
87 void *val = self->tsd[i];
88 void (*dtor)(void *) = keys[i];
89 self->tsd[i] = 0;
90 if (val && dtor && dtor != nodtor) {
91 __pthread_rwlock_unlock(&key_lock);
92 dtor(val);
93 __pthread_rwlock_rdlock(&key_lock);
94 }
95 }
96 __pthread_rwlock_unlock(&key_lock);
97 }
98 }
99
__pthread_tsd_run_dtors_ex1null100 void __pthread_tsd_run_dtors_ex1()
101 {
102 pthread_t self = __pthread_self();
103 int i, j;
104 bool tsd_used = true;
105 for (j=0; tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
106 __pthread_rwlock_rdlock(&key_lock);
107 tsd_used = false;
108 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
109 void *val = self->tsd[i];
110 void (*dtor)(void *) = keys[i];
111 self->tsd[i] = 0;
112 if (val && dtor && dtor != nodtor) {
113 __pthread_rwlock_unlock(&key_lock);
114 dtor(val);
115 tsd_used = true;
116 __pthread_rwlock_rdlock(&key_lock);
117 }
118 }
119 __pthread_rwlock_unlock(&key_lock);
120 }
121 }
122
123 weak_alias(__pthread_key_create, pthread_key_create);
124 weak_alias(__pthread_key_delete, pthread_key_delete);
125 weak_alias(__pthread_tsd_run_dtors_ex1, pthread_tsd_run_dtors);