On Mon, Jan 18, 2021 at 07:10:07AM +0100, Sebastian Huber wrote:
> Hello Jakub,
> 
> On 17/01/2021 17:04, Jakub Jelinek via Gcc-patches wrote:
> 
> > On Sun, Jan 17, 2021 at 04:25:24PM +0100, Andreas Schwab wrote:
> > > On Jan 17 2021, Jakub Jelinek via Gcc-patches wrote:
> > > 
> > > > Kwok, I guess you can reproduce it even on Linux with 
> > > > --disable-linux-futex
> > > And all targets that are not explicitly configured in
> > > libcomp/configure.tgt, where --enable-linux-futex is a no-op.
> > Completely untested patch (except for the linux futex version; and RTEMS
> > stuff is missing; I think it doesn't have a function for it but has a
> > counter in the struct, so perhaps fetch it manually from there), it is
> > Sunday, don't want to do more tonight:
> 
> here is the RTEMS part:

Ok for trunk with ChangeLog entry, thanks.

I have now committed this after bootstrapping/regtesting on x86_64-linux and
i686-linux and additionally building on x86_64-linux with
--disable-linux-futex and testing libgomp there.

2021-01-18  Jakub Jelinek  <ja...@redhat.com>

        * config/linux/sem.h (gomp_sem_getcount): New function.
        * config/posix/sem.h (gomp_sem_getcount): New function.
        * config/posix/sem.c (gomp_sem_getcount): New function.
        * config/accel/sem.h (gomp_sem_getcount): New function.
        * task.c (task_fulfilled_p): Use gomp_sem_getcount.
        (omp_fulfill_event): Likewise.

--- libgomp/config/linux/sem.h.jj       2021-01-04 10:25:56.160037625 +0100
+++ libgomp/config/linux/sem.h  2021-01-17 16:49:39.900750416 +0100
@@ -85,4 +85,13 @@ gomp_sem_post (gomp_sem_t *sem)
   if (__builtin_expect (count & SEM_WAIT, 0))
     gomp_sem_post_slow (sem);
 }
+
+static inline int
+gomp_sem_getcount (gomp_sem_t *sem)
+{
+  int count = __atomic_load_n (sem, MEMMODEL_RELAXED);
+  if ((count & SEM_WAIT) != 0)
+    return -1;
+  return count / SEM_INC;
+}
 #endif /* GOMP_SEM_H */
--- libgomp/config/posix/sem.h.jj       2021-01-04 10:25:56.166037557 +0100
+++ libgomp/config/posix/sem.h  2021-01-17 16:49:53.605593659 +0100
@@ -64,6 +64,8 @@ extern void gomp_sem_post (gomp_sem_t *s
 
 extern void gomp_sem_destroy (gomp_sem_t *sem);
 
+extern int gomp_sem_getcount (gomp_sem_t *sem);
+
 #else /* HAVE_BROKEN_POSIX_SEMAPHORES  */
 
 typedef sem_t gomp_sem_t;
@@ -84,5 +86,13 @@ static inline void gomp_sem_destroy (gom
 {
   sem_destroy (sem);
 }
+
+static inline int gomp_sem_getcount (gomp_sem_t *sem)
+{
+  int val;
+  if (sem_getvalue (sem, &val) < 0)
+    return -1;
+  return val;  
+}
 #endif /* doesn't HAVE_BROKEN_POSIX_SEMAPHORES  */
 #endif /* GOMP_SEM_H  */
--- libgomp/config/posix/sem.c.jj       2021-01-04 10:25:56.184037354 +0100
+++ libgomp/config/posix/sem.c  2021-01-17 16:52:00.207145847 +0100
@@ -112,6 +112,26 @@ void gomp_sem_destroy (gomp_sem_t *sem)
 
   return;
 }
+
+int gomp_sem_getcount (gomp_sem_t *sem)
+{
+  int ret, count;
+
+  ret = pthread_mutex_lock (&sem->mutex);
+  if (ret)
+    return -1;
+
+  count = sem->value;
+
+  ret = pthread_mutex_unlock (&sem->mutex);
+  if (ret)
+    return -1;
+
+  if (count < 0)
+    return -1;
+
+  return count;
+}
 #else /* HAVE_BROKEN_POSIX_SEMAPHORES  */
 void
 gomp_sem_wait (gomp_sem_t *sem)
--- libgomp/config/accel/sem.h.jj       2021-01-04 10:25:56.261036482 +0100
+++ libgomp/config/accel/sem.h  2021-01-17 16:53:13.381309036 +0100
@@ -62,4 +62,13 @@ gomp_sem_post (gomp_sem_t *sem)
 {
   (void) __atomic_add_fetch (sem, 1, MEMMODEL_RELEASE);
 }
+
+static inline int
+gomp_sem_getcount (gomp_sem_t *sem)
+{
+  int count = __atomic_load_n (sem, MEMMODEL_RELAXED);
+  if (count < 0)
+    return -1;
+  return count;
+}
 #endif /* GOMP_SEM_H */
--- libgomp/task.c.jj   2021-01-16 22:52:33.749412323 +0100
+++ libgomp/task.c      2021-01-17 16:54:54.315154777 +0100
@@ -330,7 +330,7 @@ gomp_task_handle_depend (struct gomp_tas
 static bool
 task_fulfilled_p (struct gomp_task *task)
 {
-  return __atomic_load_n (&task->completion_sem, __ATOMIC_RELAXED);
+  return gomp_sem_getcount (&task->completion_sem) > 0;
 }
 
 /* Called when encountering an explicit task directive.  If IF_CLAUSE is
@@ -2406,7 +2406,7 @@ omp_fulfill_event (omp_event_handle_t ev
   struct gomp_thread *thr = gomp_thread ();
   struct gomp_team *team = thr ? thr->ts.team : NULL;
 
-  if (__atomic_load_n (sem, __ATOMIC_RELAXED))
+  if (gomp_sem_getcount (sem) > 0)
     gomp_fatal ("omp_fulfill_event: %p event already fulfilled!\n", sem);
 
   gomp_debug (0, "omp_fulfill_event: %p\n", sem);


        Jakub

Reply via email to