futriix/src/fastlock.cpp

42 lines
814 B
C++
Raw Normal View History

2019-02-10 20:24:11 -05:00
#include "fastlock.h"
2019-02-10 22:00:19 -05:00
#include <unistd.h>
2019-02-10 20:24:11 -05:00
thread_local int tls_pid = -1;
2019-02-10 20:24:11 -05:00
extern "C" void fastlock_init(struct fastlock *lock)
{
2019-02-10 22:00:19 -05:00
lock->m_lock = 0;
lock->m_depth = 0;
2019-02-10 20:24:11 -05:00
}
extern "C" void fastlock_lock(struct fastlock *lock)
{
2019-02-10 22:00:19 -05:00
while (!__sync_bool_compare_and_swap(&lock->m_lock, 0, 1))
{
if (lock->m_pidOwner == getpid())
{
++lock->m_depth;
return;
}
2019-02-10 22:00:19 -05:00
}
lock->m_depth = 1;
lock->m_pidOwner = getpid();
2019-02-10 20:24:11 -05:00
}
extern "C" void fastlock_unlock(struct fastlock *lock)
{
--lock->m_depth;
if (lock->m_depth == 0)
{
lock->m_pidOwner = -1;
asm volatile ("": : :"memory");
__sync_bool_compare_and_swap(&lock->m_lock, 1, 0);
}
2019-02-10 20:24:11 -05:00
}
2019-02-10 22:00:19 -05:00
2019-02-10 20:24:11 -05:00
extern "C" void fastlock_free(struct fastlock *lock)
{
// NOP
(void)lock;
}