84 lines
1.8 KiB
C
84 lines
1.8 KiB
C
////////////////////////////////
|
|
//~ Mutex types
|
|
|
|
#define DefaultMutexSpin 4000
|
|
|
|
AlignedStruct(Mutex, 64)
|
|
{
|
|
/* Bit 31 = Exclusive lock is held
|
|
* Bit 30 = Exclusive lock is pending
|
|
* Bit 0-30 = Shared locks count
|
|
*/
|
|
Atomic32 v;
|
|
|
|
#if RtcIsEnabled
|
|
Atomic32 exclusive_fiber_id;
|
|
u8 _pad[56];
|
|
#else
|
|
u8 _pad[60];
|
|
#endif
|
|
};
|
|
StaticAssert(sizeof(Mutex) == 64); /* Padding validation */
|
|
StaticAssert(alignof(Mutex) == 64); /* Prevent false sharing */
|
|
|
|
Struct(Lock)
|
|
{
|
|
Mutex *mutex;
|
|
b32 exclusive;
|
|
};
|
|
|
|
////////////////////////////////
|
|
//~ Condition variable types
|
|
|
|
AlignedStruct(Cv, 64)
|
|
{
|
|
Atomic64 wake_gen;
|
|
u8 _pad[56];
|
|
};
|
|
StaticAssert(sizeof(Cv) == 64); /* Padding validation */
|
|
StaticAssert(alignof(Cv) == 64); /* Prevent false sharing */
|
|
|
|
////////////////////////////////
|
|
//~ Counter types
|
|
|
|
AlignedStruct(Counter, 64)
|
|
{
|
|
Atomic64 v;
|
|
u8 _pad[56];
|
|
};
|
|
StaticAssert(sizeof(Counter) == 64); /* Padding validation */
|
|
StaticAssert(alignof(Counter) == 64); /* Prevent false sharing */
|
|
|
|
////////////////////////////////
|
|
//~ Mutex operations
|
|
|
|
Lock LockSpinE(Mutex *m, i32 spin);
|
|
Lock LockSpinS(Mutex *m, i32 spin);
|
|
|
|
Lock LockE(Mutex *m);
|
|
Lock LockS(Mutex *m);
|
|
|
|
void Unlock(Lock *lock);
|
|
|
|
//- Lock assertion
|
|
#if RtcIsEnabled
|
|
# define AssertLockedE(l, m) Assert((l)->mutex == (m) && (l)->exclusive == 1)
|
|
# define AssertLockedES(l, m) Assert((l)->mutex == (m))
|
|
#else
|
|
# define AssertLockedE(l, m) LAX l
|
|
# define AssertLockedES(l, m) LAX l
|
|
#endif
|
|
|
|
////////////////////////////////
|
|
//~ Condition variable operations
|
|
|
|
void YieldOnCv(Cv *cv, Lock *lock);
|
|
void YieldOnCvTime(Cv *cv, Lock *l, i64 timeout_ns);
|
|
void SignalCv(Cv *cv, i32 count);
|
|
|
|
////////////////////////////////
|
|
//~ Counter operations
|
|
|
|
void AddCounter(Counter *counter, i64 x);
|
|
void YieldOnCounter(Counter *counter);
|