Skip to content

Conversation

@anchao
Copy link
Contributor

@anchao anchao commented Oct 16, 2025

Summary

Continuous work of #17200

sched/sleep: replace all Signal-based sleep implement to Scheduled sleep

Nuttx currently has 2 types of sleep interfaces:

  1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
    Weaknesses:
    a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
    b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.

  2. Busy sleep: up_mdelay() / up_udelay()
    Weaknesses:
    a. Does not actively trigger scheduling, occupy the CPU loading.

  3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
    Strengths:
    a. Does not depend on the signal framework.
    b. Tick-based, without additional computational overhead.

Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.

(This PR also aims to make signals optional, further reducing the code size of Nuttx.)

Signed-off-by: chao an anchao.archer@bytedance.com

Impact

N/A

Testing

new api test on sim/nsh, sabre-6quad/smp:

nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()

@github-actions github-actions bot added Area: Bluetooth Area: Networking Effects networking subsystem Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: renesas Issues related to the Renesas chips labels Oct 16, 2025
@anchao anchao force-pushed the 25101601 branch 2 times, most recently from 09f198c to dc62f8a Compare October 16, 2025 04:01
@fdcavalcanti
Copy link
Contributor

How did you test this? Can you share?

Nuttx currently has 2 types of sleep interfaces:

1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
Weaknesses:
a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.

2. Busy sleep: up_mdelay() / up_udelay()
Weaknesses:
a. Does not actively trigger scheduling, occupy the CPU loading.

3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
Strengths:
a. Does not depend on the signal framework.
b. Tick-based, without additional computational overhead.

Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.

(This PR also aims to make signals optional, further reducing the code size of Nuttx.)

Signed-off-by: chao an <anchao.archer@bytedance.com>
To make checker happy:

arch/arm/src/sama5/sam_classd.c:997: nd ==> and, 2nd
arch/arm/src/sama5/sam_classd.c:1362: levl ==> level
drivers/sensors/apds9922.c:286: persistance ==> persistence

Signed-off-by: chao an <anchao.archer@bytedance.com>
@anchao
Copy link
Contributor Author

anchao commented Oct 16, 2025

How did you test this? Can you share?

@fdcavalcanti
In our internal projects, some drivers make extensive use of the sleep interface.
In the nuttx project, I tested 2 configurations: sim/nsh and sabre-6quad/smp. I also have a simplified test code:

int main(int argc, FAR char *argv[])
{
  syslog(1, "Hello, World!!\n");
  nxsched_msleep(1000);
  syslog(1, "Hello, World!!\n");
  nxsched_usleep(1000 * 1000); 
  syslog(1, "Hello, World!!\n");
  nxsched_sleep(1);
  syslog(1, "Hello, World!!\n");
  return 0;
}

here is output:

$ qemu-system-arm -semihosting -M sabrelite -m 1024 -smp 4 -nographic -kernel ./nuttx

NuttShell (NSH) NuttX-10.4.0
nsh> hello
[    1.280000] [CPU1] Hello, World!!
[    2.290000] [CPU0] Hello, World!!
[    3.300000] [CPU0] Hello, World!!
[    4.310000] [CPU0] Hello, World!!
nsh>

could you help test the ESP chip? Some replacements may affect the ESP driver behavior.

Fix build break:

| arm-none-eabi-ld: /home/archer/code/nuttx/nuttx section `.data' will not fit in region `kflash'
| arm-none-eabi-ld: region `kflash' overflowed by 96 bytes
| Memory region         Used Size  Region Size  %age Used
|           kflash:       65632 B        64 KB    100.15%
|           uflash:           0 B        64 KB      0.00%
|           xflash:           0 B       128 KB      0.00%
|           ksram1:        4716 B         6 KB     76.76%
|           usram1:           0 B         4 KB      0.00%
|           xsram1:           0 B        22 KB      0.00%
|            sram2:           0 B        16 KB      0.00%
| make[1]: *** [Makefile:217: nuttx] Error 1
| make: *** [tools/Unix.mk:542: nuttx] Error 2

Signed-off-by: chao an <anchao.archer@bytedance.com>
@fdcavalcanti
Copy link
Contributor

How did you test this? Can you share?

@fdcavalcanti In our internal projects, some drivers make extensive use of the sleep interface. In the nuttx project, I tested 2 configurations: sim/nsh and sabre-6quad/smp. I also have a simplified test code:

int main(int argc, FAR char *argv[])
{
  syslog(1, "Hello, World!!\n");
  nxsched_msleep(1000);
  syslog(1, "Hello, World!!\n");
  nxsched_usleep(1000 * 1000); 
  syslog(1, "Hello, World!!\n");
  nxsched_sleep(1);
  syslog(1, "Hello, World!!\n");
  return 0;
}

here is output:

$ qemu-system-arm -semihosting -M sabrelite -m 1024 -smp 4 -nographic -kernel ./nuttx

NuttShell (NSH) NuttX-10.4.0
nsh> hello
[    1.280000] [CPU1] Hello, World!!
[    2.290000] [CPU0] Hello, World!!
[    3.300000] [CPU0] Hello, World!!
[    4.310000] [CPU0] Hello, World!!
nsh>

could you help test the ESP chip? Some replacements may affect the ESP driver behavior.

I will run our internal CI, please don't merge for a while.

Copy link
Contributor

@linguini1 linguini1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new interface, but I'm not confident about changing all the calls to nxsig_sleep. It will be hard to test all of those drivers, and sometimes the difference in timing between sleep implementations can cause issues. I think this should just be the interface used going forward, and drivers can be swapped out one by one when they're able to be tested. I'm worried this will break some drivers and there's no testing for those.

@anchao
Copy link
Contributor Author

anchao commented Oct 16, 2025

I like the new interface, but I'm not confident about changing all the calls to nxsig_sleep. It will be hard to test all of those drivers, and sometimes the difference in timing between sleep implementations can cause issues. I think this should just be the interface used going forward, and drivers can be swapped out one by one when they're able to be tested. I'm worried this will break some drivers and there's no testing for those.

The intent of this change is to make the signal framework configurable, as most RTOS devices don't require signal support and it would be too burdensome for an RTOS environment.

If a complete replacement isn't made at this stage, the driver and BSP code will still rely on signal.

In addition, the replacement of sleep-related implementations will not bring any impact. The implementation details of nxsched_* and nxsig_* is similar.

@fdcavalcanti
Copy link
Contributor

Look good to me.

@linguini1
Copy link
Contributor

One more question: if this replaces all the nxsig_sleep calls, doesn't that make the function obsolete? What's the use case for nxsig_sleep versus this new tick-based method?

@anchao
Copy link
Contributor Author

anchao commented Oct 17, 2025

One more question: if this replaces all the nxsig_sleep calls, doesn't that make the function obsolete? What's the use case for nxsig_sleep versus this new tick-based method?

@linguini1

nxsig_sleep/usleep has the following two features:

  1. Supports the processing of signal sets.
    https://github.com/apache/nuttx/blob/master/sched/signal/sig_timedwait.c#L98-L108
  2. Supports cancellation points.
    https://github.com/apache/nuttx/blob/master/sched/signal/sig_timedwait.c#L275-L289

Both of the above features are mandatory requirements of POSIX, so we cannot remove them.

nxsig_sleep/usleep is the internal implementation of POSIX API sleep/usleep, and userspace needs to follow the semantics of POSIX.

https://pubs.opengroup.org/onlinepubs/009696799/functions/sleep.html

The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.

@linguini1
Copy link
Contributor

Thank you for the clarification @anchao and sorry for the basic questions! That makes much more sense to me now.

Copy link
Contributor

@linguini1 linguini1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it might be a good idea to document somewhere that driver implementations should use this new API to avoid mixing nxsig* back into driver code.

@xiaoxiang781216 xiaoxiang781216 merged commit 2909b1d into apache:master Oct 17, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: renesas Issues related to the Renesas chips Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: xtensa Issues related to the Xtensa architecture Area: Audio Area: Bluetooth Area: Crypto Area: Drivers Drivers issues Area: File System File System issues Area: Graphics Area: Networking Effects networking subsystem Area: OS Components OS Components issues Area: Sensors Sensors issues Area: USB Area: Video Board: arm Board: sparc Board: xtensa Board: z80 Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants