Add initial implementation of threads/mutexes/semaphores/cvs/spinlocks
Added initial implementation (or wrapper) of the threading library. It's rather basic, but should work for most of the tasks. Unfortunately, spinlock implementation relies on GCC/Clang compiler built-ins (or in-worst-case-scenario on Win32 - InterlockExchange). In the future, I should revisit this code and fix/reimplement some stuff (or add support for Windows XP).
This commit is contained in:
50
src/Platform/Spinlock.c
Normal file
50
src/Platform/Spinlock.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <BH/Thread.h>
|
||||
|
||||
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
/* Using built-ins */
|
||||
#elif defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
|
||||
void BH_SpinlockLock(int *lock)
|
||||
{
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
while (__sync_lock_test_and_set(lock, 1));
|
||||
#elif defined(_WIN32)
|
||||
while (InterlockedExchange(lock, 1));
|
||||
#else
|
||||
#error "Spinlocks are not supported"
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int BH_SpinlockLockTry(int *lock)
|
||||
{
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
if (__sync_lock_test_and_set(lock, 1))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
#elif defined(_WIN32)
|
||||
if (InterlockedExchange(lock, 1))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
#else
|
||||
#error "Spinlocks are not supported"
|
||||
return BH_NOIMPL;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void BH_SpinlockUnlock(int *lock)
|
||||
{
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
__sync_lock_release(lock);
|
||||
#elif defined(_WIN32)
|
||||
InterlockedExchange(lock, 0);
|
||||
#else
|
||||
#error "Spinlocks are not supported"
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user