简体   繁体   中英

A thread who is spinning and trying to get the spinlock can't be preempted?

When a thread on Linux is spinning and trying to get the spinlock, Is there no chance this thread can be preempted?

EDIT:
I just want to make sure some thing. On a "UP" system, and there is no interrupt handler will access this spinlock. If the thread who is spinning and trying to get the spinlock can be preempted, I think in this case, the critical section which spinlock protects can call sleep , since the thread holding spinlock can be re-scheduled back to CPU.

No it cannot be preempted: see the code (taken from linux sources) http://lxr.free-electrons.com/source/include/linux/spinlock_api_smp.h?v=2.6.32#L241

241 static inline unsigned long __spin_lock_irqsave(spinlock_t *lock)
242 {
243         unsigned long flags;
244 
245         local_irq_save(flags);
246         preempt_disable();
247         spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
248         /*
249          * On lockdep we dont want the hand-coded irq-enable of
250          * _raw_spin_lock_flags() code, because lockdep assumes
251          * that interrupts are not re-enabled during lock-acquire:
252          */
253 #ifdef CONFIG_LOCKDEP
254         LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
255 #else
256         _raw_spin_lock_flags(lock, &flags);
257 #endif
258         return flags;
259 }
260 
[...]
349 static inline void __spin_unlock(spinlock_t *lock)
350 {
351         spin_release(&lock->dep_map, 1, _RET_IP_);
352         _raw_spin_unlock(lock);
353         preempt_enable();
354 }

see lines 246 and 353

By the way It is generally a bad idea to sleep while holding a lock (spinlock or not)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM