>It isn't an initialization problem, the real cause is some code abusing >spin lock(lock/unlock in the different thread). >After holding sched_lock in spinlock_irqsave, the api requires that the >lock/unlock come from the same thread.
So this brings up a potential problem with spin_lock, right? >All performance critical code is evaluating carefully to skip the >sched_lock/sched_unlock, like this: >https://github.com/apache/nuttx/pull/15695 >And the optimization of sched_lock is under heavy development. *raw_spin_lock_irqsave()* is not equivalent to without *sched_lock()*. On the contrary, in the Linux kernel, raw_spin_lock_irqsave() means that preemption is disabled by default. https://github.com/torvalds/linux/blob/master/include/linux/spinlock_api_smp.h#L104-L113 [image: image.png] >Here is the summary why I prefer to add sched_lock in >spin_lock/spin_lock_irqsave by default: > - The default(short) api should be safe since not all people understand > the schedule behaviour deeply, please see the related discussion: > https://github.com/apache/nuttx/issues/9531 > https://github.com/apache/nuttx/issues/1138 > https://www.kernel.org/doc/Documentation/preempt-locking.txt > - spin_lock without sched_lock is unusable in the normal thread context, > since the scheduler may suspend the spinlock owner thread at any time 1. spin_lock can be applied in scenarios requiring more fine-grained control. For example, in some cases, we can conditionally decide whether to hold the spin_lock, rather than simply forcing all users to use the version with sched_lock: [image: image.png] > - API semantic align with Linux to avoid Linux developer use the unsafe > api without any notification 2. NuttX is not Linux. We need to enable API users to truly understand what happens inside the API just by looking at its name. > - the caller of enter_critical section can call sem_post/mq_send without > problem, spin_lock/spin_lock_irqsave can only achieve the same behaviour by > holding sched lock 3. We should handle scenarios that may cause scheduling differently. We should use *spin_lock_irqsave_nopreempt()* instead of providing developers with a low - performance version by default.