aboutsummaryrefslogtreecommitdiffstats
path: root/source/core/core_sync.h
blob: a1907f68985e0e756b1848b4c40cf9340ebb4562 (plain) (blame)
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
#ifndef CORE_SYNC_H
#define CORE_SYNC_H

// NOTE: app level threading primitives that aren't in base yet. everything here is lock free
// or futex based, no pthread_cond on any hot path. candidates for promoting into base_thread.

//- cache lines

// NOTE: apple m-series (this machine) has 128 byte lines, x64 and most cortex cores have 64.
// we pad to 128 on arm64 since being too big only costs a little memory, too small costs
// false sharing between the decoder and the realtime audio thread.
#if ARCH_ARM
#define CACHE_LINE_SIZE 128
#else
#define CACHE_LINE_SIZE 64
#endif

#define CacheAligned alignas(CACHE_LINE_SIZE)

//- atomics
// NOTE: thin wrappers over the gcc/clang builtins so the memory order is always spelled out

#define AtomicLoad(pointer)                  __atomic_load_n((pointer), __ATOMIC_ACQUIRE)
#define AtomicLoadRelaxed(pointer)           __atomic_load_n((pointer), __ATOMIC_RELAXED)
#define AtomicStore(pointer, value)          __atomic_store_n((pointer), (value), __ATOMIC_RELEASE)
#define AtomicStoreRelaxed(pointer, value)   __atomic_store_n((pointer), (value), __ATOMIC_RELAXED)
#define AtomicAdd(pointer, value)            __atomic_add_fetch((pointer), (value), __ATOMIC_ACQ_REL)
#define AtomicAddRelaxed(pointer, value)     __atomic_add_fetch((pointer), (value), __ATOMIC_RELAXED)
#define AtomicExchange(pointer, value)       __atomic_exchange_n((pointer), (value), __ATOMIC_ACQ_REL)
#define AtomicCompareExchange(pointer, expected, desired) \
	__atomic_compare_exchange_n((pointer), (expected), (desired), false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)

#define AtomicFence() __atomic_thread_fence(__ATOMIC_SEQ_CST)

// NOTE: f32 values shared between threads are stored as their bit pattern in a u32
internal inline u32 f32_bits(f32 value)
{
	u32 result;
	MemCpy(&result, &value, sizeof(result));
	return result;
}

internal inline f32 f32_from_bits(u32 bits)
{
	f32 result;
	MemCpy(&result, &bits, sizeof(result));
	return result;
}

#if ARCH_ARM
#define CpuRelax() __asm__ volatile("yield" ::: "memory")
#elif ARCH_x86_64 || ARCH_X86
#define CpuRelax() __builtin_ia32_pause()
#else
#define CpuRelax() ((void)0)
#endif

//- aligned arena pushes
internal void *push_cache_aligned(Arena *arena, u64 size);
internal void *push_cache_aligned_raw(Arena *arena, u64 size); // not zeroed, pages stay untouched
#define PushArrayAligned(arena, type, count) (type *)push_cache_aligned((arena), sizeof(type) * (count))
#define PushStructAligned(arena, type)       (type *)push_cache_aligned((arena), sizeof(type))

//- futex
// NOTE: a futex is just a u32 in memory the kernel can park threads on. we use it as an
// "event counter": waiters read the counter, check their condition, then sleep until the
// counter moves. signalers bump the counter and wake. no lost wakeups because the kernel
// re-checks the value atomically before sleeping.

typedef struct Signal Signal;
struct Signal {
	CacheAligned u32 sequence;
};

internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns);
internal void futex_wake_all(u32 *address);

internal u32  signal_snapshot(Signal *signal);
internal void signal_notify(Signal *signal);
internal void signal_wait(Signal *signal, u32 snapshot, s64 timeout_ns);

//- spin lock
// NOTE: only for tiny critical sections between non realtime threads (job queue push/pop)
typedef struct SpinLock SpinLock;
struct SpinLock {
	u32 locked;
};

internal void spin_lock(SpinLock *lock);
internal void spin_unlock(SpinLock *lock);

//- main thread wakeup
// NOTE: an eventfd the main thread polls next to the wayland fd. any thread can poke it to get
// a redraw (download progress, track changed, ...) without the main loop spinning.
internal void wake_main_init(void);
internal void wake_main(void);
internal b32  wake_main_drain(void); // true if someone poked us
internal int  wake_main_fd(void);

//- time
internal u64 time_now_ns(void);
internal void sleep_ns(u64 ns);

#endif // CORE_SYNC_H