From ee7273b61bd9734a69a6457a7032fe40b1098b3a Mon Sep 17 00:00:00 2001 From: Aidan Chalk <aidan.chalk@stfc.ac.uk> Date: Fri, 21 Apr 2017 15:50:41 +0100 Subject: [PATCH] Fixed a bug with pthread IDs and made the scheduler threadsafe using our gnu99 not really safe method #13 --- src/qsched.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/qsched.c b/src/qsched.c index 30c9111..75286d3 100644 --- a/src/qsched.c +++ b/src/qsched.c @@ -52,6 +52,8 @@ char *qsched_timer_names[ qsched_timer_count ] = #if defined( HAVE_PTHREAD ) pthread_key_t qsched_pointer; +volatile int key_exists = 0; +volatile int pthread_key_mutex; #endif /** @@ -536,7 +538,7 @@ void qsched_run ( struct qsched *s , int nr_threads , qsched_funtype fun ) { int qsched_get_tid() { struct qsched *s = NULL; #if defined( HAVE_PTHREAD ) - s = (struct qsched *)pthread_getspecific(qsched_pointer); + s = (struct qsched *)pthread_getspecific(qsched_pointer); #endif if( s != NULL ) return *(int*)pthread_getspecific(s->thread_id_key); @@ -1655,7 +1657,12 @@ void qsched_init ( struct qsched *s , int nr_queues , int flags ) { error( "Failed to lock barrier mutex." ); } pthread_key_create(&s->thread_id_key, NULL); - pthread_key_create(&qsched_pointer, NULL); + while(__sync_val_compare_and_swap(&pthread_key_mutex, 0, 1)!=0); + if(!key_exists){ + pthread_key_create(&qsched_pointer, NULL); + key_exists=1; + } + pthread_key_mutex = 0; #endif /* Clear the timers. */ -- GitLab