Fastlock fixes

This commit is contained in:
John Sully 2019-02-10 22:00:19 -05:00
parent c4dbc557eb
commit 988ea40101
2 changed files with 28 additions and 9 deletions

View File

@ -1,19 +1,23 @@
#include "fastlock.h" #include "fastlock.h"
#include <unistd.h>
extern "C" void fastlock_init(struct fastlock *lock) extern "C" void fastlock_init(struct fastlock *lock)
{ {
lock->lock = 0; lock->m_lock = 0;
} }
extern "C" void fastlock_lock(struct fastlock *lock) extern "C" void fastlock_lock(struct fastlock *lock)
{ {
while (!__sync_bool_compare_and_swap(&lock->lock, 0, 1)); while (!__sync_bool_compare_and_swap(&lock->m_lock, 0, 1))
{
}
} }
extern "C" void fastlock_unlock(struct fastlock *lock) extern "C" void fastlock_unlock(struct fastlock *lock)
{ {
lock->lock = 0; __sync_bool_compare_and_swap(&lock->m_lock, 1, 0);
} }
extern "C" void fastlock_free(struct fastlock *lock) extern "C" void fastlock_free(struct fastlock *lock)
{ {
// NOP // NOP

View File

@ -4,16 +4,31 @@
extern "C" { extern "C" {
#endif #endif
struct fastlock /* Begin C API */
{ struct fastlock;
int lock;
};
void fastlock_init(struct fastlock *lock); void fastlock_init(struct fastlock *lock);
void fastlock_lock(struct fastlock *lock); void fastlock_lock(struct fastlock *lock);
void fastlock_unlock(struct fastlock *lock); void fastlock_unlock(struct fastlock *lock);
void fastlock_free(struct fastlock *lock); void fastlock_free(struct fastlock *lock);
/* End C API */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
struct fastlock
{
int m_lock;
#ifdef __cplusplus
void lock()
{
fastlock_lock(this);
}
void unlock()
{
fastlock_unlock(this);
}
#endif
};