473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Explanation of spin_lock function from net/socket.c

1,059 1GB
Expand|Select|Wrap|Line Numbers
  1. spin_lock(&net_family_lock);
  2.     if (net_families[ops->family])
  3.         err = -EEXIST;
  4.     else {
  5.         net_families[ops->family] = ops;
  6.         err = 0;
  7.     }
  8.     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
  1. # Intel syntax
  2.  
  3. lock:                        # The lock variable. 1 = locked, 0 = unlocked.
  4.      dd      0
  5.  
  6. spin_lock:
  7.      mov     eax, 1          # Set the EAX register to 1.
  8.  
  9. loop:
  10.      xchg    eax, [lock]     # Atomically swap the EAX register with
  11.                              #  the lock variable.
  12.                              # This will always store 1 to the lock, leaving
  13.                              #  previous value in the EAX register.
  14.  
  15.      test    eax, eax        # Test EAX with itself. Among other things, this will
  16.                              #  set the processor's Zero Flag if EAX is 0.
  17.                              # If EAX is 0, then the lock was unlocked and
  18.                              #  we just locked it.
  19.                              # Otherwise, EAX is 1 and we didn't acquire the lock.
  20.  
  21.      jnz     loop            # Jump back to the XCHG instruction if the Zero Flag is
  22.                              #  not set, the lock was locked, and we need to spin.
  23.  
  24.      ret                     # The lock has been acquired, return to the calling
  25.                              #  function.
  26.  
  27. spin_unlock:
  28.      mov     eax, 0          # Set the EAX register to 0.
  29.  
  30.      xchg    eax, [lock]     # Atomically swap the EAX register with
  31.                              #  the lock variable.
  32.  
  33.      ret                     # The lock has been released.
  34.  
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 2199
donbock
2,426 Expert 2GB
The use of locks implies there are, or could be, multiple independent threads of execution. In this particular case, the lock is used to insure that only one thread at a time accesses the net_families array.
  1. The lock variable is initially 0.
  2. Thread 1 enters the code snippet.
  3. The lock variable is 0, so spin_lock returns immediately to Thread 1 after setting the lock variable to 1. Thread 1 enters the protected region.
  4. Thread 2 enters this same code snippet.
  5. The lock variable is 1, so spin_lock does not return to Thread 2.
  6. Thread 1 continues through the protected region. (Thread 2 is still stuck in spin_lock.)
  7. Thread 1 exits the protected region and calls spin_unlock. (Thread 2 is still stuck in spin_lock.)
  8. spin_unlock sets the lock variable to 0 from Thread 1.
  9. The next loop of spin_lock in Thread 2 finds the lock variable is 0, so it sets it back to 1 and returns.
  10. Thread 2 enters the protected region.
  11. Thread 2 exits the protected region and calls spin_unlock.
  12. spin_unlock sets the lock variable to 0 from Thread 2.
Notice that this spin lock only works if the operating system supports preemptive multitasking. Priority scheduling could result in a deadlock. That shouldn't be possible with round-robin scheduling. I encourage you to look up the italicized terms to find out why I made these statements.
Sep 17 '10 #2
johny10151981
1,059 1GB
I liked the explanation.
But it confused my knowledge.

It seems I need a little bit more knowledge on x86 Assembly.

Thanks for your thoughtful answer.
Sep 17 '10 #3
donbock
2,426 Expert 2GB
A C version of spin_lock might look something like this:
Expand|Select|Wrap|Line Numbers
  1. volatile int lock = 0;
  2. void spin_lock(void) {
  3.    while(lock != 0)
  4.        ;   /*sit and spin*/
  5.     lock = 1;
  6. }
Consider what might happen with this version of spin_lock in the previous Thread1/Thread2 example. Suppose execution is transferred from Thread1 to Thread2 after spin_lock detects that lock is zero, but before it has a chance to set the lock to 1. Thread2 runs its copy of spin_lock, who also finds the lock to be zero. Thread2 sets the lock and enters the protected region; but while there execution returns to Thread1, who finishes exiting spin_lock and enters the protected region. We end up with both threads in the protected region at the same time -- spin_lock did not do its job.

That's why it is so vital for lock functions to implement the lock-test and lock-set operations in an atomic, or non-interruptible / non-preemptible manner. Standard C does not provide an atomic test-and-set capability -- that's why spin_lock is written in assembly code. (It certainly isn't worth any effort to make this function run as fast as possible.)

Look at the original spin_lock listing. Line 10 atomically exchanges the contents of the EAX register with the lock variable, but line 7 had previously set EAX to "1". So as you enter line 15, the lock variable is "1" and EAX contains the old value of the lock variable. Lines 15 and 21 cause the subroutine to loop back if the old value of the lock variable is nonzero, but if the old value is zero the subroutine falls through to line 24 and returns.
Sep 17 '10 #4
johny10151981
1,059 1GB
In College I have learned 8086 Assembly language(turbo assembler). That is why a I was little confused.
Thanks for your reply.

This is a very good solution.

But the point that I am missing is this is a non interrupt able routine. Say a dumb programmer create a thread that that receive data from some source and which is busy. as example

Expand|Select|Wrap|Line Numbers
  1. spin_lock(&lock_var);
  2.  
  3. read_from_rs232(&variable);
  4.  
  5. spin_unlock(&lock_var);
  6.  
and say for some reason read_from_rs232 is stuck. in that case would processor get stuck forever(I don't have knowledge about it). It seems like a dead lock.
Sep 17 '10 #5
donbock
2,426 Expert 2GB
The possibility of deadlock is present any time you have locks. It is the job of the system architect to decide what resources need to be locked and conventions for how the locks are set and released. This job can be done well, or it can be done poorly.

A general principle is to keep the duration of a lock as short as possible.

Notice that spin locks do not allow a thread to do anything useful while it is waiting for a lock. An alternative is a lock function that returns a success or failure indication. The calling function can then choose to do something else until the lock function returns success.

Locks are really only needed to coordinate access to shared resources. Less sharing (more independence) between threads means fewer locks. Fewer locks means less brainpower need be expended on preventing deadlock.
Sep 17 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Angelo Secchi | last post by:
Hi, few days ago I had a problem of converting floating from an IBM/370 system to the actual standard. Thanks to a couple of you (Anton and Howard) I got the code to solve the problem that as...
70
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()...
4
by: Dave | last post by:
Hi, I'm new to asp.net and visual studio .net. I'm trying to fix a few problems on a friend's web site and don't really understand how .net works. The site is in .net. All asp.net pages...
4
by: darrel | last post by:
I'm trying to get a page up using some sample code that interfaces with a 3rd party application using .net. The sample code the company provides, when I run it, produces this error: ...
5
by: angelcd | last post by:
hi guys, good day to you all, can someone try to explain to me whats the meaning of this , ill try to interpret it by myself but i need others side of explanation so i can really prove it to myself,...
12
by: jacob navia | last post by:
Hi I am writing this tutorial stuff again in the holidays and I came across this problem: The "width" field in printf is a minimum width. Printf will not truncate a field. for instance:...
6
by: Milsnips | last post by:
hi there, i created a little test application to send out multiple emails to addresses, but using threads. So anyway, what i've got is 1 form with a START button, and a multiline textbox for...
7
by: yaragallamurali | last post by:
Hi I have thought about my earlier post, refined it and reposting it. I am actually new to schema designing. I have read few articles about data modeling and started building schemas for real time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.