futriix/src/fastlock.h

71 lines
1.2 KiB
C
Raw Normal View History

2019-02-10 20:24:11 -05:00
#pragma once
2019-02-22 01:23:31 -05:00
#include <inttypes.h>
2019-02-10 20:24:11 -05:00
#ifdef __cplusplus
extern "C" {
#endif
2019-02-10 22:00:19 -05:00
/* Begin C API */
struct fastlock;
2019-02-10 20:24:11 -05:00
void fastlock_init(struct fastlock *lock);
void fastlock_lock(struct fastlock *lock);
2019-02-22 01:23:31 -05:00
int fastlock_trylock(struct fastlock *lock);
2019-02-10 20:24:11 -05:00
void fastlock_unlock(struct fastlock *lock);
void fastlock_free(struct fastlock *lock);
uint64_t fastlock_getlongwaitcount(); // this is a global value
2019-02-10 22:00:19 -05:00
/* End C API */
2019-02-10 20:24:11 -05:00
#ifdef __cplusplus
}
2019-02-10 22:00:19 -05:00
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
2019-02-22 01:23:31 -05:00
struct ticket
{
union
{
struct
{
uint16_t m_active;
uint16_t m_avail;
};
unsigned u;
};
2019-02-22 01:23:31 -05:00
};
#pragma GCC diagnostic pop
2019-02-10 22:00:19 -05:00
struct fastlock
{
2019-02-22 01:23:31 -05:00
volatile struct ticket m_ticket;
2019-02-16 14:25:14 -05:00
volatile int m_pidOwner;
volatile int m_depth;
unsigned futex;
2019-02-10 22:00:19 -05:00
#ifdef __cplusplus
fastlock()
{
fastlock_init(this);
}
2019-02-10 22:00:19 -05:00
void lock()
{
fastlock_lock(this);
}
2019-02-22 01:23:31 -05:00
bool try_lock()
{
return !!fastlock_trylock(this);
}
2019-02-10 22:00:19 -05:00
void unlock()
{
fastlock_unlock(this);
}
2019-02-18 22:25:35 -05:00
bool fOwnLock(); // true if this thread owns the lock, NOTE: not 100% reliable, use for debugging only
2019-02-10 22:00:19 -05:00
#endif
};