Peek whether spinlock is locked or not

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
dlarudgus20
Member
Member
Posts: 36
Joined: Sat Oct 26, 2013 4:14 am

Peek whether spinlock is locked or not

Post 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..
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: peek whether spinlock is locked or not

Post 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).
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: peek whether spinlock is locked or not

Post by sortie »

The check whether a spinlock is taken is useless. However, the operation 'Do I own this spinlock?' is very useful for assertions.
Post Reply