summaryrefslogtreecommitdiff
path: root/src/common/spinlock.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/spinlock.h')
-rw-r--r--src/common/spinlock.h37
1 files changed, 18 insertions, 19 deletions
diff --git a/src/common/spinlock.h b/src/common/spinlock.h
index 3419bfdd5..bde36b8e5 100644
--- a/src/common/spinlock.h
+++ b/src/common/spinlock.h
@@ -1,11 +1,10 @@
-#pragma once
-#ifndef _rA_SPINLOCK_H_
-#define _rA_SPINLOCK_H_
+#ifndef COMMON_SPINLOCK_H
+#define COMMON_SPINLOCK_H
//
// CAS based Spinlock Implementation
//
-// CamelCase names are choosen to be consistent with microsofts winapi
+// CamelCase names are chosen to be consistent with Microsoft's WinAPI
// which implements CriticalSection by this naming...
//
// Author: Florian Wilkemeyer <fw@f-ws.de>
@@ -15,50 +14,50 @@
//
//
+#include "../common/atomic.h"
+#include "../common/cbasetypes.h"
+#include "../common/thread.h"
+
#ifdef WIN32
#include "../common/winapi.h"
#endif
-#include "../common/cbasetypes.h"
-#include "../common/atomic.h"
-#include "../common/thread.h"
-
#ifdef WIN32
typedef struct __declspec( align(64) ) SPIN_LOCK{
volatile LONG lock;
volatile LONG nest;
volatile LONG sync_lock;
-} SPIN_LOCK, *PSPIN_LOCK;
+} SPIN_LOCK;
#else
typedef struct SPIN_LOCK{
volatile int32 lock;
volatile int32 nest; // nesting level.
volatile int32 sync_lock;
-} __attribute__((aligned(64))) SPIN_LOCK, *PSPIN_LOCK;
+} __attribute__((aligned(64))) SPIN_LOCK;
#endif
-static forceinline void InitializeSpinLock(PSPIN_LOCK lck){
+static forceinline void InitializeSpinLock(SPIN_LOCK *lck){
lck->lock = 0;
lck->nest = 0;
lck->sync_lock = 0;
}
-static forceinline void FinalizeSpinLock(PSPIN_LOCK lck){
+static forceinline void FinalizeSpinLock(SPIN_LOCK *lck){
return;
}
-#define getsynclock(l) { while(1){ if(InterlockedCompareExchange(l, 1, 0) == 0) break; rathread_yield(); } }
-#define dropsynclock(l) { InterlockedExchange(l, 0); }
+#define getsynclock(l) do { if(InterlockedCompareExchange((l), 1, 0) == 0) break; rathread_yield(); } while(/*always*/1)
+#define dropsynclock(l) do { InterlockedExchange((l), 0); } while(0)
-static forceinline void EnterSpinLock(PSPIN_LOCK lck){
+static forceinline void EnterSpinLock(SPIN_LOCK *lck){
int tid = rathread_get_tid();
- // Get Sync Lock && Check if the requester thread already owns the lock.
+ // Get Sync Lock && Check if the requester thread already owns the lock.
// if it owns, increase nesting level
getsynclock(&lck->sync_lock);
if(InterlockedCompareExchange(&lck->lock, tid, tid) == tid){
@@ -70,7 +69,7 @@ static forceinline void EnterSpinLock(PSPIN_LOCK lck){
dropsynclock(&lck->sync_lock);
- // Spin until we've got it !
+ // Spin until we've got it !
while(1){
if(InterlockedCompareExchange(&lck->lock, tid, 0) == 0){
@@ -85,7 +84,7 @@ static forceinline void EnterSpinLock(PSPIN_LOCK lck){
}
-static forceinline void LeaveSpinLock(PSPIN_LOCK lck){
+static forceinline void LeaveSpinLock(SPIN_LOCK *lck){
int tid = rathread_get_tid();
getsynclock(&lck->sync_lock);
@@ -101,4 +100,4 @@ static forceinline void LeaveSpinLock(PSPIN_LOCK lck){
-#endif
+#endif /* COMMON_SPINLOCK_H */