2019-02-10 20:24:11 -05:00
|
|
|
#pragma once
|
2019-02-22 01:23:31 -05:00
|
|
|
#include <inttypes.h>
|
2019-11-17 16:06:49 -05:00
|
|
|
#include <stddef.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-10-22 21:34:51 -04:00
|
|
|
void fastlock_init(struct fastlock *lock, const char *name);
|
2019-02-10 20:24:11 -05:00
|
|
|
void fastlock_lock(struct fastlock *lock);
|
2019-06-17 21:53:04 -04:00
|
|
|
int fastlock_trylock(struct fastlock *lock, int fWeak);
|
2019-02-10 20:24:11 -05:00
|
|
|
void fastlock_unlock(struct fastlock *lock);
|
|
|
|
void fastlock_free(struct fastlock *lock);
|
2019-07-12 20:46:50 -04:00
|
|
|
int fastlock_unlock_recursive(struct fastlock *lock);
|
|
|
|
void fastlock_lock_recursive(struct fastlock *lock, int nesting);
|
2019-02-10 20:24:11 -05:00
|
|
|
|
2019-03-19 22:04:33 -04:00
|
|
|
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
|
|
|
|
|
2019-06-15 23:53:34 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
2019-02-22 01:23:31 -05:00
|
|
|
struct ticket
|
|
|
|
{
|
2019-06-15 23:53:34 -04:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint16_t m_active;
|
|
|
|
uint16_t m_avail;
|
|
|
|
};
|
|
|
|
unsigned u;
|
|
|
|
};
|
2019-02-22 01:23:31 -05:00
|
|
|
};
|
2019-06-15 23:53:34 -04:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2019-02-10 22:00:19 -05:00
|
|
|
struct fastlock
|
|
|
|
{
|
2019-02-16 14:25:14 -05:00
|
|
|
volatile int m_pidOwner;
|
|
|
|
volatile int m_depth;
|
2019-11-17 16:06:49 -05:00
|
|
|
char szName[56];
|
|
|
|
/* Volatile data on seperate cache line */
|
|
|
|
volatile struct ticket m_ticket;
|
2019-06-15 23:53:34 -04:00
|
|
|
unsigned futex;
|
2019-11-17 16:06:49 -05:00
|
|
|
char padding[56]; // ensure ticket and futex are on their own independent cache line
|
2019-02-10 22:00:19 -05:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2019-10-22 21:34:51 -04:00
|
|
|
fastlock(const char *name)
|
2019-02-11 03:36:18 -05:00
|
|
|
{
|
2019-10-22 21:34:51 -04:00
|
|
|
fastlock_init(this, name);
|
2019-02-11 03:36:18 -05:00
|
|
|
}
|
|
|
|
|
2019-10-22 21:34:51 -04:00
|
|
|
inline void lock()
|
2019-02-10 22:00:19 -05:00
|
|
|
{
|
|
|
|
fastlock_lock(this);
|
|
|
|
}
|
|
|
|
|
2019-10-22 21:34:51 -04:00
|
|
|
inline bool try_lock(bool fWeak = false)
|
2019-02-22 01:23:31 -05:00
|
|
|
{
|
2019-06-17 21:53:04 -04:00
|
|
|
return !!fastlock_trylock(this, fWeak);
|
2019-02-22 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
2019-10-22 21:34:51 -04:00
|
|
|
inline void unlock()
|
2019-02-10 22:00:19 -05:00
|
|
|
{
|
|
|
|
fastlock_unlock(this);
|
|
|
|
}
|
2019-02-18 22:25:35 -05:00
|
|
|
|
2019-07-12 20:46:50 -04:00
|
|
|
int unlock_recursive()
|
|
|
|
{
|
|
|
|
return fastlock_unlock_recursive(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock_recursive(int nesting)
|
|
|
|
{
|
|
|
|
fastlock_lock_recursive(this, nesting);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
2019-11-17 16:06:49 -05:00
|
|
|
|
2020-04-16 20:12:05 -04:00
|
|
|
static_assert(offsetof(struct fastlock, m_ticket) == 64, "ensure padding is correct");
|