linux spin-lock function

We have seen two functions, spin_lock and spin_unlock, spin locks can be operated. There are several other functions, however, have similar's name and purpose. We will now show a full set. This will take us to a discussion that we can not where appropriate within the chapters covered; a complete understanding of the needs of the spin lock API interrupt handling and understanding of related concepts.

There are actually four functions can lock a spin lock: void spin_lock (spinlock_t * lock);

void spin_lock_irqsave(spinlock_t *lock, unsigned long flags);

void spin_lock_irq(spinlock_t *lock); void spin_lock_bh(spinlock_t *lock)

 

We have seen how the spin lock work spin_loc_irqsave disable interrupts (only in the local processor) before acquiring the spin lock;.. Before the interrupt status stored in the flags in if you are absolutely sure on your processor does not prohibit the interruption (or, in other words, are you sure you should open when you release your spinlock interrupt), you can use spin_lock_irq in place, and do not have to keep track of flags. Finally, spin_lock_bh prohibited software interrupts before acquiring the lock, but the hardware interrupt reserved open.

 

If you have a possibility to be in the (hardware or software) code in the context of running spin locks get interrupted, you must use a form spin_lock to disable interrupts. Other approaches may deadlock the system, sooner or later. If you are not in the hardware interrupt handler access your locks, but you through software interrupt (for example, in a tasklet code running in the topics covered in Chapter 7), you can use spin_lock_bh to safely avoid deadlocks, while still allowing hardware interrupts to be serviced.

There are four ways to release a spin lock; that you must use the corresponding function is used to get you lock void spin_unlock (spinlock_t * lock).;

void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags);

void spin_unlock_irq(spinlock_t *lock); void spin_unlock_bh(spinlock_t *lock);

 

Each variant spin_unlock recovery made by the corresponding lock spin_lock work function. Spin_unlock_irqrestore parameters to pass flags must be passed to the same variable spin_lock_irqsave

 

 

Amount. You must also call spin_lock_irqsave and spin_unlock_irqrestore in the same function in. Otherwise, you may destroy some of the code system.

 

There are a set of non-blocking spin-lock operations:

 

int spin_trylock(spinlock_t *lock); int spin_trylock_bh(spinlock_t *lock);

 

These functions return zero on success (get a lock), otherwise 0. no "try" version to disable interrupts.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11141722.html