[m-rev.] diff: implement semaphores on the il backend

Peter Ross pro at missioncriticalit.com
Tue Mar 4 01:34:51 AEDT 2003


On Sat, Mar 01, 2003 at 03:53:16AM +1100, Fergus Henderson wrote:
> > +:- pragma foreign_proc("C#",
> > +		try_wait0(Semaphore::in, Res::out, _IO0::di, _IO::uo),
> > +		[will_not_call_mercury, thread_safe, promise_pure], "{
> > +	System.Threading.Monitor.Enter(Semaphore);
> > +
> > +	if (Semaphore.count > 0) {
> > +		Semaphore.count--;
> > +		System.Threading.Monitor.Exit(Semaphore);
> > +		Res = 0;
> > +	} else {
> > +		System.Threading.Monitor.Exit(Semaphore);
> > +		Res = 1;
> > +	}
> 
> That is broken.  semaphore__try_wait must never block.
> You need to use TryEnter() instead of Enter().
> 

===================================================================


Estimated hours taken: 0.25
Branches: main, release

extras/concurrency/semaphore.m:
	Change try_wait0 to use TryEnter when attempting to obtain the
	mutex on the il backend.

Index: semaphore.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/concurrency/semaphore.m,v
retrieving revision 1.12
diff -u -r1.12 semaphore.m
--- semaphore.m	28 Feb 2003 14:39:18 -0000	1.12
+++ semaphore.m	3 Mar 2003 14:32:19 -0000
@@ -285,14 +285,16 @@
 :- pragma foreign_proc("C#",
 		try_wait0(Semaphore::in, Res::out, _IO0::di, _IO::uo),
 		[will_not_call_mercury, thread_safe, promise_pure], "{
-	System.Threading.Monitor.Enter(Semaphore);
-
-	if (Semaphore.count > 0) {
-		Semaphore.count--;
-		System.Threading.Monitor.Exit(Semaphore);
-		Res = 0;
+	if (System.Threading.Monitor.TryEnter(Semaphore)) {
+		if (Semaphore.count > 0) {
+			Semaphore.count--;
+			System.Threading.Monitor.Exit(Semaphore);
+			Res = 0;
+		} else {
+			System.Threading.Monitor.Exit(Semaphore);
+			Res = 1;
+		}
 	} else {
-		System.Threading.Monitor.Exit(Semaphore);
 		Res = 1;
 	}
 }").

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list