428,816 Members | 2,151 Online
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

# Explanation of spin_lock function from net/socket.c

 100+ P: 1,059 Expand|Select|Wrap|Line Numbers spin_lock(&net_family_lock);     if (net_families[ops->family])         err = -EEXIST;     else {         net_families[ops->family] = ops;         err = 0;     }     spin_unlock(&net_family_lock); Please look at the above piece of code (got from net/socket.c). Its about spin_lock function. I tried to find the explanation. But its not that easy. and here what I got a possible function definition of spin_lock and spin_unlock function got from wikipedia. Expand|Select|Wrap|Line Numbers # Intel syntax   lock:                        # The lock variable. 1 = locked, 0 = unlocked.      dd      0   spin_lock:      mov     eax, 1          # Set the EAX register to 1.   loop:      xchg    eax, [lock]     # Atomically swap the EAX register with                              #  the lock variable.                              # This will always store 1 to the lock, leaving                              #  previous value in the EAX register.        test    eax, eax        # Test EAX with itself. Among other things, this will                              #  set the processor's Zero Flag if EAX is 0.                              # If EAX is 0, then the lock was unlocked and                              #  we just locked it.                              # Otherwise, EAX is 1 and we didn't acquire the lock.        jnz     loop            # Jump back to the XCHG instruction if the Zero Flag is                              #  not set, the lock was locked, and we need to spin.        ret                     # The lock has been acquired, return to the calling                              #  function.   spin_unlock:      mov     eax, 0          # Set the EAX register to 0.        xchg    eax, [lock]     # Atomically swap the EAX register with                              #  the lock variable.        ret                     # The lock has been released.   I have tried to understand the situation. How it is working. in the first code when we call spin_lock it enters in a loop that which will continue until ZF is set to high. In that case how program will continue to next statements? If it dont get to next statements it wont get spin_unlock. Or what I am missing here? Sep 17 '10 #1
5 Replies