diff mbox

libgomp, OpenACC: Fix locking of cached_base_dev (guarded by acc_device_lock)

Message ID 87lgsqciyw.fsf@hertz.schwinge.homeip.net
State New
Headers show

Commit Message

Thomas Schwinge Feb. 28, 2017, 5:40 p.m. UTC
Hi!

We'd noticed before some locking issues in libgomp; in r245783 I just
committed the following to gomp-4_0-branch:

commit 921620f947485002e56f736d4ca18bd2ae33e05b
Author: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Tue Feb 28 17:35:49 2017 +0000

    libgomp, OpenACC: Fix locking of cached_base_dev (guarded by acc_device_lock)
    
            libgomp/
            * oacc-init.c (goacc_register, acc_init, goacc_lazy_initialize):
            Fix locking of cached_base_dev (guarded by acc_device_lock).
            (goacc_lazy_initialize): Don't call acc_init; copy code here,
            instead.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gomp-4_0-branch@245783 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog.gomp |  7 +++++++
 libgomp/oacc-init.c    | 25 +++++++++++++------------
 2 files changed, 20 insertions(+), 12 deletions(-)



Grüße
 Thomas
diff mbox

Patch

diff --git libgomp/ChangeLog.gomp libgomp/ChangeLog.gomp
index 637be06..af6a28b 100644
--- libgomp/ChangeLog.gomp
+++ libgomp/ChangeLog.gomp
@@ -1,3 +1,10 @@ 
+2017-02-28  Thomas Schwinge  <thomas@codesourcery.com>
+
+	* oacc-init.c (goacc_register, acc_init, goacc_lazy_initialize):
+	Fix locking of cached_base_dev (guarded by acc_device_lock).
+	(goacc_lazy_initialize): Don't call acc_init; copy code here,
+	instead.
+
 2017-02-27  Chung-Lin Tang  <cltang@codesourcery.com>
 
 	* testsuite/libgomp.oacc-c-c++-common/gang-private-1.c: New test.
diff --git libgomp/oacc-init.c libgomp/oacc-init.c
index 71e9ccf..05bb663 100644
--- libgomp/oacc-init.c
+++ libgomp/oacc-init.c
@@ -64,6 +64,10 @@  static gomp_mutex_t goacc_thread_lock;
    grouped by device in target.c:devices).  */
 static struct gomp_device_descr *dispatchers[_ACC_device_hwm] = { 0 };
 
+/* Register a device.
+
+   ACC_DEVICE_LOCK must be held before calling this function.  */
+
 attribute_hidden void
 goacc_register (struct gomp_device_descr *disp)
 {
@@ -71,15 +75,11 @@  goacc_register (struct gomp_device_descr *disp)
   if (disp->target_id != 0)
     return;
 
-  gomp_mutex_lock (&acc_device_lock);
-
   assert (acc_device_type (disp->type) != acc_device_none
 	  && acc_device_type (disp->type) != acc_device_default
 	  && acc_device_type (disp->type) != acc_device_not_host);
   assert (!dispatchers[disp->type]);
   dispatchers[disp->type] = disp;
-
-  gomp_mutex_unlock (&acc_device_lock);
 }
 
 /* OpenACC names some things a little differently.  */
@@ -449,13 +449,10 @@  goacc_attach_host_thread_to_device (int ord)
 void
 acc_init (acc_device_t d)
 {
+  gomp_mutex_lock (&acc_device_lock);
   if (!cached_base_dev)
     gomp_init_targets_once ();
-
-  gomp_mutex_lock (&acc_device_lock);
-
   cached_base_dev = acc_init_1 (d);
-
   gomp_mutex_unlock (&acc_device_lock);
   
   goacc_attach_host_thread_to_device (-1);
@@ -704,12 +701,16 @@  attribute_hidden void
 goacc_lazy_initialize (void)
 {
   struct goacc_thread *thr = goacc_thread ();
-
   if (thr && thr->dev)
     return;
 
+  gomp_mutex_lock (&acc_device_lock);
   if (!cached_base_dev)
-    acc_init (acc_device_default);
-  else
-    goacc_attach_host_thread_to_device (-1);
+    {
+      gomp_init_targets_once ();
+      cached_base_dev = acc_init_1 (acc_device_default);
+    }
+  gomp_mutex_unlock (&acc_device_lock);
+
+  goacc_attach_host_thread_to_device (-1);
 }