shithub: mc

Download patch

ref: 28407c92feaf5fc5f5925e0e05bec4508b172ee2
parent: 9892228031c0ffcfa95a1443fc565b031525bb3a
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Aug 30 18:16:52 EDT 2018

Simplify futures.

--- a/lib/thread/future.myr
+++ b/lib/thread/future.myr
@@ -1,14 +1,10 @@
 use std
 
-use "mutex"
-use "condvar"
-use "atomic"
+use "sem"
 
 pkg thread =
 	type future(@a) = struct
-		mtx	: mutex
-		cv	: cond
-		set	: bool
+		sem	: sem
 		val	: @a
 	;;
 
@@ -15,41 +11,23 @@
 	generic mkfut	: (-> future(@a)#)
 	generic futput	: (fut : future(@a)#, val : @a -> void)
 	generic futget	: (fut : future(@a)# -> @a)
-	generic futpeek	: (fut : future(@a)# -> @a)
 ;;
 
 generic mkfut = {
-	var fut
-
-	fut = std.alloc()
-	fut.mtx = mkmtx()
-	fut.cv = mkcond(&fut.mtx)
-	fut.set = false
-	-> fut
+	-> std.mk([.sem = mksem(0)])
 }
 
 generic futput = {fut, val
 	fut.val = val
-	xset(&fut.set, true)
-	condsignal(&fut.cv)
+	sempost(&fut.sem)
 }
 
 generic futget = {fut
 	var val
 	
-	val = futpeek(fut)
+	semwait(&fut.sem)
+	val = fut.val
 	std.free(fut)
 	-> val
-}
-
-generic futpeek = {fut
-	if !xget(&fut.set)
-		mtxlock(&fut.mtx)
-		if !xget(&fut.set)
-			condwait(&fut.cv)
-		;;
-		mtxunlock(&fut.mtx)
-	;;
-	-> fut.val
 }