aboutsummaryrefslogtreecommitdiff
path: root/core/sync/sync2/primitives_darwin.odin
blob: 66995bd0199201ce5db30a28d0f00a5ff6e72705 (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
//+build darwin
//+private
package sync2

import "core:c"
import "core:time"
import "core:intrinsics"

foreign import pthread "System.framework"

_current_thread_id :: proc "contextless" () -> int {
	tid: u64
	// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
	// For older versions there is `syscall(SYS_thread_selfid)`, but not really
	// the same thing apparently.
	foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
	pthread_threadid_np(nil, &tid)
	return int(tid)
}



_Mutex :: struct {
	mutex: Atomic_Mutex,
}

_mutex_lock :: proc(m: ^Mutex) {
	atomic_mutex_lock(&m.impl.mutex)
}

_mutex_unlock :: proc(m: ^Mutex) {
	atomic_mutex_unlock(&m.impl.mutex)
}

_mutex_try_lock :: proc(m: ^Mutex) -> bool {
	return atomic_mutex_try_lock(&m.impl.mutex)
}

_Cond :: struct {
	cond: Atomic_Cond,
}

_cond_wait :: proc(c: ^Cond, m: ^Mutex) {
	atomic_cond_wait(&c.impl.cond, &m.impl.mutex)
}

_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, duration: time.Duration) -> bool {
	return atomic_cond_wait_with_timeout(&c.impl.cond, &m.impl.mutex, duration)
}

_cond_signal :: proc(c: ^Cond) {
	atomic_cond_signal(&c.impl.cond)
}

_cond_broadcast :: proc(c: ^Cond) {
	atomic_cond_broadcast(&c.impl.cond)
}