Fix-Point opened a new pull request, #17993:
URL: https://github.com/apache/nuttx/pull/17993

   ## Summary
   
   This series of commits refactors and simplifies the tickless scheduler's 
timer expiration code.
   
   The key changes are:
   1.  **Removal of Redundant State (`g_timer_interval`):** The global atomic 
variable `g_timer_interval`, which tracked the currently active timer interval, 
has been eliminated. The next expiration time is now calculated directly from 
the active watchdog list.
   2.  **Centralized Timer Adjustment Logic:** The logic for adjusting the next 
timer tick (handling `CONFIG_SCHED_TICKLESS_LIMIT_MAX_SLEEP` and 
`CONFIG_TIMER_ADJUST_USEC`) has been moved from `sched_timerexpiration.c` into 
the watchdog module as a new inline function `wd_adjust_next_tick()`. This 
simplifies the scheduler's `nxsched_timer_start()` function.
   3.  **Elimination of Redundant Time Reads:** A new global variable 
`g_wdexpired` is introduced to store the tick count at which the watchdog timer 
actually expired. This allows the scheduler event handler 
(`nxsched_process_event`) to use this pre-fetched value, avoiding a redundant 
call to `up_timer_gettick()` during timer expiration processing. The tick is 
only fetched again when reassessing the timer via `nxsched_reassess_timer()`.
   4.  **Simplified Timer Start Abstraction:** Helper functions 
(`up_timer_tick_start`, `up_alarm_tick_start`) are consolidated and the 
`wd_timer_start()` function is significantly simplified by using 
`wd_adjust_next_tick()`.
   5.  **SCHED_RR Workaround:** A workaround is added within 
`nxsched_process_timer()` to trigger the scheduler on every timer expiration 
when `CONFIG_RR_INTERVAL > 0`. This mitigates an existing issue where a 
preempted round-robin task's timeslice counter may not decrement correctly. 
This is temporary until the core SCHED_RR behavior is fixed.
   7.  **Utility Function:** A new function `wd_get_next_expire()` is added to 
provide a safe way to get the next watchdog expiration time relative to the 
current time, returning 0 if it's in the past.
   
   These changes improve code maintainability by reducing state duplication, 
centralizing configuration-dependent logic, and clarifying the flow of time 
information through the tickless scheduler and watchdog subsystem.
   
   ## Impact
   
   *   **Users:** No direct impact on the API. The internal behavior of the 
tickless scheduler is modified, which could theoretically affect timing 
precision and power consumption characteristics, but the functional interface 
remains the same.
   *   **Hardware:** No impact on hardware support. The changes are at the OS 
scheduler level and rely on the existing `up_timer_gettick()`, 
`up_timer_start()`, and `up_alarm_start()` architecture interfaces.
   *   **Documentation:** No documentation changes are required as this is an 
internal refactor.
   *   **Compatibility:** The changes are intended to be fully compatible with 
existing use cases.
   
   ## Testing
   Internal CT passed, tested on `rv-virt:smp` with latest nuttx/master 
code(e143acfc4642e53b5998f2b2dc617be4c0a48c4f) will result in the following 
error. This issue existed before the commits in this PR were introduced, and it 
is not a problem introduced by this PR.
   
   ```bash
   user_main: signal handler test
   sighand_test: Initializing semaphore to 0
   sighand_test: Starting waiter task
   sighand_test: Started waiter_main pid=110
   waiter_main: Waiter started
   waiter_main: Unmasking signal 32
   waiter_main: Registering signal handler
   waiter_main: oact.sigaction=0 oact.sa_flags=20676e69 
oact.sa_mask=7355203a74736574
   waiter_main: Waiting on semaphore
   sighand_test: Signaling pid=110 with signo=32 sigvalue=42
   waiter_main: sem_wait() successfully interrupted by signal
   waiter_main: done
   sighand_test: ERROR signal handler did not run
   [CPU0] Current Version: NuttX  12.12.0 e143acfc464 Jan 19 2026 09:55:52 
risc-v
   [CPU0] Assertion failed (_Bool)0: at file: :0 task(CPU0): ostest process: 
ostest 0x8001416e
   [CPU0] EPC: 80002a4c
   [CPU0] A0: 80030ad0 A1: 00000000 A2: 00000000 A3: 800386e0
   [CPU0] A4: 00000002 A5: 00000000 A6: 00000000 A7: 00000001
   [CPU0] T0: 0000002e T1: 00000000 T2: 000001ff T3: 00000001
   [CPU0] T4: 00000007 T5: 00000001 T6: 0000002a
   [CPU0] S0: 0000018c S1: 80030ad0 S2: 80032000 S3: 800386e0
   [CPU0] S4: 80024c50 S5: 00000000 S6: 80033000 S7: 00002088
   [CPU0] S8: 00000006 S9: 00000000 S10: 00000000 S11: 00000000
   [CPU0] SP: 8003a910 FP: 0000018c TP: 00000000 RA: 80002a4c
   [CPU0] User Stack:
   [CPU0]   base: 0x80038b30
   [CPU0]   size: 00008096
   [CPU0]     sp: 0x8003a910
   [CPU0] 0x8003a8f0: 80033000 8002766c 80024c50 800386e0 80030ad0 80030ad0 
800386e0 80002b0e
   [CPU0] 0x8003a910: 8001416e 00000000 00000000 00000000 00000000 00000000 
00000000 0a000000
   [CPU0] 0x8003a930: 00000000 00000000 800386e0 80030ad0 80024c50 00000000 
00000000 7474754e
   [CPU0] 0x8003a950: 80020058 80025000 80036c98 80036d38 00000001 8003a9d8 
00000000 8000e61a
   [CPU0] 0x8003a970: 8003a9e0 00000000 00000000 00000000 2e323100 302e3231 
80025000 80025000
   [CPU0] 0x8003a990: 00000001 3165a9d8 63613334 36346366 614a2034 3931206e 
32303220 39302036
   [CPU0] 0x8003a9b0: 3a35353a 80003235 00000001 80009176 80025000 00001fa0 
736972d0 00762d63
   [CPU0] 0x8003a9d0: 80027a00 80027a2d 800388d0 80009358 00000000 80025000 
80025000 80025000
   [CPU0] 0x8003a9f0: 80025000 80025000 80025000 80025000 80032000 80032000 
80032000 80007c50
   [CPU0] 0x8003aa10: 00000000 80032000 80032000 80016134 80032000 00000064 
0000006e 00010064
   [CPU0] 0x8003aa30: 00000000 00000000 00002000 00000000 80032000 80025000 
00000400 8001442e
   [CPU0] 0x8003aa50: 01fc95a0 00000001 00000022 01fc5528 00004078 01fc5528 
000041f8 00000000
   [CPU0] 0x8003aa70: 00000000 00000000 00000000 00000000 00000000 00000000 
00000000 00000000
   [CPU0] 0x8003aa90: 00000000 800386e0 8001416e 80007f18 00000000 00000000 
00000005 80038af0
   [CPU0] 0x8003aab0: 00000000 00000000 00000000 80004d88 00000000 00000000 
00000000 00000000
   [CPU0] 0x8003aad0: 00000000 00000000 00000000 00000000 00000000 00000000 
00000000 00000000
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to