Adding a Nitty version of a condition variable
authorBlackMinou <romain.chanoir@viacesi.fr>
Tue, 5 Jul 2016 15:23:54 +0000 (11:23 -0400)
committerBlackMinou <romain.chanoir@viacesi.fr>
Tue, 21 Feb 2017 01:30:24 +0000 (20:30 -0500)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

lib/pthreads/pthreads.nit

index 8fed1bb..13b9e95 100644 (file)
@@ -128,7 +128,7 @@ private extern class NativePthread in "C" `{ pthread_t * `}
 
        fun equal(other: NativePthread): Bool `{ return pthread_equal(*self, *other); `}
 
-       fun kill(signal: Int) `{ pthread_kill(*self, (int)signal); `}
+       fun kill(signal: Int): Int `{ return pthread_kill(*self, (int)signal); `}
 end
 
 private extern class NativePthreadAttr in "C" `{ pthread_attr_t * `}
@@ -238,7 +238,7 @@ private extern class NativePthreadCond in "C" `{ pthread_cond_t * `}
 
        fun destroy `{ pthread_cond_destroy(self); `}
 
-       fun signal `{ pthread_cond_signal(self); `}
+       fun signal: Int `{ return pthread_cond_signal(self); `}
 
        fun broadcast `{ pthread_cond_broadcast(self);  `}
 
@@ -377,6 +377,25 @@ class Mutex
        end
 end
 
+# Condition variable
+class PthreadCond
+       super FinalizableOnce
+
+       private var native = new NativePthreadCond
+
+       # Destroy `self`
+       redef fun finalize_once do native.destroy
+
+       # Signal at least one thread waiting to wake up
+       fun signal: Int do return native.signal
+
+       # Signal all the waiting threads to wake up
+       fun broadcast do native.broadcast
+
+       # Make the current thread waiting for a signal ( `mutex` should be locked)
+       fun wait(mutex: Mutex) do native.wait(mutex.native.as(not null))
+end
+
 # Barrier synchronization tool
 #
 # Ensures that `count` threads call and block on `wait` before releasing them.