aboutsummaryrefslogtreecommitdiff
path: root/core/sync/futex_haiku.odin
blob: 52321644a3a32e6fac00412ab75d0be7a48091d3 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#+private
package sync

import "core:sys/haiku"
import "core:sys/posix"
import "core:time"

@(private="file")
Wait_Node :: struct {
	thread:     posix.pthread_t,
	futex:      ^Futex,
	prev, next: ^Wait_Node,
}
@(private="file")
atomic_flag :: distinct bool
@(private="file")
Wait_Queue :: struct {
	lock: atomic_flag,
	list: Wait_Node,
}
@(private="file")
waitq_lock :: proc "contextless" (waitq: ^Wait_Queue) {
	for cast(bool)atomic_exchange_explicit(&waitq.lock, atomic_flag(true), .Acquire) {
		cpu_relax() // spin...
	}
}
@(private="file")
waitq_unlock :: proc "contextless" (waitq: ^Wait_Queue) {
	atomic_store_explicit(&waitq.lock, atomic_flag(false), .Release)
}

// FIXME: This approach may scale badly in the future,
// possible solution - hash map (leads to deadlocks now).
@(private="file")
g_waitq: Wait_Queue

@(init, private="file")
g_waitq_init :: proc() {
	g_waitq = {
		list = {
			prev = &g_waitq.list,
			next = &g_waitq.list,
		},
	}
}

@(private="file")
get_waitq :: #force_inline proc "contextless" (f: ^Futex) -> ^Wait_Queue {
	_ = f
	return &g_waitq
}

_futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> (ok: bool) {
	waitq := get_waitq(f)
	waitq_lock(waitq)
	defer waitq_unlock(waitq)

	head   := &waitq.list
	waiter := Wait_Node{
		thread = posix.pthread_self(),
		futex  = f,
		prev   = head,
		next   = head.next,
	}

	waiter.prev.next = &waiter
	waiter.next.prev = &waiter

	old_mask, mask: posix.sigset_t
	posix.sigemptyset(&mask)
	posix.sigaddset(&mask, .SIGCONT)
	posix.pthread_sigmask(.BLOCK, &mask, &old_mask)

	if u32(atomic_load_explicit(f, .Acquire)) == expect {
		waitq_unlock(waitq)
		defer waitq_lock(waitq)
		
		sig: posix.Signal
		errno := posix.sigwait(&mask, &sig) 
		ok = errno == nil
	}

	waiter.prev.next = waiter.next
	waiter.next.prev = waiter.prev

	_ = posix.pthread_sigmask(.SETMASK, &old_mask, nil)

 	// FIXME: Add error handling!
	return
}

_futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expect: u32, duration: time.Duration) -> (ok: bool) {
	if duration <= 0 {
		return false
	}
	waitq := get_waitq(f)
	waitq_lock(waitq)
	defer waitq_unlock(waitq)

	head   := &waitq.list
	waiter := Wait_Node{
		thread = posix.pthread_self(),
		futex  = f,
		prev   = head,
		next   = head.next,
	}

	waiter.prev.next = &waiter
	waiter.next.prev = &waiter

	old_mask, mask: posix.sigset_t
	posix.sigemptyset(&mask)
	posix.sigaddset(&mask, .SIGCONT)
	posix.pthread_sigmask(.BLOCK, &mask, &old_mask)

	if u32(atomic_load_explicit(f, .Acquire)) == expect {
		waitq_unlock(waitq)
		defer waitq_lock(waitq)
		
		info: posix.siginfo_t
		ts := posix.timespec{
			tv_sec  = posix.time_t(i64(duration / 1e9)),
			tv_nsec = i64(duration % 1e9),
		}
		haiku.sigtimedwait(&mask, &info, &ts)
		errno := posix.errno() 
		ok = errno == .EAGAIN || errno == nil
	}

	waiter.prev.next = waiter.next
	waiter.next.prev = waiter.prev

	posix.pthread_sigmask(.SETMASK, &old_mask, nil)

	// FIXME: Add error handling!
	return
}

_futex_signal :: proc "contextless" (f: ^Futex) {
	waitq := get_waitq(f)
	waitq_lock(waitq)
	defer waitq_unlock(waitq)

	head := &waitq.list
	for waiter := head.next; waiter != head; waiter = waiter.next {
		if waiter.futex == f {
			posix.pthread_kill(waiter.thread, .SIGCONT)
			break
		}
	}
}

_futex_broadcast :: proc "contextless" (f: ^Futex) {
	waitq := get_waitq(f)
	waitq_lock(waitq)
	defer waitq_unlock(waitq)

	head := &waitq.list
	for waiter := head.next; waiter != head; waiter = waiter.next {
		if waiter.futex == f {
			posix.pthread_kill(waiter.thread, .SIGCONT)
		}
	}
}