nuttx icon indicating copy to clipboard operation
nuttx copied to clipboard

sched:rw_sem: replace mutex with spinlock

Open TaiJuWu opened this issue 1 year ago • 1 comments

Summary

The overhead of spinlok is less than mutext (mutex need to call enter_critical section.) After this patch, down_write_trylock and down_read_trylock can be use in interrupt context.

Impact

semaphore

Testing

CI

TaiJuWu avatar Feb 26 '24 01:02 TaiJuWu

Hi @xiaoxiang781216,

Do you know why some platform not config spinlock? Even in 1cpu, in multi-threading environment spinlock is also needed.

How do you think this path? Is it reasonable?

TaiJuWu avatar Feb 27 '24 09:02 TaiJuWu

Ping @xiaoxiang781216

TaiJuWu avatar Mar 13 '24 20:03 TaiJuWu

Hi @xiaoxiang781216,

Do you know why some platform not config spinlock? Even in 1cpu, in multi-threading environment spinlock is also needed.

How do you think this path? Is it reasonable?

spinlock isn't required for single core.

xiaoxiang781216 avatar Mar 14 '24 03:03 xiaoxiang781216

Hi @xiaoxiang781216, Do you know why some platform not config spinlock? Even in 1cpu, in multi-threading environment spinlock is also needed. How do you think this path? Is it reasonable?

spinlock isn't required for single core.

Yes, your right. If we do not consider single core. Use spinlock is better than mutex right?

For single core, we also don't need spinlock. Is there any problem? Or I missed something?

TaiJuWu avatar Mar 14 '24 04:03 TaiJuWu

In the single core, spinlock map to disable/enable irq

xiaoxiang781216 avatar Mar 14 '24 05:03 xiaoxiang781216

In the single core, spinlock map to disable/enable irq

Oh, I made a mistake. If I want to replace mutex with spinlock, I should use spinlock_irqsave() and spinlock_irqrestore().

In the mutex, it will call enter_critical_section, this function will disable interrupt, right? If only use spinlock, the interrupt is not disable.

But It will increase interrupt latency.

TaiJuWu avatar Mar 14 '24 05:03 TaiJuWu

Hi @xiaoxiang781216, Do you know why some platform not config spinlock? Even in 1cpu, in multi-threading environment spinlock is also needed. How do you think this path? Is it reasonable?

spinlock isn't required for single core.

In my mind, single core may need spinlock too, because there are two hardware threading(register sets) in a core.

TaiJuWu avatar Mar 19 '24 09:03 TaiJuWu

Hi @xiaoxiang781216, Do you know why some platform not config spinlock? Even in 1cpu, in multi-threading environment spinlock is also needed. How do you think this path? Is it reasonable?

spinlock isn't required for single core.

In my mind, single core may need spinlock too, because there are two hardware threading(register sets) in a core.

no, disable interrupt could forbid other thread switch in.

xiaoxiang781216 avatar Mar 19 '24 10:03 xiaoxiang781216

@TaiJuWu Which configuration does the CI use to check this PR?

masayuki2009 avatar Mar 19 '24 11:03 masayuki2009

@TaiJuWu Which configuration does the CI use to check this PR?

You can see the related log of CI. https://github.com/apache/nuttx/pull/11769#discussion_r1526280639

TaiJuWu avatar Mar 19 '24 15:03 TaiJuWu

@TaiJuWu Which configuration does the CI use to check this PR?

You can see the related log of CI. #11769 (comment)

So, do you mean that this PR does not break the CI now? However, my understanding is that there is no test code that uses rw_sem so far.

masayuki2009 avatar Mar 19 '24 23:03 masayuki2009

@TaiJuWu Which configuration does the CI use to check this PR?

You can see the related log of CI. #11769 (comment)

So, do you mean that this PR does not break the CI now? However, my understanding is that there is no test code that uses rw_sem so far.

Both answers are yes. There is no testcase for rw_sem at run time.

TaiJuWu avatar Mar 20 '24 00:03 TaiJuWu