[m-rev.] for review: function version of semaphore.new

Peter Wang novalazy at gmail.com
Wed Oct 17 10:27:41 AEST 2007


The intention of this is so that you can easily make a global mutex, e.g.

    :- mutable(mysem, semaphore, semaphore.new(1), ground,
	[untrailed, attach_to_io_state]).

(That is, until STM support is stable :-)


Estimated hours taken: 0.2
Branches: main

library/thread.semaphore.m:
	Add a function version of `semaphore.new' which additionally
	allows the user to give the initial counter value.

Index: library/thread.semaphore.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/thread.semaphore.m,v
retrieving revision 1.12
diff -u -r1.12 thread.semaphore.m
--- library/thread.semaphore.m	12 Sep 2007 06:21:13 -0000	1.12
+++ library/thread.semaphore.m	16 Oct 2007 06:33:37 -0000
@@ -28,11 +28,16 @@
 
 :- type semaphore.
 
-    % new(Sem, !IO) creates a new semaphore `Sem' with it's counter
+    % new(Sem, !IO) creates a new semaphore `Sem' with its counter
     % initialized to 0.
     %
 :- pred semaphore.new(semaphore::out, io::di, io::uo) is det.
 
+    % new(Count, Sem) creates a new semaphore `Sem' with its counter
+    % initialized to Count.
+    %
+:- func semaphore.new(int::in) = (semaphore::uo) is det.
+
     % wait(Sem, !IO) blocks until the counter associated with `Sem'
     % becomes greater than 0, whereupon it wakes, decrements the
     % counter and returns.
@@ -99,8 +104,11 @@
 
 %-----------------------------------------------------------------------------%
 
+new(Semaphore, !IO) :-
+    Semaphore = new(0).
+
 :- pragma foreign_proc("C",
-    new(Semaphore::out, IO0::di, IO::uo),
+    new(Count::in) = (Semaphore::uo),
     [promise_pure, will_not_call_mercury, thread_safe],
 "
     MR_Word         sem_mem;
@@ -109,7 +117,7 @@
     MR_incr_hp(sem_mem,
         MR_round_up(sizeof(ML_Semaphore), sizeof(MR_Word)));
     sem = (ML_Semaphore *) sem_mem;
-    sem->count = 0;
+    sem->count = Count;
 #ifndef MR_HIGHLEVEL_CODE
     sem->suspended_head = NULL;
     sem->suspended_tail = NULL;
@@ -129,15 +137,14 @@
     MR_GC_register_finalizer(sem, ML_finalize_semaphore, NULL);
 
     Semaphore = sem;
-    IO = IO0;
 ").
 
 :- pragma foreign_proc("C#",
-    new(Semaphore::out, _IO0::di, _IO::uo),
+    new(Count::in) = (Semaphore::uo),
     [promise_pure, will_not_call_mercury, thread_safe],
 "
     Semaphore = new ML_Semaphore();
-    Semaphore.count = 0;
+    Semaphore.count = Count;
 ").
 
 :- pragma foreign_code("C", "

--------------------------------------------------------------------------
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