1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00

Bugfix: thread_join is often called after a thread has already exited, which it

does using thread_exit(). thread_exit() was freeing the thread structure, so
thread_join was using freed memory. Rearrange things so that if the thread
is detached, the freeing happens in thread_join instead.

svn path=/trunk/thread/; revision=3944
This commit is contained in:
Michael Smith 2002-09-24 07:09:08 +00:00
parent 0dcf81c72c
commit fc3bc41c22
2 changed files with 15 additions and 1 deletions

View File

@ -102,6 +102,7 @@ static int _compare_mutexes(void *compare_arg, void *a, void *b);
static int _compare_threads(void *compare_arg, void *a, void *b);
static int _free_mutex(void *key);
static int _free_thread(void *key);
static int _free_thread_if_detached(void *key);
/* mutex fuctions */
static void _mutex_create(mutex_t *mutex);
@ -254,6 +255,7 @@ thread_t *thread_create_c(char *name, void *(*start_routine)(void *), void *arg,
thread->name = strdup(name);
thread->create_time = time(NULL);
thread->detached = 0;
start->start_routine = start_routine;
start->arg = arg;
@ -551,7 +553,7 @@ void thread_exit_c(int val, int line, char *file)
#endif
_mutex_lock(&_threadtree_mutex);
avl_delete(_threadtree, th, _free_thread);
avl_delete(_threadtree, th, _free_thread_if_detached);
_mutex_unlock(&_threadtree_mutex);
}
@ -614,6 +616,7 @@ static void *_start_routine(void *arg)
if (detach) {
pthread_detach(thread->sys_thread);
thread->detached = 1;
}
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
@ -704,6 +707,7 @@ void thread_join(thread_t *thread)
int i;
i = pthread_join(thread->sys_thread, &ret);
_free_thread(thread);
}
/* AVL tree functions */
@ -768,6 +772,13 @@ static int _free_thread(void *key)
return 1;
}
static int _free_thread_if_detached(void *key)
{
thread_t *t = key;
if(t->detached)
return _free_thread(key);
return 1;
}

View File

@ -36,6 +36,9 @@ typedef struct thread_tag {
char *file;
int line;
/* is the thread running detached? */
int detached;
/* the system specific thread */
pthread_t sys_thread;
} thread_t;