[Openmp-commits] [openmp] r252487 - Fixes to wait-loop code

Jonathan Peyton via Openmp-commits openmp-commits at lists.llvm.org
Mon Nov 9 08:31:51 PST 2015


Author: jlpeyton
Date: Mon Nov  9 10:31:51 2015
New Revision: 252487

URL: http://llvm.org/viewvc/llvm-project?rev=252487&view=rev
Log:
Fixes to wait-loop code

1) Add get_ptr_type() method to all wait flag types.
2) Flag in sleep_loc may change type by the time the resume is called from
   __kmp_null_resume_wrapper. We use get_ptr_type to obtain the real type
   and compare it to the casted object received. If they don't match, we know
   the flag has changed (already resumed and replaced by another flag). If they
   match, it doesn't hurt to go ahead and resume it.

Differential Revision: http://reviews.llvm.org/D14458

Modified:
    openmp/trunk/runtime/src/kmp_tasking.c
    openmp/trunk/runtime/src/kmp_wait_release.h
    openmp/trunk/runtime/src/z_Linux_util.c
    openmp/trunk/runtime/src/z_Windows_NT_util.c

Modified: openmp/trunk/runtime/src/kmp_tasking.c
URL: http://llvm.org/viewvc/llvm-project/openmp/trunk/runtime/src/kmp_tasking.c?rev=252487&r1=252486&r2=252487&view=diff
==============================================================================
--- openmp/trunk/runtime/src/kmp_tasking.c (original)
+++ openmp/trunk/runtime/src/kmp_tasking.c Mon Nov  9 10:31:51 2015
@@ -39,6 +39,7 @@ static void __kmp_bottom_half_finish_pro
 
 static inline void __kmp_null_resume_wrapper(int gtid, volatile void *flag) {
     if (!flag) return;
+    // Attempt to wake up a thread: examine its type and call appropriate template
     switch (((kmp_flag_64 *)flag)->get_type()) {
     case flag32: __kmp_resume_32(gtid, NULL); break;
     case flag64: __kmp_resume_64(gtid, NULL); break;

Modified: openmp/trunk/runtime/src/kmp_wait_release.h
URL: http://llvm.org/viewvc/llvm-project/openmp/trunk/runtime/src/kmp_wait_release.h?rev=252487&r1=252486&r2=252487&view=diff
==============================================================================
--- openmp/trunk/runtime/src/kmp_wait_release.h (original)
+++ openmp/trunk/runtime/src/kmp_wait_release.h Mon Nov  9 10:31:51 2015
@@ -438,6 +438,7 @@ class kmp_flag_32 : public kmp_basic_fla
                             USE_ITT_BUILD_ARG(itt_sync_obj));
     }
     void release() { __kmp_release_template(this); }
+    flag_type get_ptr_type() { return flag32; }
 };
 
 class kmp_flag_64 : public kmp_basic_flag<kmp_uint64> {
@@ -458,6 +459,7 @@ class kmp_flag_64 : public kmp_basic_fla
                             USE_ITT_BUILD_ARG(itt_sync_obj));
     }
     void release() { __kmp_release_template(this); }
+    flag_type get_ptr_type() { return flag64; }
 };
 
 // Hierarchical 64-bit on-core barrier instantiation
@@ -551,6 +553,7 @@ public:
     }
     kmp_uint8 *get_stolen() { return NULL; }
     enum barrier_type get_bt() { return bt; }
+    flag_type get_ptr_type() { return flag_oncore; }
 };
 
 

Modified: openmp/trunk/runtime/src/z_Linux_util.c
URL: http://llvm.org/viewvc/llvm-project/openmp/trunk/runtime/src/z_Linux_util.c?rev=252487&r1=252486&r2=252487&view=diff
==============================================================================
--- openmp/trunk/runtime/src/z_Linux_util.c (original)
+++ openmp/trunk/runtime/src/z_Linux_util.c Mon Nov  9 10:31:51 2015
@@ -1837,11 +1837,12 @@ static inline void __kmp_resume_template
     status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
     KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
 
-    if (!flag) {
+    if (!flag) { // coming from __kmp_null_resume_wrapper
         flag = (C *)th->th.th_sleep_loc;
     }
 
-    if (!flag) {
+    // First, check if the flag is null or its type has changed. If so, someone else woke it up.
+    if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type simply shows what flag was cast to
         KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
                        gtid, target_gtid, NULL ) );
         status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );

Modified: openmp/trunk/runtime/src/z_Windows_NT_util.c
URL: http://llvm.org/viewvc/llvm-project/openmp/trunk/runtime/src/z_Windows_NT_util.c?rev=252487&r1=252486&r2=252487&view=diff
==============================================================================
--- openmp/trunk/runtime/src/z_Windows_NT_util.c (original)
+++ openmp/trunk/runtime/src/z_Windows_NT_util.c Mon Nov  9 10:31:51 2015
@@ -455,11 +455,12 @@ static inline void __kmp_resume_template
     __kmp_suspend_initialize_thread( th );
     __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
 
-    if (!flag) {
+    if (!flag) { // coming from __kmp_null_resume_wrapper
         flag = (C *)th->th.th_sleep_loc;
     }
 
-    if (!flag) {
+    // First, check if the flag is null or its type has changed. If so, someone else woke it up.
+    if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type simply shows what flag was cast to
         KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
                        gtid, target_gtid, NULL ) );
         __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );




More information about the Openmp-commits mailing list