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
dummy_get_tl_lock_caller_count(void)26 static struct call_tl_lock *dummy_get_tl_lock_caller_count(void)
27 {
28 return NULL;
29 }
30
31 weak_alias(dummy_get_tl_lock_caller_count, get_tl_lock_caller_count);
32
__pthread_key_atfork(int who)33 void __pthread_key_atfork(int who)
34 {
35 if (who<0) __pthread_rwlock_rdlock(&key_lock);
36 else if (!who) __pthread_rwlock_unlock(&key_lock);
37 else key_lock = (pthread_rwlock_t)PTHREAD_RWLOCK_INITIALIZER;
38 }
39
__pthread_key_create(pthread_key_t * k,void (* dtor)(void *))40 int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
41 {
42 pthread_t self = __pthread_self();
43
44 /* This can only happen in the main thread before
45 * pthread_create has been called. */
46 if (!self->tsd) self->tsd = __pthread_tsd_main;
47
48 /* Purely a sentinel value since null means slot is free. */
49 if (!dtor) dtor = nodtor;
50
51 __pthread_rwlock_wrlock(&key_lock);
52 pthread_key_t j = next_key;
53 do {
54 if (!keys[j]) {
55 keys[next_key = *k = j] = dtor;
56 __pthread_rwlock_unlock(&key_lock);
57 return 0;
58 }
59 } while ((j=(j+1)%PTHREAD_KEYS_MAX) != next_key);
60
61 __pthread_rwlock_unlock(&key_lock);
62 return EAGAIN;
63 }
64
__pthread_key_delete(pthread_key_t k)65 int __pthread_key_delete(pthread_key_t k)
66 {
67 sigset_t set;
68 pthread_t self = __pthread_self(), td=self;
69
70 __block_app_sigs(&set);
71 __pthread_rwlock_wrlock(&key_lock);
72
73 __tl_lock();
74 if (get_tl_lock_caller_count()) {
75 get_tl_lock_caller_count()->__pthread_key_delete_tl_lock++;
76 }
77 do td->tsd[k] = 0;
78 while ((td=td->next)!=self);
79 if (get_tl_lock_caller_count()) {
80 get_tl_lock_caller_count()->__pthread_key_delete_tl_lock--;
81 }
82 __tl_unlock();
83
84 keys[k] = 0;
85
86 __pthread_rwlock_unlock(&key_lock);
87 __restore_sigs(&set);
88
89 return 0;
90 }
91
__pthread_tsd_run_dtors()92 void __pthread_tsd_run_dtors()
93 {
94 pthread_t self = __pthread_self();
95 int i, j;
96 for (j=0; self->tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
97 __pthread_rwlock_rdlock(&key_lock);
98 self->tsd_used = 0;
99 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
100 void *val = self->tsd[i];
101 void (*dtor)(void *) = keys[i];
102 self->tsd[i] = 0;
103 if (val && dtor && dtor != nodtor) {
104 __pthread_rwlock_unlock(&key_lock);
105 dtor(val);
106 __pthread_rwlock_rdlock(&key_lock);
107 }
108 }
109 __pthread_rwlock_unlock(&key_lock);
110 }
111 }
112
__pthread_tsd_run_dtors_ex1()113 void __pthread_tsd_run_dtors_ex1()
114 {
115 pthread_t self = __pthread_self();
116 int i, j;
117 bool tsd_used = true;
118 for (j=0; tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
119 __pthread_rwlock_rdlock(&key_lock);
120 tsd_used = false;
121 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
122 void *val = self->tsd[i];
123 void (*dtor)(void *) = keys[i];
124 self->tsd[i] = 0;
125 if (val && dtor && dtor != nodtor) {
126 __pthread_rwlock_unlock(&key_lock);
127 dtor(val);
128 tsd_used = true;
129 __pthread_rwlock_rdlock(&key_lock);
130 }
131 }
132 __pthread_rwlock_unlock(&key_lock);
133 }
134 }
135
136 weak_alias(__pthread_key_create, pthread_key_create);
137 weak_alias(__pthread_key_delete, pthread_key_delete);
138 weak_alias(__pthread_tsd_run_dtors_ex1, pthread_tsd_run_dtors);