about summary refs log blame commit diff stats
path: root/kernel.soso/spinlock.c
blob: 8efbdb205df469772d0c763c6506c86890bf22e7 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12

                     
                                                                                     








                                                         
                                        


                  

                                                 



               
                                          

                  
#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;
}