[m-rev.] diff: concurrency/semaphores.m fixes

Peter Wang wangp at students.csse.unimelb.edu.au
Tue Jan 30 13:11:38 AEDT 2007


Branches: main

extras/concurrency/semaphore.m:
        Work around a problem in which pthread_cond_wait() could wake up
        prematurely, having received a signal.

        When signalling a condition variable, make sure the calling thread
        holds the mutex.  This is the recommended way to use
        pthread_cond_signal().

Index: extras/concurrency/semaphore.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/concurrency/semaphore.m,v
retrieving revision 1.17
diff -u -r1.17 semaphore.m
--- extras/concurrency/semaphore.m      26 Sep 2006 03:54:43 -0000      1.17
+++ extras/concurrency/semaphore.m      20 Jan 2007 11:21:19 -0000
@@ -208,8 +208,8 @@
     }
 #else
     sem->count++;
-    MR_UNLOCK(&(sem->lock), ""semaphore__signal"");
     MR_SIGNAL(&(sem->cond));
+    MR_UNLOCK(&(sem->lock), ""semaphore__signal"");
 #endif
     IO = IO0;
 ").
@@ -262,7 +262,13 @@
     }
 #else
     while (sem->count <= 0) {
-        MR_WAIT(&(sem->cond), &(sem->lock));
+        /*
+        ** Although it goes against the spec, pthread_cond_wait() can
+        ** return prematurely with the error code EINTR in glibc 2.3.2
+        ** if the thread is sent a signal.
+        */
+        while (MR_WAIT(&(sem->cond), &(sem->lock)) != 0) {
+        }
     }

     sem->count--;


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