fix phtread-detatch() call (#16760)
* fix phtread-detatch() call * fix variable tag
Costa Tsaousis committed
Jan 11, 2024 at 20:38 UTC
af042f7470c8e8810256953d4d0e6c9ab5321995
1 file changed
+8
-15
libnetdata/threads/threads.c
+8
-15
@@ -10,7 +10,6 @@ static pthread_attr_t *netdata_threads_attr = NULL;
10
typedef struct {
11
void *arg;
12
char tag[NETDATA_THREAD_NAME_MAX + 1];
13
- SPINLOCK detach_lock;
13
void *(*start_routine) (void *);
14
NETDATA_THREAD_OPTIONS options;
15
} NETDATA_THREAD;
@@ -183,7 +182,6 @@ static void thread_cleanup(void *ptr) {
182
NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
183
nd_log(NDLS_DAEMON, NDLP_ERR, "THREADS: internal error - thread local variable does not match the one passed to this function. Expected thread '%s', passed thread '%s'", netdata_thread->tag, info->tag);
184
}
186
- spinlock_lock(&netdata_thread->detach_lock);
185
186
if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
187
nd_log(NDLS_DAEMON, NDLP_DEBUG, "thread with task id %d finished", gettid());
@@ -198,7 +196,6 @@ static void thread_cleanup(void *ptr) {
196
197
netdata_thread->tag[0] = '\0';
198
201
- spinlock_unlock(&netdata_thread->detach_lock);
199
freez(netdata_thread);
200
netdata_thread = NULL;
201
}
@@ -275,6 +272,14 @@ static void *netdata_thread_init(void *ptr) {
272
273
netdata_thread_set_tag(netdata_thread->tag);
274
275
+ if (!(netdata_thread->options & NETDATA_THREAD_OPTION_JOINABLE)) {
276
+ int rc = pthread_detach(pthread_self());
277
+ if (rc != 0)
278
+ nd_log(NDLS_DAEMON, NDLP_WARNING,
279
+ "cannot request detach of newly created %s thread. pthread_detach() failed with code %d",
280
+ netdata_thread->tag, rc);
281
+ }
282
+
283
void *ret = NULL;
284
pthread_cleanup_push(thread_cleanup, ptr) {
285
ret = netdata_thread->start_routine(netdata_thread->arg);
@@ -291,22 +296,10 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR
296
info->options = options;
297
strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX);
298
294
- spinlock_init(&info->detach_lock);
295
- spinlock_lock(&info->detach_lock);
296
-
299
int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info);
300
if(ret != 0)
301
nd_log(NDLS_DAEMON, NDLP_ERR, "failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
302
301
- else {
302
- if (!(options & NETDATA_THREAD_OPTION_JOINABLE)) {
303
- int ret2 = pthread_detach(*thread);
304
- if (ret2 != 0)
305
- nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot request detach of newly created %s thread. pthread_detach() failed with code %d", tag, ret2);
306
- }
307
- }
308
-
309
- spinlock_unlock(&info->detach_lock);
303
return ret;
304
}
305