About locks on hardwares. MTX_DEF or MTX_SPIN?

In ata_attach, the channels' locks are initialized by:
mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);

As I know, MTX_DEF is more costly than MTX_SPIN. Does FreeBSD use MTX_DEF on hardwares widely? If yes, what makes it necessary to use MTX_DEF other than MTX_SPIN.
 
coopci said:
Sorry, I did read the man text about mutex carefully enough ......
err... I mean "I did NOT read the man text about mutex carefully enough ......"

MTX_DEF spins for a while if the mutext is held by another thread which is running on a processor. So the anwser is obvious......
 
In fact, MTX_DEF is cheaper to acquire than MTX_SPIN on most hardware, because spin locks disable interrupts as well as use an atomic operation, whereas default mutexes only use an atomic operation.

FreeBSD default mutexes are "adaptive" meaning that they will also spin when contending a lock if the thread holding the lock is currently in execution on another CPU. If the thread holding the lock yields or is preempted, then another thread trying to acquire the lock will also yield, rather than spinning waiting for a non-running thread. As a result, default mutexes (and similar lock classes, such as rwlocks or rmlocks) are almost always preferred to spin mutexes.

The only exceptions are in the scheduler, and when in or synchronizing with "fast" interrupt handlers, which borrow the execution context of an existing kernel thread, and require interrupts to be disabled to avoid deadlock.
 
Back
Top