blob: 1ef2be13c48d3c79e29c96cf9ad4f5d7615bd559 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include "spinlock.h"
static inline int32 exchangeAtomic(volatile int32* oldValueAddress, int32 newValue)
{
//no need to use lock instruction on xchg
asm volatile ("xchgl %0, %1"
: "=r"(newValue)
: "m"(*oldValueAddress), "0"(newValue)
: "memory");
return newValue;
}
void Spinlock_Init(Spinlock* spinlock)
{
*spinlock = 0;
}
void Spinlock_Lock(Spinlock* spinlock)
{
while (exchangeAtomic((int32*)spinlock, 1))
{
halt();
}
}
void Spinlock_Unlock(Spinlock* spinlock)
{
*spinlock = 0;
}
|