Add DEBUGASSERT in nxsem_wait_slow() to catch illegal mutex
recursion attempts. This helps identify bugs where a task tries
to lock a mutex it already holds, which is not allowed.
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Refactor nxsched_set_scheduler() to eliminate goto statements and consolidate multiple return
paths into a single exit point. Extract sporadic scheduling logic into a separate function and
restructure parameter validation for better code quality and MISRA HIS compliance.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change replaces goto-based control flow with structured if-else blocks
in the up_read() function to comply with MISRA HIS coding standards while
maintaining identical functional behavior.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change optimizes the reader-writer semaphore release process by consolidating
redundant context switch operations and improving code efficiency, reducing
unnecessary wake-ups during concurrent access scenarios.
Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
This change refactors nxsched_set_param() by extracting complex conditional logic
into dedicated helper functions and consolidating multiple return statements into
single exit points to reduce cyclomatic complexity and comply with MISRA HIS coding
standards for safety-critical embedded systems.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Replace CONFIG_SYSTEM_TIME64 conditional atomic64 operations with a unified
seqlock-based approach for managing the system tick counter. This simplifies
code by eliminating architecture-specific branches and provides more robust
thread-safe access patterns.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change enables performance monitoring unit (PMU) access from userspace
on ARMv7-R architecture by adding CONFIG_ARCH_HAVE_PERF_EVENTS_USER_ACCESS
support and building PMU code for userspace when needed.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in the profil() function
into a single exit point by inverting the parameter validation condition and
restructuring error handling to reduce cyclomatic complexity and comply with MISRA HIS.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Fix violations of MISRA C:2012 Rule 10.4 (operand of unsigned and signed)
in cancellation point handling code.
Changed all CANCEL_FLAG_* macro definitions and their usage to use unsigned
literals (1u instead of 1) to ensure consistent unsigned arithmetic when
performing bitwise operations. This eliminates mixed signed/unsigned operand
violations in:
- CANCEL_FLAG_NONCANCELABLE
- CANCEL_FLAG_CANCEL_ASYNC
- CANCEL_FLAG_CANCEL_PENDING
The changes affect cancellation point entry/exit logic, cancellation state
management, and cancellation type handling across both kernel and libc
implementations.
Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
Fix violations of MISRA C:2012 Rule 10.4 (essential type operand of
unsigned and signed) in posix_spawn attribute handling code.
Changed all POSIX_SPAWN_* flag macro definitions to use unsigned literals
(1u instead of 1) to ensure consistent unsigned arithmetic when performing
bitwise operations. This eliminates mixed signed/unsigned operand violations
in the following flags:
- POSIX_SPAWN_RESETIDS
- POSIX_SPAWN_SETPGROUP
- POSIX_SPAWN_SETSCHEDPARAM
- POSIX_SPAWN_SETSCHEDULER
- POSIX_SPAWN_SETSIGDEF
- POSIX_SPAWN_SETSIGMASK
- POSIX_SPAWN_SETSID
Updated all flag checking comparisons in spawn_execattrs() to compare
against 0u instead of 0 for consistency.
Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
This change consolidates multiple return statements in nxsched_get_param()
into a single exit point and restructures the error handling path to reduce
cyclomatic complexity and comply with MISRA HIS coding standards.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit added the guard timer for hrtimer.
The guard timer uses a small memory footprint, offering two main advantages:
- Reduced branches checking for an empty hrtimer queue, simplifying code implementation and improving the performance.
- Additional health monitoring allows the system to enter a safe state in case of time acquisition errors, and supports custom error handling callback functions.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
If there are no timers in the hrtimer queue, we should cancel the timers to avoid unnecessary timer interruptions. Since we currently do not have a timer cancellation interface, we can achieve cancellation by setting the timer to its maximum value.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Determining whether a red-black tree node is the left-mode node using `RB_LEFT(hrtimer, node) == NULL` is functionally incorrect. This is because the left node of any leaf node can also be NULL. For example, in the following rbtree:
5
/ \
3 7
\
4
the left node of the right-most node 7 would also be NULL.
To avoid extra performance overhead to find the left-most node when the
rb-tree changed, this commit used `g_cached_first` to cache the
left-most node.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit fixed the overflow check and renamed the period to delay, since the callback return value is the next delay not the period.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
When a hrtimer is removed and then re-enqueued, if the removed hrtimer is the head node and the re-enqueued node is not the head node, the hardware timer still needs to be reset. This patch fixes this issue and simplifies the enqueuing.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the rbtree in hrtimer and provided better
branchless compare function.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit renamed the hrtimer_is_armed to hrtimer_is_pending, which is
more accurate in the semamtic, and simplify it.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit inlined the `hrtimer_start` to allow the compiler to optimize at least 1 branch in
hrtimer_start.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This change consolidates multiple return statements in down_read_trylock() into
a single exit point and replaces goto with if-else structure to reduce cyclomatic
complexity and comply with MISRA HIS coding standards.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change replaces goto-based control flow with structured if-else blocks
in the down_read() function to comply with MISRA HIS coding standards while
maintaining identical functional behavior.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in init_rwsem() into a
single exit point by inverting error conditions and restructuring nested
if-else blocks for MISRA HIS compliance.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Reduce multiple return statements and simplify control flow by inverting the
condition check and moving all critical monitoring operations into a single
conditional block. This improves code maintainability and addresses the
Coverity HIS_metric_violation (RETURN) defect.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Add explicit type casts to uint32_t for time conversion macro divisors to
comply with MISRA C-2012 Rules 10.1, 10.3, 11.1, 11.3, and 11.4. This fixes
Coverity warnings and improves type safety in clock_time2ticks_floor and related
time conversion operations.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Extract the core waitid logic into a separate waittcb() helper function to reduce the
cyclomatic complexity of the main waitid() function. This improves code maintainability,
reduces nested conditions, and enhances code clarity while preserving all functionality.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Replace all goto statements with structured control flow using while loops and
conditional blocks. Consolidate early returns into a single error-handling path
for better code structure and MISRA HIS standards compliance.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in the sysinfo() function
into a single exit point and inverts the error condition to improve code structure
and comply with MISRA HIS coding standards for safety-critical systems.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in down_write_trylock()
into a single exit point to reduce cyclomatic complexity and comply with MISRA
HIS coding standards for safety-critical embedded systems.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Move the static g_statenames array from file scope to block scope within the
nxsched_get_stateinfo() function. This resolves MISRA C 2012 Rule 8.9 violation
which requires static variables to be used at block scope when only one function accesses them.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in nxsched_set_priority()
into a single exit point to reduce cyclomatic complexity and comply with MISRA
HIS coding standards for safety-critical embedded systems.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Move the static g_cpuload_wdog watchdog timer from file scope to block scope
within the cpuload_init() function. This resolves MISRA C 2012 Rule 8.9 violation
which requires static variables used by a single function to be scoped at block level.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to
allow applications to detect discontinuous changes to CLOCK_REALTIME.
Background:
According to Linux timerfd_create(2) man page, when a timerfd is created
with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the
read() operation should fail with ECANCELED if the real-time clock
undergoes a discontinuous change. This allows applications to detect
and respond to system time changes.
Implementation:
1. Add clock notifier infrastructure (clock_notifier.h/c) to notify
interested parties when system time changes
2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd
3. Register timerfd with clock notifier when flag is set
4. Cancel timer and return ECANCELED when clock change is detected
5. Notify clock changes in:
- clock_settime()
- clock_initialize()
- clock_timekeeping_set_wall_time()
- rpmsg_rtc time synchronization
Changes include:
- New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c
- Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET
- Modified: clock subsystem to call notifier chain on time changes
- Modified: rpmsg_rtc to notify time changes during sync
Use case example:
Applications using timerfd with absolute time can now detect when
system time is adjusted (e.g., NTP sync, manual time change) and
take appropriate action such as recalculating timeouts or updating
scheduled events.
Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
Refactor nxsched_get_scheduler() to consolidate multiple return statements into a single
exit point by inverting the null check condition. This improves code structure and resolves
Coverity HIS_metric_violation defect for better MISRA HIS standards compliance.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Initialize the cpuload_s structure with zero values at declaration to eliminate
uninitialized variable usage. This resolves MISRA C 2012 Rule 9.1 violation and
ensures consistent, predictable system information retrieval behavior.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Refactor nxsem_post_slow() to consolidate multiple return statements into a single exit point
by introducing a result variable and wrapping semaphore operations in an error-check condition block.
This improves code structure and resolves Coverity HIS_metric_violation defect for MISRA HIS standards compliance.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Fix critical issues in SMP scheduler: correct CPU selection logic by
removing redundant lock check and prioritizing eligible CPUs, simplify
ready-to-run list addition by removing unnecessary condition check and
early validation, and relocate switch_running() call for proper cleanup
sequencing in remove_self() operation.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Refactor nxsem_wait_irq() to consolidate multiple return statements into a single
exit point by inverting the mutex condition check. This improves code maintainability
and resolves Coverity HIS_metric_violation defect for better compliance with MISRA HIS standards.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Move static g_prof variable declaration into block scope for the profil()
function when CONFIG_SMP is disabled, following MISRA C 2012 Rule 8.9 which
requires objects used by only one function to be declared within that function's scope.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
This change consolidates multiple return statements in nxsched_get_fdlist_from_tcb()
into a single exit point to reduce cyclomatic complexity and comply with MISRA HIS
coding standards for safety-critical embedded systems.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Previously, IRQ bounds checking (irq >= 0 && irq < NR_IRQS) was duplicated
across multiple IRQ subsystem functions before calling IRQ_TO_NDX(). This
led to code duplication and inconsistency.
This patch consolidates all IRQ bounds checking into the IRQ_TO_NDX() macro
itself, which now returns -EINVAL for out-of-bounds IRQ numbers. This approach:
1. Eliminates duplicated bounds checking code
2. Ensures consistent error handling across all IRQ functions
3. Simplifies caller code - just check if IRQ_TO_NDX() returns negative
4. Makes the macro behavior more predictable and self-contained
Changes:
- Modified IRQ_TO_NDX() to check (irq < 0 || irq >= NR_IRQS) and return -EINVAL
- Removed redundant IRQ range checks in irq_attach(), irq_attach_thread(),
irq_attach_wqueue(), and irqchain_detach()
- Simplified error handling to check ndx < 0 after IRQ_TO_NDX() call
This consolidation reduces code size and improves maintainability while
preserving all existing error checking functionality.
Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
Refactor sem_trywait.c to replace goto statements with structured control
flow to comply with MISRA HIS coding standards. This improves code clarity
and maintainability while preserving all functional behavior and performance
characteristics.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Consolidate CONFIG_PRIORITY_PROTECT checks by moving macro definitions to
their actual usage locations and removing duplicate/dead code in semaphore
implementation. This reduces code duplication and improves maintainability
across sem_wait, sem_trywait, and sem_post functions.
Signed-off-by: p-gaoxiang43 <p-gaoxiang43@xiaomi.com>