@@ -87,19 +87,23 @@ static void thread_free(THREAD *tf)
8787 ACL_FIBER * fiber ;
8888 unsigned int i ;
8989
90+ #ifndef THREAD_LOCAL_DYNAMIC
9091 if (__thread_local == NULL ) {
9192 return ;
9293 }
9394
95+ __thread_local = NULL ;
96+ #endif
97+
9498 /* Free fiber object in the dead fibers link */
95- while ((head = ring_pop_head (& __thread_local -> dead ))) {
99+ while ((head = ring_pop_head (& tf -> dead ))) {
96100 fiber = RING_TO_APPL (head , ACL_FIBER , me );
97101 fiber_free (fiber );
98102 }
99103
100104 /* Free all possible aliving fiber object */
101- for (i = 0 ; i < __thread_local -> slot ; i ++ ) {
102- fiber_free (__thread_local -> fibers [i ]);
105+ for (i = 0 ; i < tf -> slot ; i ++ ) {
106+ fiber_free (tf -> fibers [i ]);
103107 }
104108
105109 if (tf -> fibers ) {
@@ -113,16 +117,10 @@ static void thread_free(THREAD *tf)
113117 mem_free (tf -> stack_buff );
114118#endif
115119
116- fiber_real_free ( tf -> original ) ;
120+ fiber = tf -> original ;
117121 mem_free (tf );
118122
119- #ifdef THREAD_LOCAL_DYNAMIC
120- if (thread_self () != main_thread_self ()) {
121- pthread_setspecific (__fiber_key , NULL );
122- }
123- #else
124- __thread_local = NULL ;
125- #endif
123+ fiber_real_free (fiber );
126124}
127125
128126static void free_atomic_onexit (void )
@@ -140,7 +138,7 @@ static void thread_exit(void *ctx)
140138 }
141139}
142140
143- static void thread_init (void )
141+ static void thread_once (void )
144142{
145143 if (pthread_key_create (& __fiber_key , thread_exit ) != 0 ) {
146144 msg_fatal ("%s(%d), %s: pthread_key_create error %s" ,
@@ -157,30 +155,10 @@ static void thread_init(void)
157155 atexit (free_atomic_onexit );
158156}
159157
160- static pthread_once_t __once_control = PTHREAD_ONCE_INIT ;
161-
162- static void fiber_check (void )
158+ static void thread_init (void )
163159{
164160 THREAD * local ;
165161
166- #ifndef THREAD_LOCAL_DYNAMIC
167- if (__thread_local != NULL ) {
168- return ;
169- }
170- #endif
171-
172- if (pthread_once (& __once_control , thread_init ) != 0 ) {
173- printf ("%s(%d), %s: pthread_once error %s\r\n" ,
174- __FILE__ , __LINE__ , __FUNCTION__ , last_serror ());
175- abort ();
176- }
177-
178- #ifdef THREAD_LOCAL_DYNAMIC
179- if (__thread_local != NULL ) {
180- return ;
181- }
182- #endif
183-
184162 local = (THREAD * ) mem_calloc (1 , sizeof (THREAD ));
185163
186164 local -> original = fiber_real_origin ();
@@ -213,6 +191,30 @@ static void fiber_check(void)
213191#endif
214192}
215193
194+ static pthread_once_t __once_control = PTHREAD_ONCE_INIT ;
195+
196+ static void fiber_check (void )
197+ {
198+ #ifndef THREAD_LOCAL_DYNAMIC
199+ if (__thread_local != NULL ) {
200+ return ;
201+ }
202+ #endif
203+
204+ if (pthread_once (& __once_control , thread_once ) != 0 ) {
205+ printf ("%s(%d), %s: pthread_once error %s\r\n" ,
206+ __FILE__ , __LINE__ , __FUNCTION__ , last_serror ());
207+ abort ();
208+ }
209+
210+ #ifdef THREAD_LOCAL_DYNAMIC
211+ if (__thread_local != NULL ) {
212+ return ;
213+ }
214+ #endif
215+ thread_init ();
216+ }
217+
216218ACL_FIBER * fiber_origin (void )
217219{
218220 return __thread_local -> original ;
0 commit comments