Commit graph

312 commits

Author SHA1 Message Date
Jukka Laitinen
7f8f800e63 arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y
Correct build errors when CONFIG_ENABLE_ALL_SIGNALS is not defined

- sched makefiles: Move pending-signal helpers from the ENABLE_ALL_SIGNALS-only
  list to the !DISABLE_ALL_SIGNALS list so signal dispatch is available in
  PARTIAL builds sched: make SIG_PREALLOC_ACTIONS, SIG_ALLOC_ACTIONS and
  SIG_DEFAULT depend on ENABLE_ALL_SIGNALS
- sched: fix ifdefs around pending-signal queue access and signal-mask for
  PARTIAL/DISABLE modes
- arch: gate SYS_signal_handler / _return calls and SYSCALL_LOOKUP(signal)
  with ENABLE_ALL_SIGNALS

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2026-06-16 17:07:32 +08:00
Piyush Patle
0dccc8ba21 include/debug.h: Move to include/nuttx/debug.h
debug.h is a NuttX-specific, non-POSIX header. Placing it in the
top-level include/ directory creates naming conflicts with external
projects that define their own debug.h.
This commit moves the canonical header to include/nuttx/debug.h,
following the NuttX convention for non-POSIX/non-standard headers,
and updates all in-tree references.

A backward-compatibility shim is left at include/debug.h that
emits a deprecation #warning and re-includes <nuttx/debug.h>,
allowing out-of-tree code to continue building while migrating.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
2026-04-07 07:50:06 -03:00
wangchengdong
9c80011c53 sched/signal: update nxsig_clockwait() to support disable all signals
Update nxsig_clockwait() to handle the case where all signals functions
are disabled.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-02-26 20:03:05 +08:00
wangchengdong
8514e9978f sched/signal: move nxsig_clockwait implementation to a separate file
Move the implementation of nxsig_clockwait() into a separate file
to decouple it from nxsig_timedwait().

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-02-26 20:03:05 +08:00
wangzhi16
364a633ec3 sched/signal: Optimize code logic
Adjust the up_schedule_sigaction function to facilitate subsequent spinlock optimization work.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2026-01-27 21:24:31 +08:00
jiangtao16
bc386d73b4 sched/signal: Fix compilation warning uninitialized.
vela caros CI compilation error: 'stop' may be used uninitialized in this function.

Signed-off-by: jiangtao16 <jiangtao16@xiaomi.com>
2026-01-22 22:14:00 +08:00
wangchengdong
faf864b04f sched/signal: Add support for disabling all signal functions
Signals in NuttX serve two primary purposes:

      1. Synchronization and wake-up:
        Signals can be used to block threads on specific signal sets and later
        wake them up by delivering the corresponding signals to those threads.

      2. Asynchronous notification:
        Signals can also be used to install callback handlers for specific signals, allowing threads to
        asynchronously invoke those handlers when the signals are delivered.

    This change introduces the ability to  disable all signal functionality to reduce footprint for NuttX.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2026-01-18 08:24:13 -03:00
wangchengdong
0ea686bc5b sched/signal: Add support for partially disabling signals
Signals in NuttX serve two primary purposes:

    1. Synchronization and wake-up:
    Signals can be used to block threads on specific signal sets and later
    wake them up by delivering the corresponding signals to those threads.

    2. Asynchronous notification:
    Signals can also be used to install callback handlers for specific signals, allowing threads to
    asynchronously invoke those handlers when the signals are delivered.

This change introduces the ability to partially disable signal functionality: to disable only signal functions for
Asynchronous notification, keeping functions for Synchronization and wake-up.
This enables finer-grained control over signal usage while preserving existing behavior for supported use cases.

Co-authored-by: Guo Shichao guoshichao@xiaomi.com
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2026-01-18 08:24:13 -03:00
hujun5
29bc8c7b15 sched/signal: use spinlock to protect signal queues
Replace global critical section with fine-grained spinlock protection for
signal action queue (tg_sigactionq) and signal pending queue (tg_sigpendingq).
Use spin_lock_irqsave/spin_unlock_irqrestore with group->tg_lock for proper
synchronization, reducing interrupt latency and improving SMP scalability.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-17 18:29:17 -03:00
guoshichao
f47ff76589 signal: add default sigaction for SIGURG
according to the posix standard: the default action for SIGURG is to
ignore the signal, we need to add the default sigaction handler for
SIGURG
this fix the /tset/ANSI.os/signal/signal_X/T.signal_X testcase

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-13 13:56:39 -08:00
anjiahao
6f1e99c9bd sched: assert if call timedwait from interrupt
Nuttx does not allow calling interfaces like TIME_WAIT in interrupts, so we need to directly assert.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-18 18:19:42 +08:00
wangchengdong
ba05c7f133 sched/signal: Fix nxsig_ismember() return value behavior
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
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"
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
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
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
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
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
p-szafonimateusz
0c9d0b3f84 sched/sig_pending: sigpending() should return caller pending signals only
Previously sigpending() returned all pending signals in the group, which is not
POSIX compliant. It should return signals pending only for the caller, so
a signal send with pthread_kill() intended for another thread should not be
returned (it's not pending for a caller).

This fixes the pthread_create.c/test9 test case from
PSE52 Open Group Threads Test Suite.

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 16:15:49 -04:00
Jukka Laitinen
a030e3c8cc sched/signal/sig_dispatch.c: Simplify the nxsig_dispatch
The nxsig_dispatch should just deliver the signal to either a
thread by pid (tid) or to the process (group) by pid.

Simplify the code so that the intent is more obvious.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-09-02 14:56:35 +08:00
Jukka Laitinen
a0caa5cd96 sched/signal/sig_dispatch.c: Correct signal dispatch to specific thread
The signal dispatch is called from interrupt handlers as well, so
this_task() is wrong. The thread to which the signal is supposed to
be delivered is known (stcb), use that.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-09-02 14:56:35 +08:00
wangjianyu3
b9203c1747 sched/signal: Old signal action need save sa_user
For example, set sigaction after create signalfd,
the sa_sigaction was restored but sa_user not,
causing signalfd_action() get the wild private data.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-08-28 21:52:44 +08:00
Jukka Laitinen
0dbbce3a8f sched: Remove unnecessary affinity restriction & restoration over smp calls
When the task has TCB_FLAG_CPU_LOCKED it is locked to the CPU regardless
of the affinity. There is no need to switch the affinity back and forth.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
Jukka Laitinen
3e29996d97 sched: After nxsched_add_readytorun, change first parameter to up_switch_context to this_task()
Change every occurence of up_switch_context to use this_task() as the first parameter.

"nxsched_add_readytorun" returns "true" if context switch is required. "nxsched_add_readytorun"
typically could only switch the assigned/running task to the one which is passed in as parameter.
But this is not strictly guaranteed in SMP; if other CPUs tweak with affinities or priorities,
it may be possible that the running task after the call is changed, but is some other
task from the readytorun list (and it should be, if there is higher priority one available or the
affinity of the added task prevents it to be scheduled in, but the previous head of the readytorun
list should run.

this_task() is always the correct one to switch to, since it always points to the tcb which was
just switched in by the nxsched_add_readytorun.

This is also a precursor to re-writing the SMP queue logic to remove pending lists for SMP.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-08-20 10:16:43 +08:00
raiden00pl
a8e03eefcd sched: unify Private Types banners
unify Private Types banners according to NuttX coding standard

Signed-off-by: raiden00pl <raiden00@railab.me>
2025-05-28 10:17:15 +08:00
wangzhi16
35a62b7d5e Revert "Reduce the size of tcb by four bytes."
This reverts commit 893c5e92c2.
2025-05-27 16:30:35 +08:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
Jukka Laitinen
6ed21ec1af sched: Remove checking of WDOG_ISACTIVE when cancelling wdog
Checking only adds a race condition. The checking if the wdog is active
or not must be done inside wd_cancel, where the proper spinlock is held

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-19 20:34:31 +08:00
Serg Podtynnyi
c5757ed71f sched/timedwait: Ensure critical section for nxsig_timeout
nxsig_timeout calls nxsched_add_readytorun and up_switch_context, so
must be in critical section.
nxsig_timeout is used as wdentry in nxsig_clockwait

See wd_expiration CALL_FUNC is not in critical section.

Signed-off-by: Serg Podtynnyi <serg@podtynnyi.com>
2025-05-15 19:36:17 +08:00
ouyangxiangzhen
64a7049dec clock: Add clock_delay2abstick.
This commit added a macro function clock_delay2abstick to calculate the
absolute tick after the delay.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
3622e4830a sched: Correct word spelling mistakes.
This commit fixed serveral word spelling mistakes.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
8e65375365 sched: Remove unnecessary tick++ for absolute wdog timer.
To improve the absolute timer accuracy, this commit move the tick++ to relative wdog timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
buxiasen
cd7a8f73eb comments: update text typo pre-empted to preempted
Or will be catch by codespell, when do checkpatch.sh

Also fix the relative comment file changed.

include/nuttx/scsi.h
drivers/syslog/ramlog.c
excluded as we have to modify field name in struct

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2025-05-12 15:01:37 +08:00
Jukka Laitinen
e4d5c78008 Fix signal delivered to a wrong thread
When using pthread_kill, the signal should be delivered to the
specified thread. Current implementation, however, may add the
signal to the groups pending list, if the signal is masked at the
time of dispatch. From the group's pending list it can be delivered
to any thread of the group, which is wrong.

Fix this by adding a new field "FAR struct tcb_s *tcb" to
"struct sigpendq", marking if the signal needs to be delivered
to a specific thread. Use NULL for the value if delivery to any
thread in the group is ok.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:35 +08:00
Jukka Laitinen
efc0f400bb sched/signal/sig_dispatch.c: Correct spelling in comments, waitting ==> waiting
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
a0efb1fefe sched/signal: Move dynamic allocation of pending signals to a safe place
The kmm_alloc can break the critical section, if it sleeps on the
heap mutex. If we run out of pending signal structures, allocate more
right after entering the critical section but before checking if the signal
needs to be added to the pending queue.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
77df55db45 sched/signal: Move nxsig_find_action outside of critical section
The TCB used in find_action is locked by spinlock, so it doesn't belong
inside critical section. Just find the possible action already in the
beginning of nxsig_tcbdispatch.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-08 21:32:08 +08:00
Jukka Laitinen
2b513af2c3 sched/signal/sig_dispatch: Fix race conditions between nxsig_tcbdispatch and nxsig_deliver
For example a race in managing tcb->sigprocmask may cause signal not being delivered at all.
The mechanism is as follows:

1. cpu 1: nxsig_deliver adds the signal to the sigprocmask for the duration of signal delivery
2. cpu 2: new signal (same signo) is dispatched in nxsig_tcbdispatch
3. cpu 2: nxsig_tcbdispatch detects that signal is masked
4. cpu 2: nxsig_tcbdispatch leaves critical section before calling nxsig_add_pendigsignal
5. cpu 1: nxsig_deliver finishes. The "nxsig_unmask_pendingsignal" is called in the end but does nothing
6. cpu 2: nxsig_tcbdispatch continues and adds the pending signal

In the end, the logic in the end of nxsig_deliver, which tries to handle signals added
to the pending queue during the signal action delivery (step 5) failed, and the pending signal
was not delivered.

Fix this by just keeping the critical section for during the whole duration of nxsig_tcbdispatch,
and move things which can't be executed from within critical section outside.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-02 09:21:14 -03:00
Ville Juven
c12aa5663d sched/affinity: Fix CPU_LOCKED functionality for some SMP calls
For some SMP calls it is necessary to lock the current CPU for the process
receiving the SMP call. This is done by setting the CPU affinity to the
current CPU and preventing the CPU selection algorithm from switching
CPUs.

  dtcb->flags |= TCB_FLAG_CPU_LOCKED;
  CPU_SET(dtcb->cpu, &dtcb->affinity);

However, this logic is currently broken, as CPU_SET is defined as:

  #define CPU_SET(c,s) do { *(s) |= (1u << (c)); } while (0)

In order to assign tcb->cpu (the current CPU) to the affinity mask, the
mask must be cleared first by calling CPU_ZERO.
2025-04-26 13:56:27 +08:00
Jukka Laitinen
4b14b206db sched: Replace direct semaphore value access with nxsem_get_value
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-07 16:29:57 +08:00
hujun5
57e54b399b sched: remove all spin_lock_irqsave(NULL)
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-06 23:11:44 +08:00
wangzhi16
893c5e92c2 Reduce the size of tcb by four bytes.
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2025-01-02 23:18:42 +08:00
hujun5
88b4aebedc in smp_call handle we only change TCB_FLAG_CPU_LOCKED bit of tcb->flag
reason:
change unrelated changes to tcb->flag fields, may cause error

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-11-16 01:58:59 +08:00
hujun5
c498991ba4 smpcall: add nxsched_smp_call_async and nxsched_smp_call_single_async
reason:
The old implementation of the SMP call, even when using the "no wait" parameter,
could still result in waiting, if invoking it within a critical section
may lead to deadlocks. Therefore, in order to implement a truly asynchronous SMP
call strategy, we have added nxsched_smp_call_async.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-11-06 09:53:28 +08:00
ouyangxiangzhen
17c51c0667 userspace: Exclude nuttx/arch.h
This patch fixed userspace headers conflict. Architecture-related definition and API should not be exposed to users.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-11-01 16:59:37 +08:00