summaryrefslogtreecommitdiff
path: root/lib/thread/test/future.myr
blob: 63969a443989ad364d2166317e4183826e864c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std
use sys
use thread

use "test/util.use"

var fut
var nready : int32
var ndone : int32

const main = {
	nready = 0
	ndone = 0
	fut = thread.mkfut()
	/* set after we have some waiters */
	mkherd(100, getfuture)
	while nready != 100
		/* spin */
	;;
	std.put("done waiting for ready\n")
	std.assert(ndone == 0, "thread proceeded too soon\n")
	thread.futset(&fut, 666)
	std.assert(thread.futset(&fut, 1) == false, "double set future\n")
	while ndone != 100
		/* spin */
	;;
	std.put("double set future ok")
	/* start up a few more to make sure we can still read */
	mkherd(50, getfuture)
	while ndone != 150
		/* spin */
	;;

	
	/* set ahead of time */
	ndone = 0
	fut = thread.mkfut()
	thread.futset(&fut, 666)
	std.assert(thread.futset(&fut, 666) == false, "double set future\n")
	mkherd(100, getfuture)
	while ndone != 100
		/* spin */
	;;
}

const getfuture = {
	thread.xadd(&nready, 1)
	std.assert(thread.futget(&fut) == 666, "wrong value gotten from future")
	thread.xadd(&ndone, 1)
}