Changeset: 63f23a3d619b for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=63f23a3d619b
Modified Files:
        gdk/gdk_system.c
Branch: Jun2016
Log Message:

Only add thread to list of to-be-joined threads after successful creation

This avoids a potential race condition if two threads simultaneously
start a new thread where one new threads exits quickly and the other
thread joins and cleans up the entry of the first thread before it
reads from the entry.


diffs (44 lines):

diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -477,14 +477,6 @@ rm_posthread_locked(struct posthread *p)
 }
 
 static void
-rm_posthread(struct posthread *p)
-{
-       pthread_mutex_lock(&posthread_lock);
-       rm_posthread_locked(p);
-       pthread_mutex_unlock(&posthread_lock);
-}
-
-static void
 thread_starter(void *arg)
 {
        struct posthread *p = (struct posthread *) arg;
@@ -574,10 +566,6 @@ MT_create_thread(MT_Id *t, void (*f) (vo
                p->func = f;
                p->arg = arg;
                p->exited = 0;
-               pthread_mutex_lock(&posthread_lock);
-               p->next = posthreads;
-               posthreads = p;
-               pthread_mutex_unlock(&posthread_lock);
                f = thread_starter;
                arg = p;
                newtp = &p->tid;
@@ -592,8 +580,13 @@ MT_create_thread(MT_Id *t, void (*f) (vo
 #else
                *t = (MT_Id) (((size_t) *newtp) + 1);   /* use pthread-id + 1 */
 #endif
+               if (p) {
+                       pthread_mutex_lock(&posthread_lock);
+                       p->next = posthreads;
+                       posthreads = p;
+                       pthread_mutex_unlock(&posthread_lock);
+               }
        } else if (p) {
-               rm_posthread(p);
                free(p);
        }
 #ifdef HAVE_PTHREAD_SIGMASK
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to