atomics as macros

This commit is contained in:
jacob 2025-09-09 19:06:18 -05:00
parent 84f5c503df
commit 0518513478
6 changed files with 30 additions and 30 deletions

View File

@ -209,8 +209,6 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t);
//- Static
#define PERSIST static
#define Global static
/* TODO: Remove this */
#define internal static
//- Read-only
#if PlatformIsWindows
@ -526,46 +524,46 @@ Global const f64 *_f64_nan = (f64 *)&_f64_nan_u64;
#if LanguageIsC
//- Atomic types
Struct(Atomic8) { volatile i8 _v; };
Struct(Atomic8) { volatile i8 _v; };
Struct(Atomic16) { volatile i16 _v; };
Struct(Atomic32) { volatile i32 _v; };
Struct(Atomic64) { volatile i64 _v; };
//- Cache-line isolated aligned atomic types
AlignedStruct(Atomic8Padded, 64) { Atomic8 v; u8 _pad[63]; };
AlignedStruct(Atomic8Padded, 64) { Atomic8 v; u8 _pad[63]; };
AlignedStruct(Atomic16Padded, 64) { Atomic16 v; u8 _pad[62]; };
AlignedStruct(Atomic32Padded, 64) { Atomic32 v; u8 _pad[60]; };
AlignedStruct(Atomic64Padded, 64) { Atomic64 v; u8 _pad[56]; };
StaticAssert(sizeof(Atomic8Padded) == 64 && alignof(Atomic8Padded) == 64);
StaticAssert(sizeof(Atomic8Padded) == 64 && alignof(Atomic8Padded) == 64);
StaticAssert(sizeof(Atomic16Padded) == 64 && alignof(Atomic16Padded) == 64);
StaticAssert(sizeof(Atomic32Padded) == 64 && alignof(Atomic32Padded) == 64);
StaticAssert(sizeof(Atomic64Padded) == 64 && alignof(Atomic64Padded) == 64);
#if PlatformIsWindows && ArchIsX64
//- 8 bit atomic operations
ForceInline i8 Atomic8Fetch(Atomic8 *x) { return x->_v; }
ForceInline i8 Atomic8FetchSet(Atomic8 *x, i8 e) { return (i8)_InterlockedExchange8((volatile char *)&x->_v, e); }
ForceInline i8 Atomic8FetchTestSet(Atomic8 *x, i8 c, i8 e) { return (i8)_InterlockedCompareExchange8((volatile char *)&x->_v, e, c); }
ForceInline i8 Atomic8FetchXor(Atomic8 *x, i8 c) { return (i8)_InterlockedXor8((volatile char *)&x->_v, c); }
ForceInline i8 Atomic8FetchAdd(Atomic8 *x, i8 a) { return (i8)_InterlockedExchangeAdd8((volatile char *)&x->_v, a); }
#define Atomic8Fetch(x) (x)->_v
#define Atomic8FetchSet(x, e) (i8)_InterlockedExchange8((volatile char *)&(x)->_v, (e))
#define Atomic8FetchTestSet(x, c, e) (i8)_InterlockedCompareExchange8((volatile char *)&(x)->_v, (e), (c))
#define Atomic8FetchXor(x, c) (i8)_InterlockedXor8((volatile char *)&(x)->_v, (c))
#define Atomic8FetchAdd(x, a) (i8)_InterlockedExchangeAdd8((volatile char *)&(x)->_v, (a))
//- 16 bit atomic operations
ForceInline i16 Atomic16Fetch(Atomic16 *x) { return x->_v; }
ForceInline i16 Atomic16FetchSet(Atomic16 *x, i16 e) { return (i16)_InterlockedExchange16(&x->_v, e); }
ForceInline i16 Atomic16FetchTestSet(Atomic16 *x, i16 c, i16 e) { return (i16)_InterlockedCompareExchange16(&x->_v, e, c); }
ForceInline i16 Atomic16FetchTestXor(Atomic16 *x, i16 c) { return (i16)_InterlockedXor16(&x->_v, c); }
ForceInline i16 Atomic16FetchTestAdd(Atomic16 *x, i16 a) { return (i16)_InterlockedExchangeAdd16(&x->_v, a); }
#define Atomic16Fetch(x) (x)->_v
#define Atomic16FetchSet(x, e) (i16)_InterlockedExchange16(&(x)->_v, (e))
#define Atomic16FetchTestSet(x, c, e) (i16)_InterlockedCompareExchange16(&(x)->_v, (e), (c))
#define Atomic16FetchTestXor(x, c) (i16)_InterlockedXor16(&(x)->_v, (c))
#define Atomic16FetchTestAdd(x, a) (i16)_InterlockedExchangeAdd16(&(x)->_v, (a))
//- 32 bit atomic operations
ForceInline i32 Atomic32Fetch(Atomic32 *x) { return x->_v; }
ForceInline i32 Atomic32FetchSet(Atomic32 *x, i32 e) { return (i32)_InterlockedExchange((volatile long *)&x->_v, e); }
ForceInline i32 Atomic32FetchTestSet(Atomic32 *x, i32 c, i32 e) { return (i32)_InterlockedCompareExchange((volatile long *)&x->_v, e, c); }
ForceInline i32 Atomic32FetchXor(Atomic32 *x, i32 c) { return (i32)_InterlockedXor((volatile long *)&x->_v, c); }
ForceInline i32 Atomic32FetchAdd(Atomic32 *x, i32 a) { return (i32)_InterlockedExchangeAdd((volatile long *)&x->_v, a); }
#define Atomic32Fetch(x) (x)->_v
#define Atomic32FetchSet(x, e) (i32)_InterlockedExchange((volatile long *)&(x)->_v, (e))
#define Atomic32FetchTestSet(x, c, e) (i32)_InterlockedCompareExchange((volatile long *)&(x)->_v, (e), (c))
#define Atomic32FetchXor(x, c) (i32)_InterlockedXor((volatile long *)&(x)->_v, (c))
#define Atomic32FetchAdd(x, a) (i32)_InterlockedExchangeAdd((volatile long *)&(x)->_v, (a))
//- 64 bit atomic operations
ForceInline i64 Atomic64Fetch(Atomic64 *x) { return x->_v; }
ForceInline i64 Atomic64FetchSet(Atomic64 *x, i64 e) { return (i64)_InterlockedExchange64(&x->_v, e); }
ForceInline i64 Atomic64FetchTestSet(Atomic64 *x, i64 c, i64 e) { return (i64)_InterlockedCompareExchange64(&x->_v, e, c); }
ForceInline i64 Atomic64FetchXor(Atomic64 *x, i64 c) { return (i64)_InterlockedXor64(&x->_v, c); }
ForceInline i64 Atomic64FetchAdd(Atomic64 *x, i64 a) { return (i64)_InterlockedExchangeAdd64(&x->_v, a); }
#define Atomic64Fetch(x) (x)->_v
#define Atomic64FetchSet(x, e) (i64)_InterlockedExchange64(&(x)->_v, (e))
#define Atomic64FetchTestSet(x, c, e) (i64)_InterlockedCompareExchange64(&(x)->_v, (e), (c))
#define Atomic64FetchXor(x, c) (i64)_InterlockedXor64(&(x)->_v, (c))
#define Atomic64FetchAdd(x, a) (i64)_InterlockedExchangeAdd64(&(x)->_v, (a))
#else
# error Atomics not implemented
#endif
@ -705,7 +703,7 @@ Struct(ComputeShader) { Resource resource; };
#if LanguageIsC
# if PlatformIsWindows
# define FiberId() (*(i16 *)(void *)(volatile u64)__readgsqword(32))
# define FiberId() (*(volatile i16 *)__readgsqword(32))
# else
# error FiberId not implemented
# endif

View File

View File

View File

@ -837,11 +837,10 @@ ForceInline W32_Fiber *W32_FiberFromId(i16 id)
}
//- Fiber control flow
ForceNoInline void W32_FiberResume(W32_Fiber *fiber)
void W32_FiberResume(W32_Fiber *fiber)
{
MemoryBarrier();
SwitchToFiber(fiber->addr);
MemoryBarrier();
}
void W32_YieldFiber(W32_Fiber *fiber, W32_Fiber *parent_fiber)

View File

@ -288,7 +288,7 @@ void W32_WakeByTime(u64 time);
W32_Fiber *W32_AcquireFiber(W32_JobPool *pool);
void W32_ReleaseFiber(W32_JobPool *pool, W32_Fiber *fiber);
ForceInline W32_Fiber *W32_FiberFromId(i16 id);
ForceNoInline void W32_FiberResume(W32_Fiber *fiber);
void W32_FiberResume(W32_Fiber *fiber);
void W32_YieldFiber(W32_Fiber *fiber, W32_Fiber *parent_fiber);
////////////////////////////////

View File

@ -884,6 +884,9 @@ void StartupMeta(void)
PushStringToList(arena, &cp.flags_msvc, Lit("-INCREMENTAL:NO"));
PushStringToList(arena, &cp.flags_msvc, Lit("-nologo"));
/* Optimization */
PushStringToList(arena, &cp.compiler_only_flags_msvc, Lit("-Od"));
/* Debug info */
PushStringToList(arena, &cp.flags_msvc, Lit("-DEBUG:FULL"));
PushStringToList(arena, &cp.compiler_only_flags_msvc, Lit("-Z7"));