summaryrefslogtreecommitdiff
path: root/lib/thread/futex+freebsd.myr
blob: c011bf9428ae99cd052ee2a9230772b57b880503 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std
use sys

use "atomic"
use "common"

pkg thread =
	type ftxtag = uint32
	impl atomic ftxtag

	const ftxwait : (uaddr : ftxtag#, val : ftxtag, tmout : std.time -> sys.errno)
	const ftxwake : (uaddr : ftxtag# -> void)
	const ftxwakeall : (uaddr : ftxtag# -> void)
;;

const ftxwait = {uaddr, val, tmout
	var ut : sys.umtx_time, utp, utsize, rc

	if tmout < 0
		utp = Zptr
		utsize = Zptr
	else
		var t = (tmout : int64)
		std.assert(sys.clock_gettime(`sys.Clockmonotonic, &ut.timeout) == 0,
			"error: clock_gettime returned -1\n")
		ut.timeout.nsec += (t % 1_000_000) * 1000
		ut.timeout.sec += (ut.timeout.nsec / 1_000_000_000) + (t / 1_000_000)
		ut.timeout.nsec %= 1_000_000_000
		ut.flags = (sys.Umtxabstime : uint32)
		ut.clockid = (sys.clockid(`sys.Clockmonotonic) : uint32)
		utp = (&ut : void#)
		utsize = (sizeof(sys.umtx_time) : void#)
	;;

	while (rc = (sys.umtx_op((uaddr : void#),
			sys.Umtxwaituintpriv,
			(val : uint64),
			utsize,
			utp) : sys.errno)) == sys.Eintr
	;;

	match rc
	| 0: -> 0
	| sys.Eagain: -> sys.Eagain
	| sys.Etimedout: -> sys.Etimedout
	| err:
		std.fput(2, "error: umtx_op returned {}\n", err)
		std.suicide()
	;;
}

const ftxwake = {uaddr
	sys.umtx_op((uaddr : void#), sys.Umtxwakepriv, 1, Zptr, Zptr)
}

const ftxwakeall = {uaddr
	sys.umtx_op((uaddr : void#), sys.Umtxwakepriv, 0x7fffffff, Zptr, Zptr)
}

impl atomic ftxtag =
	xget = {p; -> (xget32((p : uint32#)) : ftxtag)}
	xset = {p, v; xset32((p : uint32#), (v : uint32))}
	xadd = {p, v; -> (xadd32((p : uint32#), (v : uint32)) : ftxtag)}
	xcas = {p, old, new; -> (xcas32((p : uint32#), (old : uint32), (new : uint32)) : ftxtag)}
	xchg = {p, v; -> (xchg32((p : uint32#), (v : uint32)) : ftxtag)}
;;