summaryrefslogtreecommitdiff
path: root/lib/thread/mutex+futex.myr
blob: bb9012d87d239af2fa96e862109d7e006be235c7 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std

use "atomic"
use "futex"
use "tls"
use "types"

pkg thread =
	type mutex = struct
		_state	: ftxtag
		_owner	: tid
	;;	

	const mkmtx	: (-> mutex)
	const mtxlock	: (mtx : mutex# -> void)
	const mtxtrylock	: (mtx : mutex# -> bool)
	const mtxunlock	: (mtx : mutex# -> void)

	pkglocal const mtxcontended	: (mtx : mutex# -> void)
;;

const Unlocked = 0
const Locked = 1
const Contended = 2

var nspin = 10	/* FIXME: pick a sane number, based on CPU count */

const mkmtx = {
	-> [._state = Unlocked, ._owner = -1]
}

const mtxlock = {mtx
	var c

	if mtx._owner == tid()
		std.fput(std.Err,
			"error: thread {} attempted to relock a mutex it already holds\n",
			tid())
		std.suicide()
	;;

	/*
	Uncontended case: we get an unlocked mutex, and we lock it.
	*/
	c = Locked
	for var i = 0; i < nspin; i++
		c = xcas(&mtx._state, Unlocked, Locked)
		if c == Unlocked
			mtx._owner = tid()
			-> void
		;;
	;;

	/*
	Contended case: we set the lock state to Contended. This indicates that
	the lock is locked, and we potentially have threads waiting on it,
	which means that we will need to wake them up.
	*/
	if c == Locked
		c = xchg(&mtx._state, Contended)
	;;

	while c != Unlocked
		ftxwait(&mtx._state, Contended, -1)
		c = xchg(&mtx._state, Contended)
	;;
	mtx._owner = tid()
}

const mtxtrylock = {mtx
	if xcas(&mtx._state, Unlocked, Locked) == Unlocked
		mtx._owner = tid()
		-> true
	;;
	-> false
}

const mtxunlock = {mtx
	/*
	Nonatomically loading mtx._owner may produce false negatives on
	weakly-ordered architectures but having to atomically store and load
	mtx._owner doesn't seem worth it.
	*/
	if mtx._owner != tid()
		std.fput(std.Err,
			"error: thread {} attempted to unlock a mutex last held by {}\n",
			tid(), mtx._owner)
		std.suicide()
	;;
	mtx._owner = -1

	/*
	Either the lock is contended or it's uncontended. Any other
	state is a bug.

	Uncontended case: If the mutex state is not contended, and we still
	are uncontended by the xchg() call, then it's safe to simply return;
	nobody was waiting for us.
	*/
	if xchg(&mtx._state, Unlocked) == Contended
		ftxwake(&mtx._state)
	;;
}

const mtxcontended = {mtx
	if mtx._owner == tid()
		std.fput(std.Err,
			"error: thread {} attempted to relock a mutex it already holds\n",
			tid())
		std.suicide()
	;;

	while xchg(&mtx._state, Contended) != Unlocked
		ftxwait(&mtx._state, Contended, -1)
	;;
	mtx._owner = tid()
}