一个正在旋转并试图获取自旋锁的线程不能被抢占?

时间:2022-09-02 20:47:44

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

当Linux上的一个线程正在旋转并试图获取spinlock时,这个线程不可能被抢占吗?

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.

编辑:我只是想确定一下。在“向上”系统上,没有中断处理程序将访问这个自旋锁。如果正在旋转并试图获取自旋锁的线程可以被抢占,我认为在这种情况下,自旋锁保护的关键部分可以调用sleep,因为持有自旋锁的线程可以重新调度回CPU。

1 个解决方案

#1


2  

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

不,它不能被抢占:查看代码(来自linux源代码)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

参见第246和353行

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

顺便说一下,抱着一把锁睡觉通常是个坏主意(不管你是不是旋锁)

#1


2  

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

不,它不能被抢占:查看代码(来自linux源代码)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

参见第246和353行

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

顺便说一下,抱着一把锁睡觉通常是个坏主意(不管你是不是旋锁)