Changeset: bfcea6701f71 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=bfcea6701f71 Modified Files: gdk/gdk_utils.c Branch: default Log Message:
Use atomic pointer variable. diffs (92 lines): diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c --- a/gdk/gdk_utils.c +++ b/gdk/gdk_utils.c @@ -699,28 +699,29 @@ GDKexiting(void) return (bool) (ATOMIC_GET(&GDKstopped) > 0); } -static struct serverthread { +struct serverthread { struct serverthread *next; MT_Id pid; -} *serverthread; +}; +static ATOMIC_PTR_TYPE serverthread = ATOMIC_PTR_VAR_INIT(NULL); void GDKprepareExit(void) { + struct serverthread *st; + if (ATOMIC_ADD(&GDKstopped, 1) > 0) return; THRDDEBUG dump_threads(); - MT_lock_set(&GDKthreadLock); - while (serverthread != NULL) { - struct serverthread *st = serverthread; - serverthread = st->next; - MT_lock_unset(&GDKthreadLock); + /* we're saved from the ABA problem in this code because it is + * only executed by one thread */ + while ((st = ATOMIC_PTR_GET(&serverthread)) != NULL) { + while (!ATOMIC_PTR_CAS(&serverthread, &st, st->next)) + ; MT_join_thread(st->pid); GDKfree(st); - MT_lock_set(&GDKthreadLock); } - MT_lock_unset(&GDKthreadLock); join_detached_threads(); } @@ -734,15 +735,15 @@ GDKregister(MT_Id pid) if ((st = GDKmalloc(sizeof(struct serverthread))) == NULL) return; st->pid = pid; - MT_lock_set(&GDKthreadLock); - st->next = serverthread; - serverthread = st; - MT_lock_unset(&GDKthreadLock); + st->next = ATOMIC_PTR_GET(&serverthread); + while (!ATOMIC_PTR_CAS(&serverthread, &st->next, st)) + ; } void GDKreset(int status) { + struct serverthread *st; MT_Id pid = MT_getpid(); assert(GDKexiting()); @@ -756,23 +757,21 @@ GDKreset(int status) GDKval = NULL; } - MT_lock_set(&GDKthreadLock); - while (serverthread != NULL) { - struct serverthread *st = serverthread; - serverthread = st->next; - MT_lock_unset(&GDKthreadLock); + /* we're saved from the ABA problem in this code because it is + * only executed by one thread */ + while ((st = ATOMIC_PTR_GET(&serverthread)) != NULL) { + while (!ATOMIC_PTR_CAS(&serverthread, &st, st->next)) + ; MT_join_thread(st->pid); GDKfree(st); - MT_lock_set(&GDKthreadLock); } - MT_lock_unset(&GDKthreadLock); join_detached_threads(); if (status == 0) { /* they had their chance, now kill them */ bool killed = false; MT_lock_set(&GDKthreadLock); - assert(serverthread == NULL); + assert(ATOMIC_PTR_GET(&serverthread) == NULL); for (Thread t = GDKthreads; t < GDKthreads + THREADS; t++) { MT_Id victim; if ((victim = (MT_Id) ATOMIC_GET(&t->pid)) != 0) { _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list