[xiph-cvs] cvs commit: thread thread.c thread.h
Michael Smith
msmith at xiph.org
Tue Sep 24 00:09:09 PDT 2002
msmith 02/09/24 03:09:09
Modified: . thread.c thread.h
Log:
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.
Revision Changes Path
1.16 +12 -1 thread/thread.c
Index: thread.c
===================================================================
RCS file: /usr/local/cvsroot/thread/thread.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- thread.c 16 Aug 2002 14:23:17 -0000 1.15
+++ thread.c 24 Sep 2002 07:09:08 -0000 1.16
@@ -102,6 +102,7 @@
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->name = strdup(name);
thread->create_time = time(NULL);
+ thread->detached = 0;
start->start_routine = start_routine;
start->arg = arg;
@@ -551,7 +553,7 @@
#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 @@
if (detach) {
pthread_detach(thread->sys_thread);
+ thread->detached = 1;
}
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
@@ -704,6 +707,7 @@
int i;
i = pthread_join(thread->sys_thread, &ret);
+ _free_thread(thread);
}
/* AVL tree functions */
@@ -768,6 +772,13 @@
return 1;
}
+static int _free_thread_if_detached(void *key)
+{
+ thread_t *t = key;
+ if(t->detached)
+ return _free_thread(key);
+ return 1;
+}
<p><p>1.7 +3 -0 thread/thread.h
Index: thread.h
===================================================================
RCS file: /usr/local/cvsroot/thread/thread.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- thread.h 10 Aug 2002 03:22:44 -0000 1.6
+++ thread.h 24 Sep 2002 07:09:08 -0000 1.7
@@ -36,6 +36,9 @@
char *file;
int line;
+ /* is the thread running detached? */
+ int detached;
+
/* the system specific thread */
pthread_t sys_thread;
} thread_t;
<p><p>--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list