Skip to content

Commit 41a96ed

Browse files
author
zhengshuxin
committed
Fixed bug that the thread local object can't be freed in fiber.c and fiber_io.c.
1 parent de74163 commit 41a96ed

2 files changed

Lines changed: 41 additions & 42 deletions

File tree

lib_fiber/c/src/fiber.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

128126
static 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+
216218
ACL_FIBER *fiber_origin(void)
217219
{
218220
return __thread_local->original;

lib_fiber/c/src/fiber_io.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ static void free_file(void *arg)
5353
file_event_unrefer(fe);
5454
}
5555

56-
static void thread_free(void *ctx)
56+
static void thread_free(FIBER_TLS *tf)
5757
{
58-
FIBER_TLS *tf = (FIBER_TLS *) ctx;
58+
#ifndef THREAD_LOCAL_DYNAMIC
5959
if (__thread_local == NULL) {
6060
return;
6161
}
6262

63+
__thread_local = NULL;
64+
#endif
65+
6366
if (tf->event) {
6467
event_free(tf->event);
6568
tf->event = NULL;
@@ -75,18 +78,12 @@ static void thread_free(void *ctx)
7578

7679
array_free(tf->cache, free_file);
7780
mem_free(tf);
78-
79-
#ifdef THREAD_LOCAL_DYNAMIC
80-
pthread_setspecific(__fiber_key, NULL);
81-
#else
82-
__thread_local = NULL;
83-
#endif
8481
}
8582

8683
static void thread_exit(void *ctx)
8784
{
8885
if (ctx) {
89-
thread_free(ctx);
86+
thread_free((FIBER_TLS *) ctx);
9087
}
9188
}
9289

0 commit comments

Comments
 (0)