mirror of
https://github.com/netdata/netdata.git
synced 2025-04-10 08:07:34 +00:00
fix random crashes on pthread_detach() (#16137)
This commit is contained in:
parent
f97eef550e
commit
3581d73519
1 changed files with 8 additions and 3 deletions
|
@ -9,8 +9,8 @@ static pthread_attr_t *netdata_threads_attr = NULL;
|
|||
|
||||
typedef struct {
|
||||
void *arg;
|
||||
pthread_t *thread;
|
||||
char tag[NETDATA_THREAD_NAME_MAX + 1];
|
||||
SPINLOCK detach_lock;
|
||||
void *(*start_routine) (void *);
|
||||
NETDATA_THREAD_OPTIONS options;
|
||||
} NETDATA_THREAD;
|
||||
|
@ -185,6 +185,7 @@ static void thread_cleanup(void *ptr) {
|
|||
NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
|
||||
netdata_log_error("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);
|
||||
}
|
||||
spinlock_lock(&netdata_thread->detach_lock);
|
||||
|
||||
if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
|
||||
netdata_log_info("thread with task id %d finished", gettid());
|
||||
|
@ -199,6 +200,7 @@ static void thread_cleanup(void *ptr) {
|
|||
|
||||
netdata_thread->tag[0] = '\0';
|
||||
|
||||
spinlock_unlock(&netdata_thread->detach_lock);
|
||||
freez(netdata_thread);
|
||||
netdata_thread = NULL;
|
||||
}
|
||||
|
@ -281,13 +283,15 @@ static void *netdata_thread_init(void *ptr) {
|
|||
}
|
||||
|
||||
int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg) {
|
||||
NETDATA_THREAD *info = mallocz(sizeof(NETDATA_THREAD));
|
||||
NETDATA_THREAD *info = callocz(1, sizeof(NETDATA_THREAD));
|
||||
info->arg = arg;
|
||||
info->thread = thread;
|
||||
info->start_routine = start_routine;
|
||||
info->options = options;
|
||||
strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX);
|
||||
|
||||
spinlock_init(&info->detach_lock);
|
||||
spinlock_lock(&info->detach_lock);
|
||||
|
||||
int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info);
|
||||
if(ret != 0)
|
||||
netdata_log_error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
|
||||
|
@ -300,6 +304,7 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR
|
|||
}
|
||||
}
|
||||
|
||||
spinlock_unlock(&info->detach_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue