]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add error checking to mutexes and condition variables.
authorThomas Koenig <tkoenig@gcc.gnu.org>
Sun, 10 Jan 2021 12:21:26 +0000 (13:21 +0100)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Sun, 10 Jan 2021 12:25:09 +0000 (13:25 +0100)
After the discussions about PTHREAD_PROCESS_SHARED and after
observing that on Cygwin the program simply hangs because
this flag is not implemented there, the best way is to check
for errors and exit with a clear error message instead of
some mysterious hang.

libgfortran/ChangeLog:

* caf_shared/util.c (ERRCHECK): New macro.
(initialize_shared_mutex): Use it to check return codes.
(initialize_shared_condition): Likewise.

libgfortran/caf_shared/util.c

index 683e2f36d63e7b78e32308d7f82b6d79105aa5a4..65b347e7a8c61f17f793eca61df9e60d9ac3abbf 100644 (file)
@@ -48,24 +48,33 @@ next_power_of_two (size_t size)
   return 1 << (PTR_BITS - __builtin_clzl (size - 1));
 }
 
+#define ERRCHECK(a) do {       \
+    int rc = a;                        \
+    if (rc) {                  \
+      errno = rc;              \
+      perror (#a " failed");   \
+      exit (1);                        \
+    }                          \
+} while(0)
+
 void
 initialize_shared_mutex (pthread_mutex_t *mutex)
 {
   pthread_mutexattr_t mattr;
-  pthread_mutexattr_init (&mattr);
-  pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED);
-  pthread_mutex_init (mutex, &mattr);
-  pthread_mutexattr_destroy (&mattr);
+  ERRCHECK (pthread_mutexattr_init (&mattr));
+  ERRCHECK (pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED));
+  ERRCHECK (pthread_mutex_init (mutex, &mattr));
+  ERRCHECK (pthread_mutexattr_destroy (&mattr));
 }
 
 void
 initialize_shared_condition (pthread_cond_t *cond)
 {
   pthread_condattr_t cattr;
-  pthread_condattr_init (&cattr);
-  pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED);
-  pthread_cond_init (cond, &cattr);
-  pthread_condattr_destroy (&cattr);
+  ERRCHECK (pthread_condattr_init (&cattr));
+  ERRCHECK (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED));
+  ERRCHECK (pthread_cond_init (cond, &cattr));
+  ERRCHECK (pthread_condattr_destroy (&cattr));
 }
 
 int