[m-rev.] for post-commit review: isolate all dependencies on POSIX unnamed semaphores

Julien Fischer jfischer at opturion.com
Thu Sep 29 14:00:02 AEST 2016


For post-commit review by Paul or Peter.

------------------------------------

Isolate all dependencies on POSIX unnamed semaphores.

Isolate all dependencies on POSIX unnamed semaphores to the runtime's
mercury_thread module.  This is a step towards fixing bug #357.

runtime/mercury_thread.{h,c}:
     Provide wrappers for the functions sem_destroy and sem_timedwait.
     The latter does not exist on OS X, so (for now) just abort if it
     is called.

runtime/mercury_context.c:
library/thread.m:
      Use the above wrappers instead of calling the sem_* functions
      directly.

Julien.

diff --git a/library/thread.m b/library/thread.m
index 9f1e96f..c54e5c8 100644
--- a/library/thread.m
+++ b/library/thread.m
@@ -540,7 +540,7 @@ INIT mercury_sys_init_thread_modules

      ML_incr_thread_barrier_count();

-    sem_init(&args.sem, 0, 0);
+    MR_sem_init(&args.sem, 0);
      args.goal = goal;
      args.thread_local_mutables =
          MR_clone_thread_local_mutables(MR_THREAD_LOCAL_MUTABLES);
@@ -556,7 +556,7 @@ INIT mercury_sys_init_thread_modules
          MR_SEM_WAIT(&args.sem, ""ML_create_exclusive_thread"");
      }

-    sem_destroy(&args.sem);
+    MR_sem_destroy(&args.sem);

      if (args.thread_started) {
          *thread_id = args.thread_id;
diff --git a/runtime/mercury_context.c b/runtime/mercury_context.c
index 4e83d87..ee1f6db 100644
--- a/runtime/mercury_context.c
+++ b/runtime/mercury_context.c
@@ -755,7 +755,7 @@ MR_finalize_context_stuff(void)
      pthread_mutex_destroy(&MR_runqueue_lock);
      pthread_mutex_destroy(&free_context_list_lock);
    #ifdef MR_LL_PARALLEL_CONJ
-    sem_destroy(&shutdown_ws_semaphore);
+    MR_sem_destroy(&shutdown_ws_semaphore);
    #endif
  #endif

@@ -2272,9 +2272,10 @@ retry_sleep:
          }
          ts.tv_sec = tv.tv_sec;
          ts.tv_nsec = tv.tv_usec * 1000;
-        result = sem_timedwait(&(esync->d.es_sleep_semaphore), &ts);
+        result = MR_SEM_TIMED_WAIT(&(esync->d.es_sleep_semaphore), &ts,
+            "MR_do_sleep");
  #else
-        result = sem_wait(&(esync->d.es_sleep_semaphore));
+        result = MR_SEM_WAIT(&(esync->d.es_sleep_semaphore), "MR_do_sleep");
  #endif

          if (result != 0) {
diff --git a/runtime/mercury_thread.c b/runtime/mercury_thread.c
index 356a50b..16129ea 100644
--- a/runtime/mercury_thread.c
+++ b/runtime/mercury_thread.c
@@ -440,6 +440,29 @@ MR_sem_wait(MercurySem *sem, const char *from)
  }

  int
+MR_sem_timed_wait(MercurySem *sem, const struct timespec *abstime,
+        const char *from)
+{
+    int err;
+
+    fprintf(stderr,
+        "%" MR_INTEGER_LENGTH_MODIFIER "d timed wait on sem: %p (%s)\n",
+            MR_SELF_THREAD_ID, sem, from);
+    fflush(stderr);
+#if defined(MR_MAC_OSX)
+    MR_fatal_error("sem_timedwait not supported on Mac OS X");
+#else
+    err = sem_timedwait(sem, abstime);
+#endif
+    fprintf(stderr,
+        "%" MR_INTEGER_LENGTH_MODIFIER "d timed wait returned %d\n",
+        MR_SELF_THREAD_ID, err);
+    fflush(stderr);
+
+    return err;
+}
+
+int
  MR_sem_post(MercurySem *sem, const char *from)
  {
      int err;
@@ -457,6 +480,15 @@ MR_sem_post(MercurySem *sem, const char *from)
      return err;
  }

+void
+MR_sem_destroy(MercurySem *sem)
+{
+   if (sem_destroy(sem) == -1) {
+        MR_perror("cannot destroy semaphore");
+        exit(EXIT_FAILURE);
+    }
+}
+
  // For pthreads-win32 MR_null_thread() is defined as follows. For other
  // pthread implementations it is defined as a macro in mercury_thread.h.

diff --git a/runtime/mercury_thread.h b/runtime/mercury_thread.h
index 7dae740..5d98fbf 100644
--- a/runtime/mercury_thread.h
+++ b/runtime/mercury_thread.h
@@ -38,7 +38,10 @@

    extern void   MR_sem_init(MercurySem *sem, unsigned int);
    extern int    MR_sem_wait(MercurySem *sem, const char *from);
+  extern int    MR_sem_timed_wait(MercurySem *sem, const struct timespec *abstime,
+                    const char *from);
    extern int    MR_sem_post(MercurySem *sem, const char *from);
+  extern void   MR_sem_destroy(MercurySem *sem);

    #if defined(MR_PTHREADS_WIN32)
      extern MercuryThread MR_null_thread(void);
@@ -118,6 +121,22 @@
              sem_wait((sem))                                             \
          )

+#if defined(MR_MAC_OSX)
+    #define MR_SEM_TIMED_WAIT(sem, abstime, from)                       \
+        ( MR_debug_threads ?                                            \
+            MR_sem_timed_wait((sem), (abstime), (from))                 \
+        :                                                               \
+            MR_fatal_error("sem_timedwait not supported on Mac OS X")   \
+        )
+#else
+    #define MR_SEM_TIMED_WAIT(sem, abstime, from)                       \
+        ( MR_debug_threads ?                                            \
+            MR_sem_timed_wait((sem), (abstime), (from))                 \
+        :                                                               \
+            sem_timedwait((sem))                                        \
+        )
+#endif
+
      #define MR_SEM_POST(sem, from)                                      \
          ( MR_debug_threads ?                                            \
              MR_sem_post((sem), (from))                                  \


More information about the reviews mailing list