spin lock in FreeBSD

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
 
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.
 
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
 
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.

Mutexes which do not context switch are MTX_SPIN mutexes. These should only be used to protect data shared with primary interrupt code. This includes INTR_FAST interrupt handlers and low level scheduling code. In all architectures both acquiring and releasing of a uncontested spin mutex is more expensive than the same operation on a non-spin mutex. In order to protect an interrupt service routine from blocking against itself all interrupts are either blocked or deferred on a processor while holding a spin lock. It is permissible to hold multiple spin mutexes.

Once a spin mutex has been acquired it is not permissible to acquire a blocking mutex.
 
@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.
 
Back
Top