aboutsummaryrefslogtreecommitdiff
path: root/core/sync/futex_freebsd.odin
blob: e3f95b1467b1109a7080802afb0c393f3c82ce52 (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
#+private
#+build freebsd
package sync

import "core:c"
import "core:sys/freebsd"
import "core:time"

_futex_wait :: proc "contextless" (f: ^Futex, expected: u32) -> bool {
	timeout := freebsd.timespec {14400, 0} // 4 hours
	timeout_size := cast(rawptr)cast(uintptr)size_of(timeout)

	for {
		errno := freebsd._umtx_op(f, .WAIT_UINT, cast(c.ulong)expected, timeout_size, &timeout)

		if errno == nil {
			return true
		}

		if errno == .ETIMEDOUT {
			continue
		}

		panic_contextless("_futex_wait failure")
	}

	unreachable()
}

_futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, duration: time.Duration) -> bool {
	if duration <= 0 {
		return false
	}

	timeout := freebsd.timespec {cast(freebsd.time_t)duration / 1e9, cast(c.long)duration % 1e9}
	timeout_size := cast(rawptr)cast(uintptr)size_of(timeout)

	errno := freebsd._umtx_op(f, .WAIT_UINT, cast(c.ulong)expected, timeout_size, &timeout)
	if errno == nil {
		return true
	}

	if errno == .ETIMEDOUT {
		return false
	}

	panic_contextless("_futex_wait_with_timeout failure")
}

_futex_signal :: proc "contextless" (f: ^Futex) {
	errno := freebsd._umtx_op(f, .WAKE, 1, nil, nil)

	if errno != nil {
		panic_contextless("_futex_signal failure")
	}
}

_futex_broadcast :: proc "contextless" (f: ^Futex)  {
	errno := freebsd._umtx_op(f, .WAKE, cast(c.ulong)max(i32), nil, nil)

	if errno != nil {
		panic_contextless("_futex_broadcast failure")
	}
}