Page 1 of 1

Peek whether spinlock is locked or not

Posted: Sun May 25, 2014 3:31 am
by dlarudgus20
Here's wiki about this http://wiki.osdev.org/Spinlock:

Code: Select all

    cmp dword [lock],0
    je .free
    jne .locked
However, to communicate with C, isn't it better?

Code: Select all

ckSpinlockPeek: ; bool ckSpinlockPeek(Spinlock *pSpinlock);
    mov eax, [esp + 4]
    mov eax, dword [eax]
    ret
My question is whether it is okay or not.. I'm afraid whether there's any point I missed..

Re: peek whether spinlock is locked or not

Posted: Sun May 25, 2014 3:46 am
by Nable
dlarudgus20 wrote:ckSpinlockPeek
Code is ok (you just test the value at some moment and return whether it's zero or not.
The thing that is IMHO not OK is that function to peek for current value of syncronization variable is almost useless: you cannot do something atomically with this test. I.e. when function returns, return value may be already out-dated. Atomic test-and-set and test-and-reset functions are good but (strictly speaking) function that just tests looks senseless (even for operations like TryLock).

Re: peek whether spinlock is locked or not

Posted: Sun May 25, 2014 6:17 am
by sortie
The check whether a spinlock is taken is useless. However, the operation 'Do I own this spinlock?' is very useful for assertions.