Commit Graph

2990 Commits

Author SHA1 Message Date
donghaokun
839468f52d docs/sched/sched: Add documentation for different sleep interfaces
Some checks failed
Build Documentation / build-html (push) Has been cancelled
As pull request apache#17200 & apache#17368 introduced support for scheduling sleep, a documentation is needed for different sleep interfaces.
This patch adds the description for sleep interfaces currently provided in NuttX, including Scheduled sleep(nxsched_sleep()...), Signal-scheduled sleep(nxsig_sleep()...), and Busy sleep(up_udelay()).

Signed-off-by: Haokun Dong <donghaokun@lixiang.com>
2025-12-04 18:59:58 +01:00
hujun5
0c6fe04ef7 Revert "sched_idletask: remove the check for whether tcb is NULL"
This reverts commit 8f91054b1d.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-11-27 08:26:53 -03:00
hujun5
8f91054b1d sched_idletask: remove the check for whether tcb is NULL
We should not call these functions before nx_start;
therefore, this_task will not be empty.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-11-25 16:01:29 -03:00
Matteo Golin
3edf6de552 drivers/timers/arch_alarm: Revert removal of ndelay_accurate
Some checks failed
Docker-Linux / push (push) Has been cancelled
This reverts the removal of ndelay_accurate from #14450, since as
mentioned in #17011, this fails to consider the `sim` architecture
where CONFIG_BOARD_LOOPSPERMSEC was set to 0 because of reliance on the
accurate implementations of the up_delay functions. All the commit did
was remove a more accurate implementation in favour of a less accurate
one.

Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
2025-11-25 22:33:48 +08:00
wangchengdong
ba05c7f133 sched/signal: Fix nxsig_ismember() return value behavior
Some checks failed
Build Documentation / build-html (push) Has been cancelled
nxsig_ismember() has a return type of int, but the current
implementation returns a boolean value, which is incorrect.

All callers should determine membership by checking whether
the return value is 1 or 0, which is also consistent with the POSIX sigismember() API.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 10:02:52 +08:00
wangchengdong
817e4ee354 sched/signal: Initialize signal action pool during init phase
Initialize the signal action pool during the signal initialization
phase to improve performance and reduce footprint.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-25 10:02:24 +08:00
wangchengdong
bc561c677a sched/sleep: Add nxched_nanosleep() API
Introduce the nxched_nanosleep() API to provide a lightweight
nanosecond-level sleep based on nxsched_ticksleep().
This API offers the same functionality as nxsig_nanosleep() but without
signal-related overhead, making it suitable for implementing libc
sleep() or usleep() when signals are disabled.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 10:02:13 +08:00
wangchengdong
df17524e55 sched/signal: Use clock_compare() in nxsig_clockwait()
clock_compare() should be used when comparing clock tick values to
ensure correct handling of tick wrap-around and time ordering.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
wangchengdong
7421846c94 sched/signal: Remove redundant wd_cancel() in nxsig_clockwait()
Both the watchdog timeout and signal dispatch paths already cancel
the watchdog timer, so the explicit wd_cancel() call is redundant.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
wangchengdong
a232815648 sched/signal: Fix remaining time calculation in nxsig_clockwait()
Always compute the expected wake-up time by default, so the remaining
time can be calculated correctly when the flag is not TIMER_ABSTIME.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-25 09:50:13 +08:00
chengdong wang
df3fe0e16f Revert "sched/signal: Unblock task waiting for event when a signal received"
Some checks failed
Build Documentation / build-html (push) Has been cancelled
Events are not defined in the POSIX standard, so signals are not required
to wake tasks that are in the TSTATE_WAIT_EVENT state.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-24 11:00:23 -03:00
wangchengdong
e758e92830 sched/signal: Unblock task waiting for event when a signal received
Some checks failed
Build Documentation / build-html (push) Has been cancelled
If the task is blocked waiting for a event, then that task must
 be unblocked when a signal is received.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-22 17:24:53 -03:00
ouyangxiangzhen
fc28b93224 timers/oneshot: Remove all callback and args.
This commit remove all callback and args in the APIs.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-11-18 13:02:29 +01:00
ouyangxiangzhen
5c113f79b7 timers/oneshot: Remove all private callback.
This commit removed all private callback and handle it on the upperhalf
of oneshot.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-11-18 13:02:29 +01:00
wangchengdong
1b58a449c5 sched/clock: Make g_system_ticks a static variable
Make g_system_ticks a static variable, as it is only accessed by
    clock_get_sched_ticks().

    This change improves code modularity and enhances safety, since
    clock_get_sched_ticks() performs proper synchronization when
    reading g_system_ticks.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-13 18:40:16 -03:00
wangchengdong
2440b4ef39 sched/sched: Fix nxsched_suspend() logic
nxsched_deliver_task() or nxsched_merge_pending() should only be
called when a context switch is required. This behavior is
independent of whether the current task is locked.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-13 21:01:18 +08:00
hujun5
d94cb53d6c sched_lock: remove the check for whether tcb is NULL
Remove Redundant Checks

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-11-11 17:26:30 +08:00
wangchengdong
71f53c6c74 sched/wdog: Inline wd_start() to improve performance
Move wd_start() to an inline function to reduce function call
    overhead and improve performance in time-critical watchdog operations.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-10 23:16:57 +08:00
chao an
8847389886 style/FAR: remove FAR qualifier
N/A, remove FAR qualifier from non-pointer types

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-11-10 10:56:29 +01:00
wangchengdong
de0aead39f sched/wdog: Fix wd_start() boundary check for WDOG_MAX_DELAY
The current implementation allows the delay passed to wd_start() to be
   equal to WDOG_MAX_DELAY. However, since clock_delay2abstick() internally
   increments the delay by one tick, using a delay equal to WDOG_MAX_DELAY
   will cause an overflow and make the clock comparison invalid.

   This patch fixes the issue by disallowing the delay to be equal to
   WDOG_MAX_DELAY.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-10 10:45:15 +08:00
wangchengdong
190daef2bf sched/wdog: Use list_in_list() to detect active watchdogs
Use list_in_list() to determine whether a watchdog is active,
     eliminating the need to set the watchdog function pointer to NULL
     as an indicator of inactivity.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-07 10:30:53 +08:00
wangchengdong
1217fcf907 sched/wdog: Improve wdog Precision
Currently, wd_timer() obtains the current systick
    from the input parameter, and the wdog processes
    the list without updating the systick value.
    This is not incorrect, as the systick should continue
    to increase while the watchdog is processing callbacks.
    This patch addresses the issue by retrieving the updated
    systick value after each watchdog node loop.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-06 13:26:58 +08:00
wangchengdong
522f32f7ef sched/event: Fix uninitialized need_switch flag issue in event_post()
The `need_switch` flag was not initialized in `event_post()`. Since it is
    a local variable, it could contain a random non-zero value, leading to
    incorrect behavior.
    In addition, this patch also fixes an issue where the event clearing
    operation in `event_post()` was not performed correctly.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-04 21:43:59 +08:00
Jukka Laitinen
e45f91f350 sched/signal/sig_dispatch.c: Remove DEBUGASSERT(!sched_idletask())
The DEBUGASSERT is wrong in two cases;
1) In SIM, which does sim_timer_update from the idle task, and the sim timer runs on signals
2) If signal is dispatched from within an interrupt

In these cases, if there are not available items for pending signals, return
the nxsig_tcbdispatch with -EAGAIN instead of asserting.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-11-02 16:46:18 -03:00
chao an
62660a692d sched/task: move lock to mhead scope
1. rename mutex_lock to mhead_lock to declare purpose of lock
2. move lock to mhead scope

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-11-01 22:52:07 +08:00
wangchengdong
d8304979b4 tools/nxgdb: Add gdb and statenames for TSTATE_WAIT_EVENT
This patch add new taskstate entry in tools/pynuttx/nxgdb/thread.py
   and in sched_get_stateinfo.c/g_statenames for the new added task
   state: TSTATE_WAIT_EVENT

   test log is:
     nsh> ps
       PID  PPID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK    USED FILLED COMMAND
         0     0     0   0 FIFO     Kthread   - Ready              0000000000000000 0001008 0000196  19.4%  Idle_Task
         1     0     0 192 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0001984 0000016   0.8%  hpwork 0x70000100 0x7000014c
         2     0     2 100 FIFO     Task      - Running            0000000000000000 0002008 0000740  36.8%  nsh_main
         4     0     0 100 FIFO     Kthread   - Waiting  Event     0000000000000000 0000984 0000012   1.2%  event_wait_task

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
de5973268b [!] sched/event: Remove wait object dependency from event implementation
The current event implementation relies on a dedicated wait object,
    which introduces several issues:
        1. Increases memory usage
        2. Complicates the event logic
        3. Makes the API design less clean, as it requires a separate
                nxevent_tickwait_wait() function

    This patch removes the event’s dependency on the wait object
    and eliminates the nxevent_tickwait_wait() API accordingly.

    BREAKING CHANGE: this commit removed the nxevent_tickwait_wait function
    so the related docs should be updated accordingly

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
94cfb1180b sched/event: Implement nxevent_timeout with nxevent_wait_irq
Implement nxevent_timeout with nxevent_wait_irq to remove
   code duplication

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
39f54c1524 sched/event: Add nxevent_wait_irq() to handle the task cancel
If the thread is blocked waiting on a event, then the
    thread must be unblocked from the evnet to handle
    the task cancellation.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
735e489070 [!] sched/event: Replace semaphore with direct scheduler operations in event
The current event implementation uses semaphores for wait and post
    operations. Since semaphores are relatively heavy-weight and intended
    for resource-based synchronization, this is suboptimal.

    So this patch replaced the semaphore-based mechanism with direct
    scheduler operations to improve performance and reduce memory footprint.

    This patch also introduce a new task state TSTATE_WAIT_EVENT to indicate
    the task is waiting for a event.

    BREAKING CHANGE:  This commit introduced a new task state TSTATE_WAIT_EVENT
    so apps/nshlib/, procfs/ and tools/pynuttx/nxgdb/ are needed to be updated accordingly.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
5fa8634ce7 sched/event: Restore critical section protection for event wait and post
Restore the use of critical sections to provide mutual exclusion
    between event wait and post operations. This allows replacing the
    heavier semaphore-based mechanism with direct scheduler operations
    for synchronization.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 19:56:32 -03:00
wangchengdong
a04f257d4b sched/wqueue: Replace spinlock/sched_lock with spin_lock_irqsave_nopreempt
Replace the spinlock/sched_lock pair in the sched wqueue module with
    spin_lock_irqsave_nopreempt() to improve code clarity and consistency.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 15:02:56 +08:00
wangchengdong
a49a5fd27f sched/clock: Replace spinlock/sched_lock with spin_lock_irqsave_nopreempt
Replace the spinlock/sched_lock pair in the sched clock module with
    spin_lock_irqsave_nopreempt() to improve code clarity and consistency.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-31 15:02:56 +08:00
wangchengdong
ace17ad902 sched/sched: Use nxsched_wakeup() to implement nxsched_timeout()
Refactor nxsched_timeout() to use nxsched_wakeup() in order to
    eliminate code duplication and improve maintainability.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-28 10:43:24 +08:00
wangchengdong
5beb4ae6d5 sched/sched: Introduce nxsched_wakeup()
Add a new function nxsched_wakeup() to the scheduler,
    which allows waking up a sleeping task before its timeout.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-28 10:43:24 +08:00
wangchengdong
201406b22b sched/sched: Reuse nxsig_wait_irq() in nxsig_timeout()
Currently, there is duplicated code in nxsig_wait_irq() and nxsig_timeout().
    This patch updates the input parameters of nxsig_wait_irq() so that it can
    be reused in nxsig_timeout(), reducing code duplication and improving
    maintainability.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-27 10:05:45 -03:00
wangchengdong
6913e52ac3 sched/signal: Remove redundant code in nxsig_wait_irq()
nxsig_wait_irq() is always invoked within an existing critical section,
    and the task state is already verified, similar to nxsem_wait_irq().
    Therefore, the additional critical section protection and task state
    check inside the function are redundant and have been removed.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-27 10:05:45 -03:00
wangchengdong
97db9a499c sched/signal: use list_waitingforsignal() to access g_waitingforsignal
Replace direct access to the g_waitingforsignal list with the
    NuttX-defined macro list_waitingforsignal() to improve code
    consistency and maintainability.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-20 11:24:25 -03:00
wangchengdong
519ffe66d0 sched/sched: Replace nxsched_alarm_tick_expiration()
Introduce a new function nxsched_tick_expiration() to replace
    nxsched_alarm_tick_expiration(). The new function provides a
    common implementation that can be shared by both the alarm
    and timer architectures.

    This change reduces code duplication and provides a unified
    function that can be directly reused.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-18 20:07:59 +08:00
wangchengdong
c7b6442974 sched/clock: Improve CLOCK_MONOTONIC nxclock_gettime()
Improve nxclock_gettime(), to get the system time excluding
   the time that the system is suspended, when the clockid is
   CLOCK_MONOTONIC

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-18 20:07:59 +08:00
wangchengdong
1a22f4bf28 sched/clock: Improve clock implementation with sched ticks functions
Replace direct usage of g_system_ticks with clock_increase_sched_ticks()
   and clock_get_sched_ticks()

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-18 20:07:59 +08:00
wangchengdong
54adf5979c sched/clock: Add clock_increase_sched_ticks() and clock_get_sched_ticks()
In the current NuttX kernel implementation, there is no function
    to represent pure scheduler ticks.
    The existing clock_systime_ticks() cannot serve this purpose,
    because it keeps increasing even when the scheduler is not running.
    This happens since the returned value reflects real hardware time:
    as long as the timer or alarm hardware is active,
    clock_systime_ticks() will continuously increase.

    This patch introduces two new functions that provide access to
    scheduler ticks — a counter that only increases when the scheduler
    itself is running.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-10-18 20:07:59 +08:00
chao an
87f134cfaa 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>
2025-10-17 14:05:02 +08:00
chao an
02bfd290ac sched/sleep: add support for scheduling 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>
2025-10-16 12:46:42 +02:00
guoshengyuan1
5874f46a5b sched/irq: add stack size judgment when detecting stack overflow
`up_check_intstack` exists only when CONFIG_ARCH_INTERRUPTSTACK
is greater than 0, so the irq should first determine whether
CONFIG_ARCH_INTERRUPTSTACK is greater than 0 to determine
whether up_check_intstack can be called.

Signed-off-by: guoshengyuan1 <guoshengyuan1@xiaomi.com>
2025-10-14 17:42:01 +08:00
chao an
10fd309452 sched/signal: Remove shadow definitions to reduce unnecessary API
Reduce unnecessary API definitions:

nxsig_waitinfo() -> nxsig_timedwait()

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-14 17:40:18 +08:00
wangchengdong
07a470f696 sched: Improve nxsched stack overflow checking implementation
1. Remove STACKCHECK_SOFTWARE config,
   2. Do sp value checking when STACKCHECK_MARGIN == 0,
   3. Do margin-based stack check when STACKCHECK_MARGIN > 0,
   4. Disable stack check when STACKCHECK_MARGIN == -1

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-11 18:00:44 +08:00
chao an
4c7deedd27 sched/clock: remove return value of clock_systime_timespec()
clock_systime_timespec() always returns 0, so there is no need to
check the return value in the caller code, let us remove the return
value directly.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-10 23:15:27 +08:00
chao an
09247c113a sched/clock: remove unused function clock_dow() (dow:day of week)
1. This file is not included in the makefile.
2. This feature should be replaced with mktime().

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-09 15:07:35 -03:00
wangchengdong
08050f506c sched/wdog: remove unnecessary list_node redefinition
The wdog implementation redefined `list_node`, which could cause
  conflicts or unexpected behavior when both `wdog/wdog.h` and
  `list.h` are included in the same source file. This patch removes
  the redundant definition to avoid such issues.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-09 11:41:32 +08:00