Re: Definition of spin_lock(lock) and spin_unlock(lock)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, May 30, 2002 at 10:18:05AM +0530, Sharath Ballal wrote:
> Semaphores are used for synchronization.   If you have a piece of code,
> which will be run by multiple threads,
> then semaphores is one way of synchronization.  The condition to enter the
> code could be when the semaphore
> value is 0.  The thread would then make it 1 atomically thus obtaining the
> lock and at the end would make it 0 again.

Sorry, byt you seem to have te numbers reversed. Semaphores block when
they would go below zero, but they can go up to arbitrary positive
integer. Thus down is "lock" and passes when the semaphore is (at least)
1 and blocks when it's 0.

>  Any other thread will not be able to execute the code till the semaphore
> value becomes 0.
> 
> Other ways of synchronization are to use mutex, which are implemented using
> pthreads.

Since we are talking about kernel - "mutex"es ARE semaphores.
But to be more precise. "mutex" stands for "mutual exclusion" and is
used to refer to any synchronization primitive, that can assure that.
In kernel, there are two: semaphores and spinlocks. Semaphore schedule
if they can't down, so they are useable when you are going to schedule
within the locked code and they must be used on UP because arbitrary
code may run while the thread is sleeping.

On the other hand spinlocks perform busy-waiting (while(lock){}), so
they can't be used to protect code that may sleep (because the other
thread wouldn't let you finish and unlock). But no code may break in
something that does not sleep on UP. Thus they are defined only for SMP
and expand to no-ops on UP (which is I hope answer for the original
question).

Note: spinlocks are not semaphores and they have value 1 when locked for
implementation reasons.

Note: There is no pthread library in kernel. It's in userland and may be
actually implemented in various ways.

--------------------------------------------------------------------------------
                  				- Jan Hudec `Bulb' <bulb@ucw.cz>
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux