From ea3779e74cc239c60a3193f6961d9a9d01fbc43a Mon Sep 17 00:00:00 2001 From: Aidan Chalk <aidan.chalk@stfc.ac.uk> Date: Fri, 21 Apr 2017 15:09:55 +0100 Subject: [PATCH] getting thread ID is now just a simple call that doesn't require the user to pass any values. Its slightly complex and relies on pthread_getspecific returning NULL when not called from a pthread - however this is what the pthread specification says so I think its fine --- examples/thread_test.c | 2 +- src/fqsched.F90 | 3 +-- src/qsched.c | 14 ++++++++++++-- src/qsched.h | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/thread_test.c b/examples/thread_test.c index 8ee63e0..15cebb9 100644 --- a/examples/thread_test.c +++ b/examples/thread_test.c @@ -13,7 +13,7 @@ void runner(int type, void* data) { - printf("%i\n", qsched_get_tid(&s)); + printf("%i\n", qsched_get_tid()); } diff --git a/src/fqsched.F90 b/src/fqsched.F90 index 22822d3..6a705d5 100644 --- a/src/fqsched.F90 +++ b/src/fqsched.F90 @@ -25,10 +25,9 @@ Module quicksched Interface - Integer(Kind=C_INT) Function qsched_get_tid(s) BIND(C) + Integer(Kind=C_INT) Function qsched_get_tid() BIND(C) Use, Intrinsic :: ISO_C_BINDING Implicit None - Type(C_PTR), VALUE :: s End Function Type(C_PTR) Function f_qsched_create() BIND(C) diff --git a/src/qsched.c b/src/qsched.c index 5fb406a..30c9111 100644 --- a/src/qsched.c +++ b/src/qsched.c @@ -50,6 +50,9 @@ char *qsched_timer_names[ qsched_timer_count ] = { "queue" , "qlock" , "lock" , "gettask" , "done" , "prepare" }; +#if defined( HAVE_PTHREAD ) +pthread_key_t qsched_pointer; +#endif /** * @brief Change the owner of a resource. @@ -412,6 +415,7 @@ void *qsched_pthread_run ( void *in ) { struct task *t; #if defined ( HAVE_PTHREAD ) pthread_setspecific(s->thread_id_key, &r->tid); + pthread_setspecific(qsched_pointer, r->s); #endif /* Main loop. */ while ( 1 ) { @@ -529,8 +533,12 @@ void qsched_run ( struct qsched *s , int nr_threads , qsched_funtype fun ) { } -int qsched_get_tid(struct qsched *s) { - if( !HAVE_OPENMP || s->flags & (qsched_flag_yield | qsched_flag_pthread ) ) +int qsched_get_tid() { + struct qsched *s = NULL; + #if defined( HAVE_PTHREAD ) + s = (struct qsched *)pthread_getspecific(qsched_pointer); + #endif + if( s != NULL ) return *(int*)pthread_getspecific(s->thread_id_key); else return omp_get_thread_num(); @@ -1557,6 +1565,7 @@ void qsched_free ( struct qsched *s ) { s->runners_count = 0; } pthread_key_delete(s->thread_id_key); + pthread_key_delete(qsched_pointer); #endif /* Clear the flags. */ @@ -1646,6 +1655,7 @@ 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); #endif /* Clear the timers. */ diff --git a/src/qsched.h b/src/qsched.h index b92c67d..47bf8e7 100644 --- a/src/qsched.h +++ b/src/qsched.h @@ -212,7 +212,7 @@ void qsched_reset ( struct qsched *s ); void qsched_addtask_dynamic ( struct qsched *s , int type , unsigned int flags , void *data , int data_size , int cost , qsched_res_t *locks , int nr_locks , qsched_res_t *uses , int nr_uses ); void qsched_ensure ( struct qsched *s , int nr_tasks , int nr_res , int nr_deps , int nr_locks , int nr_uses , int size_data ); void qsched_res_own ( struct qsched *s , qsched_res_t res , int owner ); -int qsched_get_tid(struct qsched *s); +int qsched_get_tid(); struct qsched * f_qsched_create(); void f_qsched_destroy( struct qsched *s); -- GitLab