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>
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>
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
Initialize the signal action pool during the signal initialization
phase to improve performance and reduce footprint.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
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
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
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
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
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
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>
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
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>
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>
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>
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
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>
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
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>
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
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
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
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
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
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
Refactor nxsched_timeout() to use nxsched_wakeup() in order to
eliminate code duplication and improve maintainability.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
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
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>
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>
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
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>
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
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
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
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>
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>
`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>
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>
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>
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>