Espresso 0.0.2c
This commit is contained in:
40
lib/sync.c
Normal file
40
lib/sync.c
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
|
||||
#include <sync.h>
|
||||
|
||||
|
||||
void spinlock_init(spinlock_t* lock)
|
||||
{
|
||||
lock->locked = 0;
|
||||
}
|
||||
|
||||
static inline int atomic_xchg(volatile int* addr, int newval)
|
||||
{
|
||||
int result;
|
||||
|
||||
asm volatile (
|
||||
"xchg %0, %1"
|
||||
: "=r"(result), "+m"(*addr)
|
||||
: "0"(newval)
|
||||
: "memory"
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void spin_lock(spinlock_t* lock)
|
||||
{
|
||||
while (atomic_xchg(&lock->locked, 1))
|
||||
{
|
||||
while (lock->locked)
|
||||
{
|
||||
asm volatile("pause");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void spin_unlock(spinlock_t* lock)
|
||||
{
|
||||
__atomic_clear(&lock->locked, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user