# spin lock in FreeBSD



## luo (Oct 17, 2011)

I find mtx_lock_spin() API will disable local CPU interrupts while holding a spin lock.

In linux, while acquiring a spin lock, spin_lock_bh() API just spins, doesn't disables local CPU interrupts and never sleeps. Does FreeBSD have such API for spin lock? In other words, I need a spin lock, which just spins, never sleeps and doesn't disable local CPU interrupts.

thanks


----------



## fefaya (Oct 17, 2011)

luo said:
			
		

> I need a spin lock, which just spins, never sleeps



Hmm...are you sure that mtx_lock_spin() in FreeBSD will sleep? I thought that spin lock have 'spin' on its name because it will spin, not sleep.


----------



## luo (Oct 18, 2011)

fefaya said:
			
		

> Hmm...are you sure that mtx_lock_spin() in FreeBSD will sleep? I thought that spin lock have 'spin' on its name because it will spin, not sleep.



I need such a spin lock in FreeBSD, which meets the following three condition:

1 don't sleep.
2 just spins while acquiring a lock.
3 don't disable local CPU interrupts.

thanks


----------



## SirDice (Oct 18, 2011)

Have you read the mtx_lock_spin(9) man page?



> There are currently two flavors of mutexes, those that context switch when they block and those that do not.
> 
> By default, MTX_DEF mutexes will context switch when they are already held. As an optimization, they may spin for some amount of time before context switching. It is important to remember that since a thread may be preempted at any time, the possible context switch introduced by acquiring a mutex is guaranteed to not break anything that is not already broken.
> 
> ...


----------



## trasz@ (Nov 1, 2011)

@luo: Why do you need something like that?  I assume you know that

1. Spinlocks are not the main synchronisation mechanism in FreeBSD, unlike in Linux,

2. Code running in interrupt handlers may perform bounded sleep, unlike in Linux,

3. Thus, most of the time you just use mutex and don't care about disabling interrupts.


----------

