[m-rev.] diff: fix semaphore finalization

Julien Fischer juliensf at csse.unimelb.edu.au
Wed Aug 22 18:18:01 AEST 2007


Estimated hours taken: 0.5
Branches: main

Minor fix for the finalization of semaphores.

library/thread.semaphore.m:
 	Fix up a spot where we were assuming that MR_CONSERVATIVE_GC
 	is equivalent to MR_BOEHM_GC by using the new MR_GC_register_finalizer
 	macro instead of calling GC_REGISTER_FINALIZER directly.

 	Remove the #ifdef protecting ML_finalize_semaphore.  It is safe to
 	compile it in non .gc grades and it (or something like it) will
 	eventually need to be used in most other grades anyway.

 	Add a `can_pass_as_mercury_type' assertion to the C foreign type
 	definition for semaphore/0.

library/.cvsignore:
 	Ignore erlang_conf.hrl.

library/stm_builtin.m:
 	Fix some compilation errors in this module.  The pthread header is
 	only #included when MR_THREAD_SAFE is defined so protect calls
 	to pthread fuctions with #ifdef MR_THREAD_SAFE.

 	s/MR_TRACE_STM/ML_TRACE_STM/

Julien.

Index: library/.cvsignore
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/.cvsignore,v
retrieving revision 1.30
diff -u -r1.30 .cvsignore
--- library/.cvsignore	15 Jun 2007 12:41:47 -0000	1.30
+++ library/.cvsignore	22 Aug 2007 08:14:18 -0000
@@ -43,3 +43,4 @@
  *.jar
  LIB_FLAGS
  config.log
+erlang_conf.hrl
Index: library/stm_builtin.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/stm_builtin.m,v
retrieving revision 1.1
diff -u -r1.1 stm_builtin.m
--- library/stm_builtin.m	22 Aug 2007 06:40:58 -0000	1.1
+++ library/stm_builtin.m	22 Aug 2007 08:14:18 -0000
@@ -482,15 +482,14 @@

      % Blocks the thread from being rescheduled.
      %
-%:- impure pred stm_block_thread(stm::ui) is det.
  :- pragma foreign_proc("C",
      stm_block_thread(_STM::ui),
      [will_not_call_mercury, thread_safe],
  "
-#if defined(MR_HIGHLEVEL_CODE)
+#if defined(MR_HIGHLEVEL_CODE) && defined(MR_THREAD_SAFE)
      pthread_yield();
  #else
-    MR_TRACE_STM(""Yeilding to thread"");
+    ML_TRACE_STM(""Yielding to thread"");
  #endif
  ").

Index: library/thread.semaphore.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/thread.semaphore.m,v
retrieving revision 1.9
diff -u -r1.9 thread.semaphore.m
--- library/thread.semaphore.m	17 Jul 2007 05:36:08 -0000	1.9
+++ library/thread.semaphore.m	22 Aug 2007 08:14:18 -0000
@@ -86,15 +86,15 @@
  };
  ").

-:- pragma foreign_type("C",  semaphore, "ML_Semaphore *").
+:- pragma foreign_type("C",  semaphore, "ML_Semaphore *",
+    [can_pass_as_mercury_type]).
  :- pragma foreign_type("IL", semaphore,
      "class [mercury]mercury.thread.semaphore__csharp_code.mercury_code.ML_Semaphore").
  :- pragma foreign_type("Erlang", semaphore, "").

  :- pragma foreign_decl("C", "
-#ifdef MR_CONSERVATIVE_GC
-  void ML_finalize_semaphore(void *obj, void *cd);
-#endif
+extern void
+ML_finalize_semaphore(void *obj, void *cd);
  ").

  %-----------------------------------------------------------------------------%
@@ -103,7 +103,7 @@
      new(Semaphore::out, IO0::di, IO::uo),
      [promise_pure, will_not_call_mercury, thread_safe],
  "
-    MR_Word sem_mem;
+    MR_Word         sem_mem;
      ML_Semaphore    *sem;

      MR_incr_hp(sem_mem,
@@ -126,9 +126,7 @@
      ** The condvar and the mutex will need to be destroyed
      ** when the semaphore is garbage collected.
      */
-#ifdef MR_CONSERVATIVE_GC
-    GC_REGISTER_FINALIZER(sem, ML_finalize_semaphore, NULL, NULL, NULL);
-#endif
+    MR_GC_register_finalizer(sem, ML_finalize_semaphore, NULL);

      Semaphore = sem;
      IO = IO0;
@@ -143,24 +141,21 @@
  ").

  :- pragma foreign_code("C", "
-#ifdef MR_CONSERVATIVE_GC
-  void
-  ML_finalize_semaphore(void *obj, void *cd)
-  {
+
+void
+ML_finalize_semaphore(void *obj, void *cd)
+{
      ML_Semaphore    *sem;

      sem = (ML_Semaphore *) obj;

-  #ifdef MR_THREAD_SAFE
-    #ifdef MR_HIGHLEVEL_CODE
-    pthread_cond_destroy(&(sem->cond));
+    #if defined(MR_THREAD_SAFE)
+        #if defined(MR_HIGHLEVEL_CODE)
+            pthread_cond_destroy(&(sem->cond));
+        #endif
+        pthread_mutex_destroy(&(sem->lock));
      #endif
-    pthread_mutex_destroy(&(sem->lock));
-  #endif
-
-    return;
-  }
-#endif
+}
  ").

      % semaphore.signal causes the calling context to resume in semaphore.nop,

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list