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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
//- futex
#if PLATFORM_LINUX
internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns)
{
struct timespec timeout = {};
struct timespec *timeout_ptr = NULL;
if(timeout_ns >= 0) {
timeout.tv_sec = timeout_ns / 1000000000ll;
timeout.tv_nsec = timeout_ns % 1000000000ll;
timeout_ptr = &timeout;
}
// NOTE: EAGAIN (value already changed), EINTR and ETIMEDOUT are all fine, the caller re-checks
syscall(SYS_futex, address, FUTEX_WAIT_PRIVATE, expected, timeout_ptr, NULL, 0);
}
internal void futex_wake_all(u32 *address)
{
syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, INT32_MAX, NULL, NULL, 0);
}
#elif PLATFORM_APPLE
internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns)
{
// NOTE: timeout in microseconds, 0 = forever. spurious returns are fine, the caller re-checks
u32 timeout_us = 0;
if(timeout_ns >= 0) timeout_us = (u32)Max(Min((timeout_ns + 999) / 1000, (s64)UINT32_MAX), (s64)1);
__ulock_wait(DARWIN_UL_COMPARE_AND_WAIT | DARWIN_ULF_NO_ERRNO, address, expected, timeout_us);
}
internal void futex_wake_all(u32 *address)
{
__ulock_wake(DARWIN_UL_COMPARE_AND_WAIT | DARWIN_ULF_WAKE_ALL | DARWIN_ULF_NO_ERRNO, address, 0);
}
#elif PLATFORM_WINDOWS
internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns)
{
// NOTE: like the futex, spurious and early returns are fine, the caller re-checks
DWORD timeout_ms = (timeout_ns < 0) ? INFINITE : (DWORD)((timeout_ns + 999999ll) / 1000000ll);
WaitOnAddress((volatile VOID *)address, &expected, sizeof(expected), timeout_ms);
}
internal void futex_wake_all(u32 *address)
{
WakeByAddressAll((PVOID)address);
}
#endif
internal u32 signal_snapshot(Signal *signal)
{
return AtomicLoad(&signal->sequence);
}
internal void signal_notify(Signal *signal)
{
AtomicAdd(&signal->sequence, 1);
futex_wake_all(&signal->sequence);
}
internal void signal_wait(Signal *signal, u32 snapshot, s64 timeout_ns)
{
futex_wait(&signal->sequence, snapshot, timeout_ns);
}
//- spin lock
internal void spin_lock(SpinLock *lock)
{
for(;;) {
if(!AtomicExchange(&lock->locked, 1)) {
return;
}
// NOTE: spin on a plain load so we don't hammer the line with exclusive requests
while(AtomicLoadRelaxed(&lock->locked)) {
CpuRelax();
}
}
}
internal void spin_unlock(SpinLock *lock)
{
AtomicStore(&lock->locked, 0u);
}
//- main thread wakeup
global_variable int global_wake_fd = -1;
#if PLATFORM_LINUX
internal void wake_main_init(void)
{
global_wake_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
AssertLog(global_wake_fd != -1, "failed to create eventfd");
}
internal void wake_main(void)
{
u64 one = 1;
ssize_t written = write(global_wake_fd, &one, sizeof(one));
Unused(written);
}
internal b32 wake_main_drain(void)
{
u64 value = 0;
ssize_t got = read(global_wake_fd, &value, sizeof(value));
return got == (ssize_t)sizeof(value) && value > 0;
}
#elif PLATFORM_APPLE
// NOTE: no eventfd: a non blocking pipe, the read end is what the pump watches
global_variable int global_wake_write_fd = -1;
internal void wake_main_init(void)
{
int fds[2];
AssertLog(pipe(fds) == 0, "failed to create the wake pipe");
for(u32 index = 0; index < 2; ++index) {
fcntl(fds[index], F_SETFL, fcntl(fds[index], F_GETFL) | O_NONBLOCK);
fcntl(fds[index], F_SETFD, FD_CLOEXEC);
}
global_wake_fd = fds[0];
global_wake_write_fd = fds[1];
}
internal void wake_main(void)
{
u8 one = 1;
ssize_t written = write(global_wake_write_fd, &one, 1);
Unused(written);
}
internal b32 wake_main_drain(void)
{
u8 buffer[64];
b32 woke = false;
while(read(global_wake_fd, buffer, sizeof(buffer)) > 0) woke = true;
return woke;
}
#elif PLATFORM_WINDOWS
internal HANDLE wake_main_handle(void)
{
return (global_wake_fd == -1) ? NULL : (HANDLE)(smm)global_wake_fd;
}
internal void wake_main_init(void)
{
// NOTE: manual reset: the pump's wait must not consume it, wake_main_drain resets it
HANDLE event = CreateEventW(NULL, TRUE, FALSE, NULL);
AssertLog(event != NULL, "failed to create the wake event");
global_wake_fd = (int)(smm)event;
}
internal void wake_main(void)
{
HANDLE event = wake_main_handle();
if(event) SetEvent(event);
}
internal b32 wake_main_drain(void)
{
HANDLE event = wake_main_handle();
if(!event || WaitForSingleObject(event, 0) != WAIT_OBJECT_0) return false;
ResetEvent(event);
return true;
}
#endif
internal int wake_main_fd(void)
{
return global_wake_fd;
}
//- time
#if PLATFORM_LINUX || PLATFORM_APPLE
internal u64 time_now_ns(void)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (u64)now.tv_sec * 1000000000ull + (u64)now.tv_nsec;
}
internal void sleep_ns(u64 ns)
{
struct timespec duration = {(time_t)(ns / 1000000000ull), (long)(ns % 1000000000ull)};
nanosleep(&duration, NULL);
}
#elif PLATFORM_WINDOWS
internal u64 time_now_ns(void)
{
local_persist u64 frequency = 0;
if(!frequency) {
LARGE_INTEGER f;
QueryPerformanceFrequency(&f);
frequency = (u64)f.QuadPart;
}
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
u64 ticks = (u64)counter.QuadPart;
// NOTE: split to avoid overflowing ticks * 1e9
return (ticks / frequency) * 1000000000ull + ((ticks % frequency) * 1000000000ull) / frequency;
}
internal void sleep_ns(u64 ns)
{
// NOTE: high resolution waitable timer (windows 10 1803+), plain Sleep as the fallback
HANDLE timer = CreateWaitableTimerExW(NULL, NULL, 0x00000002 /* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION */, TIMER_ALL_ACCESS);
if(timer) {
LARGE_INTEGER due;
due.QuadPart = -(LONGLONG)(ns / 100ull);
if(SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) {
WaitForSingleObject(timer, INFINITE);
}
CloseHandle(timer);
return;
}
Sleep((DWORD)((ns + 999999ull) / 1000000ull));
}
#endif
//- aligned arena pushes
// NOTE: arena_alloc only guarantees 16 byte alignment, hot simd arrays want a full cache line
internal void *push_cache_aligned_raw(Arena *arena, u64 size)
{
u8 *raw = (u8 *)arena_alloc(arena, size + CACHE_LINE_SIZE, 0);
return (void *)Align(raw, CACHE_LINE_SIZE);
}
internal void *push_cache_aligned(Arena *arena, u64 size)
{
u8 *raw = (u8 *)arena_alloc(arena, size + CACHE_LINE_SIZE, 1);
return (void *)Align(raw, CACHE_LINE_SIZE);
}
|