aboutsummaryrefslogtreecommitdiff
path: root/core/sys/unix/pthread_darwin.odin
blob: 358b5d2fda284c7c853897c1b4385c5c67bbb3b0 (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
//+build darwin
package unix

import "core:c"

// NOTE(tetra): No 32-bit Macs.
// Source: _pthread_types.h on my Mac.
PTHREAD_SIZE           :: 8176
PTHREAD_ATTR_SIZE      :: 56
PTHREAD_MUTEXATTR_SIZE :: 8
PTHREAD_MUTEX_SIZE     :: 56
PTHREAD_CONDATTR_SIZE  :: 8
PTHREAD_COND_SIZE      :: 40
PTHREAD_ONCE_SIZE      :: 8
PTHREAD_RWLOCK_SIZE    :: 192
PTHREAD_RWLOCKATTR_SIZE :: 16

pthread_t :: distinct u64

pthread_attr_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_ATTR_SIZE] c.char,
}

pthread_cond_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_COND_SIZE] c.char,
}

pthread_condattr_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_CONDATTR_SIZE] c.char,
}

pthread_mutex_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_MUTEX_SIZE] c.char,
}

pthread_mutexattr_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_MUTEXATTR_SIZE] c.char,
}

pthread_once_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_ONCE_SIZE] c.char,
}

pthread_rwlock_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_RWLOCK_SIZE] c.char,
}

pthread_rwlockattr_t :: struct #align(16) {
	sig: c.long,
	_: [PTHREAD_RWLOCKATTR_SIZE] c.char,
}

SCHED_OTHER :: 1 // Avoid if you are writing portable software.
SCHED_FIFO  :: 4
SCHED_RR :: 2 // Round robin.

SCHED_PARAM_SIZE :: 4

sched_param :: struct {
	sched_priority: c.int,
	_: [SCHED_PARAM_SIZE] c.char,
}

// Source: https://github.com/apple/darwin-libpthread/blob/03c4628c8940cca6fd6a82957f683af804f62e7f/pthread/pthread.h#L138
PTHREAD_CREATE_JOINABLE :: 1
PTHREAD_CREATE_DETACHED :: 2
PTHREAD_INHERIT_SCHED :: 1
PTHREAD_EXPLICIT_SCHED :: 2
PTHREAD_PROCESS_SHARED :: 1
PTHREAD_PROCESS_PRIVATE :: 2


PTHREAD_MUTEX_NORMAL :: 0
PTHREAD_MUTEX_RECURSIVE :: 1
PTHREAD_MUTEX_ERRORCHECK :: 2

PTHREAD_CANCEL_ENABLE       :: 0
PTHREAD_CANCEL_DISABLE      :: 1
PTHREAD_CANCEL_DEFERRED     :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 1

foreign import pthread "system:System.framework"

@(default_calling_convention="c")
foreign pthread {
	pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
	pthread_setcanceltype  :: proc (type:  c.int, old_type:  ^c.int) -> c.int ---
	pthread_cancel         :: proc (thread: pthread_t) -> c.int ---
}