Make the lock recursive, this is because processEventsWhileBlocked may cause us to lock multiple times

This commit is contained in:
John Sully 2019-02-15 14:11:05 -05:00
parent 48f6d0d800
commit f5caec488d
3 changed files with 2457 additions and 3 deletions

View File

@ -1,25 +1,41 @@
#include "fastlock.h"
#include <unistd.h>
thread_local int tls_pid = -1;
extern "C" void fastlock_init(struct fastlock *lock)
{
lock->m_lock = 0;
lock->m_depth = 0;
}
extern "C" void fastlock_lock(struct fastlock *lock)
{
while (!__sync_bool_compare_and_swap(&lock->m_lock, 0, 1))
{
if (lock->m_pidOwner == getpid())
{
++lock->m_depth;
return;
}
}
lock->m_depth = 1;
lock->m_pidOwner = getpid();
}
extern "C" void fastlock_unlock(struct fastlock *lock)
{
__sync_bool_compare_and_swap(&lock->m_lock, 1, 0);
--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);
}
}
extern "C" void fastlock_free(struct fastlock *lock)
{
// NOP
(void)lock;
}
}

View File

@ -18,7 +18,9 @@ void fastlock_free(struct fastlock *lock);
struct fastlock
{
int m_lock;
volatile int m_lock;
int m_pidOwner;
int m_depth;
#ifdef __cplusplus
fastlock()

2436
src/networking.cpp Normal file

File diff suppressed because it is too large Load Diff