[m-rev.] diff: add a wrapper for initializing unnamed semaphores in the runtime

Julien Fischer jfischer at opturion.com
Fri Sep 18 10:34:01 AEST 2015


Add a wrapper for initializing unnamed semaphores in the runtime.

runtime/mercury_thread.[ch]:
      Add a new wrapper function around sem_init: the wrapper ensures that the
      return value is always checked and isolates the direct calls to sem_init
      to within the the mercury_thread module.  The latter is important since
      in order to fix bug #357 we are going to have to replace the use of unnamed
      semaphores on OS X with something else (I'm currently looking at using
      the semaphore implementation from GCD's libdispatch as a possible fix.)

runtime/mercury_context.c:
      Use MR_sem_init where appropriate.

      s/sem_t/MercurySem/ in a few spots.

Julien.

diff --git a/runtime/mercury_context.c b/runtime/mercury_context.c
index 651f61f..63379f0 100644
--- a/runtime/mercury_context.c
+++ b/runtime/mercury_context.c
@@ -138,7 +138,7 @@ MR_milliseconds_from_now(struct timespec *timeout, unsigned int msecs);
  #define ENGINE_STATE_ALL            0xFFFF

  struct engine_sleep_sync_i {
-    sem_t                                       es_sleep_semaphore;
+    MercurySem                                  es_sleep_semaphore;
      MercuryLock                                 es_wake_lock;
      volatile MR_Unsigned                        es_state;
      volatile unsigned                           es_action;
@@ -261,7 +261,7 @@ static MR_Context       *free_small_context_list = NULL;
  #ifdef  MR_LL_PARALLEL_CONJ
  MR_Integer volatile         MR_num_idle_ws_engines = 0;
  static MR_Integer volatile  MR_num_outstanding_contexts = 0;
-static sem_t                shutdown_ws_semaphore;
+static MercurySem           shutdown_ws_semaphore;

  static MercuryLock      MR_par_cond_stats_lock;

@@ -365,10 +365,7 @@ MR_init_context_stuff(void)
      #ifdef MR_DEBUG_RUNTIME_GRANULARITY_CONTROL
      pthread_mutex_init(&MR_par_cond_stats_lock, MR_MUTEX_ATTR);
      #endif
-    if (sem_init(&shutdown_ws_semaphore, 0, 0) == -1) {
-        MR_perror("cannot initialize semaphore");
-        exit(EXIT_FAILURE);
-    }
+    MR_sem_init(&shutdown_ws_semaphore, 0);
    #endif
      pthread_mutex_init(&MR_STM_lock, MR_MUTEX_ATTR);

@@ -401,11 +398,7 @@ MR_init_context_stuff(void)
          MR_max_engines, MR_ALLOC_SITE_RUNTIME);
      for (i = 0; i < MR_max_engines; i++) {
          engine_sleep_sync *esync = get_engine_sleep_sync(i);
-
-        if (sem_init(&esync->d.es_sleep_semaphore, 0, 0) == -1 ) {
-            MR_perror("cannot initialize semaphore");
-            exit(EXIT_FAILURE);
-        };
+        MR_sem_init(&esync->d.es_sleep_sempahore, 0);
          pthread_mutex_init(&esync->d.es_wake_lock, MR_MUTEX_ATTR);
          /*
          ** All engines are initially working (because telling them to wake up
diff --git a/runtime/mercury_thread.c b/runtime/mercury_thread.c
index 7c8c876..3c654b2 100644
--- a/runtime/mercury_thread.c
+++ b/runtime/mercury_thread.c
@@ -428,6 +428,15 @@ MR_cond_timed_wait(MercuryCond *cond, MercuryLock *lock,
      return err;
  }

+void
+MR_sem_init(MercurySem *sem, unsigned int value)
+{
+    if (sem_init(sem, 0, value) == -1) {
+        MR_perror("cannot initialize semaphore");
+        exit(EXIT_FAILURE);
+    }
+}
+
  int
  MR_sem_wait(MercurySem *sem, const char *from)
  {
diff --git a/runtime/mercury_thread.h b/runtime/mercury_thread.h
index 4e50f43..9766d13 100644
--- a/runtime/mercury_thread.h
+++ b/runtime/mercury_thread.h
@@ -43,6 +43,8 @@ extern int
  MR_cond_timed_wait(MercuryCond *cond, MercuryLock *lock,
      const struct timespec *abstime, const char *from);

+extern void
+MR_sem_init(MercurySem *sem, unsigned int);
  extern int
  MR_sem_wait(MercurySem *sem, const char *from);
  extern int



More information about the reviews mailing list