about summary refs log tree commit diff stats
path: root/tools/iso/kernel.soso/spinlock.c
blob: 8efbdb205df469772d0c763c6506c86890bf22e7 (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
#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;
}