rtsched 0.2.0

A no_std runtime scheduler for Cortex-M thread management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 kwangdo.yi

//! Kernel timer queue keyed by ktimer expiration time.
//!
//! Active and inactive timers occupy separate trees with cached minima.
//! Runnable selection reads the active minimum, while timer programming and
//! expiry dispatch compare both minima so inactive releases and wakes still fire.
//!
//! The queue is intrusive: each `KTimerEntity` embeds its own `RbNode`, so
//! inserting ktimers does not allocate.

use core::cell::UnsafeCell;
use core::cmp::Ordering;
use core::mem::offset_of;
use core::ptr;
#[cfg(feature = "host")]
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering as AtomicOrdering};

use crate::arch::platform;
use crate::arch::platform::{SCHEDULER_TIMER_RELOAD_MAX, SCHEDULER_TIMER_RELOAD_MIN};
use crate::critical_section;
use crate::rbtree::{RBTree, RBTreeNode, RbNode};
use crate::runq::enqueue_runq_from_waitq;
use crate::sync::{MessageWaitResult, finish_message_wait};
use crate::thread::{
    RtThread, ThreadCtx, ThreadHandle, ThreadKind, ThreadState, rt_ktimer_entity,
    rt_thread_from_handle, set_rt_ktimer_entity,
};
use crate::waitq::{
    WAIT_QUEUE, WaitQueueError, insert_wait_thread, pop_expired_wait_thread, remove_wait_thread,
};

const KTIMER_EXPIRE_NEVER: u64 = u64::MAX;
static KTIMER_QUEUE: GlobalKTimerQueue = GlobalKTimerQueue::new();
static mut NEXT_KTIMER: *mut KTimerEntity = ptr::null_mut();
pub(crate) static mut CFS_KTIMER: CfsKTimer = CfsKTimer::new(0, 0, "cfs");
pub(crate) static mut WAIT_KTIMER: WaitKTimer = WaitKTimer::new();

#[cfg(feature = "host")]
static TEST_ELAPSED_TICKS_OVERRIDE_SET: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "host")]
static TEST_ELAPSED_TICKS_OVERRIDE: AtomicU32 = AtomicU32::new(0);

struct GlobalKTimerQueue {
    queue: UnsafeCell<KTimerQueue>,
}

impl GlobalKTimerQueue {
    const fn new() -> Self {
        Self {
            queue: UnsafeCell::new(KTimerQueue::new()),
        }
    }

    fn get(&self) -> *mut KTimerQueue {
        self.queue.get()
    }
}

unsafe impl Sync for GlobalKTimerQueue {}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RtTiming {
    /// Time between releases of consecutive jobs, when the next job is released.
    period_ticks: u32,
    /// Relative deadline for each released job, when this job must complete by.
    relative_deadline_ticks: u32,
    /// Maximum runtime budget charged to the current job window.
    /// How much CPU time it is allowed to consume.
    budget_ticks: u32,
}

impl RtTiming {
    pub const fn new(period_ticks: u32, relative_deadline_ticks: u32, budget_ticks: u32) -> Self {
        Self {
            period_ticks,
            relative_deadline_ticks,
            budget_ticks,
        }
    }

    pub const fn from_period(period_ticks: u32) -> Self {
        Self::new(period_ticks, period_ticks, period_ticks)
    }

    pub const fn period_ticks(&self) -> u32 {
        self.period_ticks
    }

    pub const fn relative_deadline_ticks(&self) -> u32 {
        self.relative_deadline_ticks
    }

    pub const fn budget_ticks(&self) -> u32 {
        self.budget_ticks
    }
}

#[repr(C)]
pub(crate) struct KTimerEntity {
    expire_at: u64,
    node: RbNode,
    active: bool,
    timing: RtTiming,
}

impl KTimerEntity {
    #[cfg(feature = "host")]
    pub const fn new(expire_ticks: u32) -> Self {
        Self::new_with_timing(expire_ticks, RtTiming::from_period(expire_ticks))
    }

    pub const fn new_with_timing(expire_ticks: u32, timing: RtTiming) -> Self {
        Self {
            expire_at: expire_ticks as u64,
            node: RbNode::new(),
            active: true,
            timing,
        }
    }

    #[cfg(feature = "host")]
    pub fn expire(&self) -> u32 {
        self.expire_at.min(u64::from(u32::MAX)) as u32
    }

    pub fn set_expire(&mut self, expire: u32) {
        self.expire_at = u64::from(expire);
    }

    pub fn expire_at(&self) -> u64 {
        self.expire_at
    }

    pub fn set_expire_at(&mut self, expire_at: u64) {
        self.expire_at = expire_at;
    }

    pub fn set_expire_after(&mut self, now_ticks: u64, ticks: u32) {
        self.expire_at = now_ticks.saturating_add(u64::from(ticks));
    }

    pub fn set_expire_never(&mut self) {
        self.expire_at = KTIMER_EXPIRE_NEVER;
    }

    pub fn remaining_at(&self, now_ticks: u64) -> u32 {
        if self.expire_at == KTIMER_EXPIRE_NEVER {
            return SCHEDULER_TIMER_RELOAD_MAX;
        }

        self.expire_at
            .saturating_sub(now_ticks)
            .min(u64::from(SCHEDULER_TIMER_RELOAD_MAX)) as u32
    }

    pub fn is_expired_at(&self, now_ticks: u64) -> bool {
        self.expire_at <= now_ticks
    }

    pub fn reset_links(&mut self) {
        self.node.reset_links();
    }

    #[cfg(feature = "host")]
    pub fn is_linked(&self) -> bool {
        self.node.is_linked()
    }

    pub fn is_active(&self) -> bool {
        self.active
    }

    pub fn timing(&self) -> RtTiming {
        self.timing
    }

    pub fn period_ticks(&self) -> u32 {
        self.timing.period_ticks()
    }

    pub fn relative_deadline_ticks(&self) -> u32 {
        self.timing.relative_deadline_ticks()
    }

    pub fn budget_ticks(&self) -> u32 {
        self.timing.budget_ticks()
    }

    /// Recover the owning RT timer from its embedded timer entity.
    ///
    /// # Safety
    ///
    /// `entity` must be non-null and must point to the `entity` field of a
    /// live `RtKTimer`. It must not point to the global CFS timer, the global
    /// wait timer, or any other allocation with a `KTimerEntity` layout.
    pub unsafe fn container_of(entity: *mut Self) -> *mut RtKTimer {
        debug_assert!(!entity.is_null());
        debug_assert!(!is_cfs_ktimer(entity));
        debug_assert!(!is_wait_ktimer(entity));

        (entity as *mut u8)
            .wrapping_sub(offset_of!(RtKTimer, entity))
            .cast::<RtKTimer>()
    }
}

#[repr(C)]
pub(crate) struct CfsKTimer {
    pub entity: KTimerEntity,
    pub name: &'static str,
}

impl CfsKTimer {
    pub const fn new(period_ticks: u32, execution_ticks: u32, name: &'static str) -> Self {
        Self {
            entity: KTimerEntity::new_with_timing(
                period_ticks,
                RtTiming::new(period_ticks, execution_ticks, execution_ticks),
            ),
            name,
        }
    }

    pub fn entity_mut(&mut self) -> *mut KTimerEntity {
        ptr::addr_of_mut!(self.entity)
    }

    #[cfg(feature = "host")]
    pub fn execution_ticks(&self) -> u32 {
        self.entity.relative_deadline_ticks()
    }

    #[cfg(feature = "host")]
    pub fn period_ticks(&self) -> u32 {
        self.entity.period_ticks()
    }
}

#[repr(C)]
pub(crate) struct WaitKTimer {
    pub(crate) entity: KTimerEntity,
    pub(crate) name: &'static str,
}

impl WaitKTimer {
    pub const fn new() -> Self {
        Self {
            entity: KTimerEntity {
                expire_at: KTIMER_EXPIRE_NEVER,
                node: RbNode::new(),
                active: false,
                timing: RtTiming::new(0, 0, 0),
            },
            name: "wait",
        }
    }

    pub fn entity_mut(&mut self) -> *mut KTimerEntity {
        ptr::addr_of_mut!(self.entity)
    }
}

#[repr(C)]
pub struct RtKTimer {
    pub(crate) entity: KTimerEntity,
    pub name: &'static str,
    pub miss_cnt: u32,
    thread_ctx: *mut ThreadCtx,
}

impl RtKTimer {
    pub const fn new(period_ticks: u32, thread_ctx: *mut ThreadCtx, name: &'static str) -> Self {
        Self::new_with_timing(RtTiming::from_period(period_ticks), thread_ctx, name)
    }

    pub const fn new_with_timing(
        timing: RtTiming,
        thread_ctx: *mut ThreadCtx,
        name: &'static str,
    ) -> Self {
        Self {
            entity: KTimerEntity::new_with_timing(timing.relative_deadline_ticks(), timing),
            name,
            miss_cnt: 0,
            thread_ctx,
        }
    }

    pub(crate) fn entity_mut(&mut self) -> *mut KTimerEntity {
        ptr::addr_of_mut!(self.entity)
    }

    pub(crate) fn thread_ctx(&self) -> *mut ThreadCtx {
        self.thread_ctx
    }

    pub fn timing(&self) -> RtTiming {
        self.entity.timing()
    }

    pub fn period_ticks(&self) -> u32 {
        self.entity.period_ticks()
    }

    pub fn relative_deadline_ticks(&self) -> u32 {
        self.entity.relative_deadline_ticks()
    }

    pub fn budget_ticks(&self) -> u32 {
        self.entity.budget_ticks()
    }

    pub(crate) fn init_rt_ktimer(&mut self, thread_ctx: *mut ThreadCtx) {
        self.thread_ctx = thread_ctx;
        if !thread_ctx.is_null() {
            unsafe {
                set_rt_ktimer_entity(ThreadHandle::from_thread_ctx(thread_ctx), self.entity_mut());
            }
        }
    }
}

/// Convert a raw tick interval into a scheduler timer reload register value.
///
/// scheduler timer stores a reload register value. A reload
/// value of `R` wraps after `R + 1` ticks, so an interval of `N` ticks must be
/// represented as `N - 1`. This helper returns the raw conversion and allows
/// reload `0`; scheduler programming raises the reload value `0` to
/// `SCHEDULER_TIMER_RELOAD_MIN` before writing the hardware register.
pub fn reload_from_ticks(ticks: u32) -> Option<u32> {
    ticks
        .checked_sub(1)
        .filter(|&reload| reload <= SCHEDULER_TIMER_RELOAD_MAX)
}

/// Initialize the global ktimer and wait-timer state.
///
/// # Safety
///
/// Call this during single-threaded scheduler setup, before interrupts or
/// threads can access the ktimer or wait queues. `init_cfs` and RT thread
/// spawning must happen after this initialization.
///
/// Do not call this while timer entities or wait entities are queued, running,
/// or otherwise visible to the scheduler. Reinitializing with live entities
/// invalidates their intrusive links and loses the current timer expiration.
pub unsafe fn init_ktimer_queue() {
    critical_section(|| unsafe {
        ptr::write(KTIMER_QUEUE.get(), KTimerQueue::new());
        ptr::write(&raw mut NEXT_KTIMER, ptr::null_mut());
        ptr::write(&raw mut WAIT_KTIMER, WaitKTimer::new());
        (*KTIMER_QUEUE.get()).enqueue_inactive((*ptr::addr_of_mut!(WAIT_KTIMER)).entity_mut());
    });
}

pub(crate) unsafe fn enqueue_ktimer(entity: *mut KTimerEntity) {
    critical_section(|| unsafe {
        let queue = &mut *KTIMER_QUEUE.get();
        debug_assert!(
            !queue.contains(entity.cast_const()),
            "ktimer entity is already queued"
        );
        (*entity).reset_links();
        queue.enqueue_active(entity);
        refresh_next_ktimer(queue);
    });
}

unsafe fn update_wait_ktimer_deadline(wait_ktimer_entity: *mut KTimerEntity) {
    unsafe {
        let wait_entity = (*WAIT_QUEUE.get()).first();
        if wait_entity.is_null() {
            (*wait_ktimer_entity).set_expire_never();
        } else {
            (*wait_ktimer_entity).set_expire_at((*wait_entity).wake_at);
        }
    }
}

unsafe fn record_rt_budget_overrun(entity: *mut KTimerEntity, rt_thread: *mut RtThread) {
    unsafe {
        if (*rt_thread).runtime > (*entity).budget_ticks() {
            crate::rtsched_println!(
                "Budget overrun in thread '{}': runtime {} ticks exceeded budget {} ticks",
                (*rt_thread).thread.name,
                (*rt_thread).runtime,
                (*entity).budget_ticks()
            );
            let rt_ktimer = KTimerEntity::container_of(entity);
            (*rt_ktimer).miss_cnt = (*rt_ktimer).miss_cnt.saturating_add(1);
        }
    }
}

unsafe fn is_real_rt_deadline_miss(entity: *mut KTimerEntity, rt_thread: *mut RtThread) -> bool {
    unsafe { (*rt_thread).runtime > (*entity).relative_deadline_ticks() }
}

pub(crate) unsafe fn program_wait_ktimer() {
    critical_section(|| unsafe {
        let queue = &mut *KTIMER_QUEUE.get();
        // Keep the linked node's raw pointers valid across the unchanged-deadline path.
        let wait_ktimer_entity = ptr::addr_of_mut!(WAIT_KTIMER.entity);

        let first_wait = (*WAIT_QUEUE.get()).first();
        let expire_at = if first_wait.is_null() {
            KTIMER_EXPIRE_NEVER
        } else {
            (*first_wait).wake_at
        };
        if (*wait_ktimer_entity).expire_at() != expire_at {
            queue.remove(wait_ktimer_entity);
            (*wait_ktimer_entity).set_expire_at(expire_at);
            queue.enqueue_inactive(wait_ktimer_entity);
        }
        // A wake can change the selected RT timer even when the wait deadline stays put.
        refresh_next_ktimer(queue);
    });
}

pub(crate) unsafe fn wake_wait_thread(queue: &mut KTimerQueue, elapsed: u32) {
    unsafe {
        loop {
            let Some(wait_thread) = pop_expired_wait_thread(queue.now_ticks()) else {
                break;
            };
            let wait_thread_ptr = wait_thread.as_ptr();

            finish_message_wait(wait_thread, MessageWaitResult::TimedOut);
            (*wait_thread_ptr).set_state(ThreadState::ReadyActive);
            crate::diagnostics::trace::record_wakeup(wait_thread_ptr);
            if (*wait_thread_ptr).is_cfs() {
                enqueue_runq_from_waitq(wait_thread);
            } else if (*wait_thread_ptr).is_rt() {
                let ktimer_entity = rt_ktimer_entity(wait_thread);
                let rt_thread = rt_thread_from_handle(wait_thread);

                (*rt_thread).runtime = (*rt_thread).runtime.saturating_add(elapsed);
                record_rt_budget_overrun(ktimer_entity, rt_thread);

                (*ktimer_entity).set_expire_after(
                    queue.now_ticks(),
                    (*ktimer_entity)
                        .relative_deadline_ticks()
                        .saturating_sub((*rt_thread).runtime),
                );
                (*ktimer_entity).reset_links();
                queue.enqueue_active(ktimer_entity);
            }
        }
    }
}

unsafe fn remove_ktimer(entity: *mut KTimerEntity) -> *mut KTimerEntity {
    critical_section(|| unsafe {
        if entity.is_null() {
            return ptr::null_mut();
        }

        let queue = &mut *KTIMER_QUEUE.get();
        let removed = queue.dequeue(entity);
        if NEXT_KTIMER == removed {
            refresh_next_ktimer(queue);
        }
        removed
    })
}

unsafe fn reinsert_ktimer(entity: *mut KTimerEntity) {
    critical_section(|| unsafe {
        if entity.is_null() {
            return;
        }

        let queue = &mut *KTIMER_QUEUE.get();
        debug_assert!(
            !queue.contains(entity.cast_const()),
            "ktimer entity is already queued"
        );
        (*entity).reset_links();
        queue.enqueue_active(entity);

        refresh_next_ktimer(queue);
    });
}

unsafe fn refresh_next_ktimer(queue: &mut KTimerQueue) {
    unsafe {
        let mut entity = queue.first_active();

        if !entity.is_null() && (*entity).is_expired_at(queue.now_ticks()) {
            queue.remove(entity);
            if is_cfs_ktimer(entity) {
                (*entity).set_expire_after(queue.now_ticks(), (*entity).period_ticks());
            } else {
                let rt_thread_ctx = (*KTimerEntity::container_of(entity)).thread_ctx();
                let rt_thread = rt_thread_from_handle(ThreadHandle::from_thread_ctx(rt_thread_ctx));

                if is_real_rt_deadline_miss(entity, rt_thread) {
                    crate::diagnostics::ktimer::record_rt_deadline_miss(queue, entity, rt_thread);
                }
                (*rt_thread).runtime = 0;
                (*entity).set_expire_after(queue.now_ticks(), (*entity).relative_deadline_ticks());
            }
            queue.enqueue_active(entity);

            entity = queue.first_active();
        }

        NEXT_KTIMER = entity;
    }
}

pub(crate) fn dequeue_ktimerq_to_waitq(thread: ThreadHandle) -> Result<(), WaitQueueError> {
    critical_section(|| unsafe {
        let thread_ptr = thread.as_ptr();
        if !(*thread_ptr).is_rt() {
            return Err(WaitQueueError::NotFound);
        }

        let ktimer_entity = rt_ktimer_entity(thread);
        if ktimer_entity.is_null() {
            return Err(WaitQueueError::NotFound);
        }

        remove_ktimer(ktimer_entity);
        insert_wait_thread(thread);
        program_wait_ktimer();

        Ok(())
    })
}

pub(crate) fn enqueue_ktimerq_from_waitq(thread: ThreadHandle) -> Result<(), WaitQueueError> {
    critical_section(|| unsafe {
        let thread_ptr = thread.as_ptr();
        if !(*thread_ptr).is_rt() {
            return Err(WaitQueueError::NotFound);
        }

        let ktimer_entity = rt_ktimer_entity(thread);
        if ktimer_entity.is_null() {
            return Err(WaitQueueError::NotFound);
        }

        remove_wait_thread(thread);

        (*thread_ptr).set_state(ThreadState::ReadyActive);
        crate::diagnostics::trace::record_wakeup(thread_ptr);
        reinsert_ktimer(ktimer_entity);
        program_wait_ktimer();

        Ok(())
    })
}

pub fn next_ktimer_reload() -> Option<u32> {
    critical_section(|| unsafe {
        let queue = &*KTIMER_QUEUE.get();
        scheduler_timer_reload_for_entity(queue, queue.first())
    })
}

pub(crate) fn elapsed_ticks_since_last_interrupt() -> u32 {
    #[cfg(feature = "host")]
    {
        if TEST_ELAPSED_TICKS_OVERRIDE_SET.load(AtomicOrdering::Relaxed) {
            return TEST_ELAPSED_TICKS_OVERRIDE.load(AtomicOrdering::Relaxed);
        }
    }

    scheduler_timer_reload()
        .or_else(next_ktimer_reload)
        .map(|reload| reload.saturating_add(1))
        .unwrap_or_default()
}

#[cfg(feature = "host")]
pub(crate) fn set_elapsed_ticks_since_last_interrupt_for_test(elapsed: u32) {
    TEST_ELAPSED_TICKS_OVERRIDE.store(elapsed, AtomicOrdering::Relaxed);
    TEST_ELAPSED_TICKS_OVERRIDE_SET.store(true, AtomicOrdering::Relaxed);
}

#[cfg(feature = "host")]
pub(crate) fn clear_elapsed_ticks_since_last_interrupt_for_test() {
    TEST_ELAPSED_TICKS_OVERRIDE_SET.store(false, AtomicOrdering::Relaxed);
    TEST_ELAPSED_TICKS_OVERRIDE.store(0, AtomicOrdering::Relaxed);
}

pub(crate) fn elapsed_ticks_since_current_reload() -> u32 {
    critical_section(|| unsafe {
        let queue = &*KTIMER_QUEUE.get();
        queue
            .time_with_elapsed(elapsed_ticks_from_platform_timer())
            .saturating_sub(queue.now_ticks()) as u32
    })
}

/// Current scheduler time, including ticks since the last timer reprogramming.
pub(crate) fn current_time_ticks() -> u64 {
    critical_section(|| unsafe {
        (*KTIMER_QUEUE.get()).time_with_elapsed(elapsed_ticks_from_platform_timer())
    })
}

fn scheduler_timer_reload() -> Option<u32> {
    platform::scheduler_timer_reload()
}

fn elapsed_ticks_from_platform_timer() -> u32 {
    match (
        platform::scheduler_timer_reload(),
        platform::scheduler_timer_current(),
    ) {
        (Some(reload), Some(current)) => reload.saturating_sub(current),
        _ => 0,
    }
}

pub(crate) unsafe fn advance_ktimers(elapsed: u32) {
    unsafe {
        (*KTIMER_QUEUE.get()).advance_timer_interval(elapsed);
    }
}

pub(crate) unsafe fn dispatch_expired_ktimer(elapsed: u32) -> *mut KTimerEntity {
    unsafe { (*KTIMER_QUEUE.get()).dispatch_expired(elapsed) }
}

pub(crate) unsafe fn update_next_ktimer(entity: *mut KTimerEntity) {
    critical_section(|| unsafe {
        NEXT_KTIMER = entity;
    });
}

unsafe fn thread_scheduler_ktimer(thread: ThreadHandle) -> *mut KTimerEntity {
    unsafe {
        match (*thread.as_ptr()).kind {
            ThreadKind::Cfs => ptr::addr_of_mut!(CFS_KTIMER.entity),
            ThreadKind::Rt => rt_ktimer_entity(thread),
            ThreadKind::Idle => ptr::null_mut(),
        }
    }
}

pub(crate) unsafe fn thread_scheduler_expire_at(thread: ThreadHandle) -> u64 {
    critical_section(|| unsafe {
        let entity = thread_scheduler_ktimer(thread);
        if entity.is_null() {
            KTIMER_EXPIRE_NEVER
        } else {
            (*entity).expire_at()
        }
    })
}

pub(crate) unsafe fn earliest_queued_scheduler_expire_at() -> u64 {
    critical_section(|| unsafe {
        let queue = &*KTIMER_QUEUE.get();
        let mut entity = queue.first();

        while !entity.is_null() {
            if !is_wait_ktimer(entity) {
                return (*entity).expire_at();
            }
            entity = queue.next(entity);
        }

        KTIMER_EXPIRE_NEVER
    })
}

pub(crate) unsafe fn set_thread_scheduler_expire_at(thread: ThreadHandle, expire_at: u64) {
    critical_section(|| unsafe {
        let entity = thread_scheduler_ktimer(thread);
        if entity.is_null() {
            return;
        }

        let queue = &mut *KTIMER_QUEUE.get();
        let was_queued = queue.contains(entity.cast_const());
        let was_active = (*entity).is_active();

        if was_queued {
            queue.remove(entity);
        }

        (*entity).set_expire_at(expire_at);

        if was_queued {
            (*entity).reset_links();
            if was_active {
                queue.enqueue_active(entity);
            } else {
                queue.enqueue_inactive(entity);
            }
            refresh_next_ktimer(queue);
        } else if NEXT_KTIMER == entity {
            refresh_next_ktimer(queue);
        }
    });
}

/// Return whether the named kernel timer is currently active.
///
/// Returns `false` when no timer with the given name exists.
pub fn is_active_ktimer(name: &str) -> bool {
    critical_section(|| unsafe {
        let queue = &*KTIMER_QUEUE.get();
        let mut entity = queue.first();

        while !entity.is_null() {
            if ktimer_name(entity) == name {
                return (*entity).is_active();
            }
            entity = queue.next(entity);
        }

        false
    })
}

pub(crate) fn next_ktimer() -> *mut KTimerEntity {
    critical_section(|| unsafe { NEXT_KTIMER })
}

pub(crate) fn with_ktimer_queue<R>(f: impl FnOnce(&KTimerQueue) -> R) -> R {
    critical_section(|| unsafe { f(&*KTIMER_QUEUE.get()) })
}

pub(crate) fn is_cfs_ktimer(entity: *const KTimerEntity) -> bool {
    !entity.is_null() && entity == unsafe { ptr::addr_of_mut!(CFS_KTIMER.entity).cast_const() }
}

pub(crate) fn is_wait_ktimer(entity: *const KTimerEntity) -> bool {
    !entity.is_null() && entity == unsafe { ptr::addr_of_mut!(WAIT_KTIMER.entity).cast_const() }
}

pub(crate) unsafe fn ktimer_name(entity: *const KTimerEntity) -> &'static str {
    unsafe {
        if is_cfs_ktimer(entity) {
            (*ptr::addr_of_mut!(CFS_KTIMER)).name
        } else if is_wait_ktimer(entity) {
            (*ptr::addr_of_mut!(WAIT_KTIMER)).name
        } else {
            (*KTimerEntity::container_of(entity.cast_mut())).name
        }
    }
}

pub(crate) unsafe fn yield_ktimer(
    entity: *mut KTimerEntity,
    elapsed: u32,
    reset_runtime: bool,
) -> *mut KTimerEntity {
    critical_section(|| unsafe {
        if entity.is_null() {
            return ptr::null_mut();
        }

        let queue: &mut KTimerQueue = &mut *KTIMER_QUEUE.get();

        queue.remove(entity);

        if is_cfs_ktimer(entity) {
            (*entity).set_expire_after(
                queue.now_ticks().saturating_add(u64::from(elapsed)),
                (*entity).period_ticks().saturating_sub(elapsed),
            );
        } else {
            let current_rt_thread_ctx = (*KTimerEntity::container_of(entity)).thread_ctx();
            let current_rt_thread =
                rt_thread_from_handle(ThreadHandle::from_thread_ctx(current_rt_thread_ctx));

            (*current_rt_thread).runtime = (*current_rt_thread).runtime.saturating_add(elapsed);
            record_rt_budget_overrun(entity, current_rt_thread);

            (*entity).set_expire_after(
                queue.now_ticks().saturating_add(u64::from(elapsed)),
                (*entity)
                    .period_ticks()
                    .saturating_sub((*current_rt_thread).runtime),
            );
            if reset_runtime {
                (*current_rt_thread).runtime = 0;
            }
            (*current_rt_thread_ctx).set_state(ThreadState::ReadyInactive);
        }

        queue.advance_time(elapsed);
        queue.enqueue_inactive(entity);
        queue.first_active()
    })
}

fn writable_reload(reload: u32) -> u32 {
    reload.clamp(SCHEDULER_TIMER_RELOAD_MIN, SCHEDULER_TIMER_RELOAD_MAX)
}

fn programmable_reload_from_ticks(ticks: u32) -> u32 {
    let reload = reload_from_ticks(ticks).unwrap_or(if ticks == 0 {
        0
    } else {
        SCHEDULER_TIMER_RELOAD_MAX
    });
    writable_reload(reload)
}

unsafe fn scheduler_timer_reload_for_entity(
    queue: &KTimerQueue,
    entity: *mut KTimerEntity,
) -> Option<u32> {
    if entity.is_null() {
        return None;
    }

    unsafe {
        let reload = if is_cfs_ktimer(entity) {
            let ticks = (*entity)
                .remaining_at(queue.now_ticks())
                .min((*entity).relative_deadline_ticks());
            programmable_reload_from_ticks(ticks)
        } else {
            let raw_reload = (*entity)
                .expire_at
                .saturating_sub(queue.now_ticks())
                .saturating_sub(1)
                .min(u64::from(SCHEDULER_TIMER_RELOAD_MAX)) as u32;
            writable_reload(raw_reload)
        };

        Some(reload)
    }
}

pub(crate) fn program_next_scheduler_timer() -> Option<u32> {
    critical_section(|| unsafe {
        let queue = &mut *KTIMER_QUEUE.get();
        // Account time spent between a cooperative yield and timer programming
        // without counting the already charged part of this interval twice.
        queue.now_ticks = queue.time_with_elapsed(elapsed_ticks_from_platform_timer());
        let reload = scheduler_timer_reload_for_entity(queue, queue.first())?;

        debug_assert!((SCHEDULER_TIMER_RELOAD_MIN..=SCHEDULER_TIMER_RELOAD_MAX).contains(&reload));
        if platform::program_scheduler_timer_reload(reload) {
            queue.timer_epoch_ticks = queue.now_ticks;
        }

        Some(reload)
    })
}

unsafe impl RBTreeNode for KTimerEntity {
    fn node(entity: *mut Self) -> *mut RbNode {
        if entity.is_null() {
            ptr::null_mut()
        } else {
            unsafe { ptr::addr_of_mut!((*entity).node) }
        }
    }

    fn entity_of(node: *mut RbNode) -> *mut Self {
        if node.is_null() {
            ptr::null_mut()
        } else {
            unsafe {
                (node as *mut u8)
                    .sub(offset_of!(KTimerEntity, node))
                    .cast::<KTimerEntity>()
            }
        }
    }

    fn entity_of_const(node: *const RbNode) -> *const Self {
        if node.is_null() {
            ptr::null()
        } else {
            unsafe {
                (node as *const u8)
                    .sub(offset_of!(KTimerEntity, node))
                    .cast::<KTimerEntity>()
            }
        }
    }

    unsafe fn cmp(a: *const Self, b: *const Self) -> core::cmp::Ordering {
        unsafe {
            match (*a).expire_at.cmp(&(*b).expire_at) {
                core::cmp::Ordering::Equal => (a as usize).cmp(&(b as usize)),
                other => other,
            }
        }
    }
}

pub struct KTimerQueue {
    active_tree: RBTree<KTimerEntity>,
    inactive_tree: RBTree<KTimerEntity>,
    first_active: *mut RbNode,
    first_inactive: *mut RbNode,
    now_ticks: u64,
    // Reset to now_ticks when the scheduler timer is reprogrammed - yieldyi().
    // This allows the scheduler to account for elapsed time since
    // the last timer programming.
    timer_epoch_ticks: u64,
}

impl KTimerQueue {
    pub const fn new() -> Self {
        Self {
            active_tree: RBTree::new(),
            inactive_tree: RBTree::new(),
            first_active: ptr::null_mut(),
            first_inactive: ptr::null_mut(),
            now_ticks: 0,
            timer_epoch_ticks: 0,
        }
    }

    // The hardware counter can keep running after a software yield has already
    // advanced now_ticks. Its elapsed value is relative to timer_epoch_ticks,
    // not the newer now_ticks; adding it to now_ticks would expire waits early.
    fn time_with_elapsed(&self, elapsed: u32) -> u64 {
        self.now_ticks
            .max(self.timer_epoch_ticks.saturating_add(u64::from(elapsed)))
    }

    fn advance_timer_interval(&mut self, elapsed: u32) {
        self.now_ticks = self.time_with_elapsed(elapsed);
        self.timer_epoch_ticks = self.now_ticks;
    }

    #[cfg(feature = "host")]
    pub fn is_empty(&self) -> bool {
        self.active_tree.is_empty() && self.inactive_tree.is_empty()
    }

    pub fn len(&self) -> usize {
        self.active_tree.len() + self.inactive_tree.len()
    }

    pub fn first(&self) -> *mut KTimerEntity {
        Self::earlier_of(
            self.first_active(),
            <KTimerEntity as RBTreeNode>::entity_of(self.first_inactive),
        )
    }

    pub fn first_active(&self) -> *mut KTimerEntity {
        <KTimerEntity as RBTreeNode>::entity_of(self.first_active)
    }

    #[cfg(feature = "host")]
    pub fn last(&self) -> *mut KTimerEntity {
        let active = self.active_tree.last();
        let inactive = self.inactive_tree.last();
        if active.is_null() {
            inactive
        } else if inactive.is_null()
            || unsafe { <KTimerEntity as RBTreeNode>::cmp(active, inactive) } == Ordering::Greater
        {
            active
        } else {
            inactive
        }
    }

    /// Return the next timer in combined expiration/address order.
    pub fn next(&self, entity: *mut KTimerEntity) -> *mut KTimerEntity {
        if entity.is_null() {
            return ptr::null_mut();
        }

        unsafe {
            let (own_tree, other_tree) = if (*entity).is_active() {
                (&self.active_tree, &self.inactive_tree)
            } else {
                (&self.inactive_tree, &self.active_tree)
            };
            Self::earlier_of(own_tree.next(entity), other_tree.upper_bound(entity))
        }
    }

    pub fn contains(&self, entity: *const KTimerEntity) -> bool {
        self.active_tree.contains(entity) || self.inactive_tree.contains(entity)
    }

    pub fn now_ticks(&self) -> u64 {
        self.now_ticks
    }

    #[cfg(feature = "host")]
    pub fn next_deadline(&self) -> Option<u32> {
        let first = self.first();
        if first.is_null() {
            None
        } else {
            Some(unsafe { (*first).remaining_at(self.now_ticks) })
        }
    }

    #[cfg(feature = "host")]
    pub fn next_reload(&self) -> Option<u32> {
        unsafe { scheduler_timer_reload_for_entity(self, self.first()) }
    }

    pub fn advance_time(&mut self, elapsed: u32) {
        self.now_ticks = self.now_ticks.saturating_add(u64::from(elapsed));
    }

    /// Dispatch all timers that expired at the queue's current time.
    ///
    /// # Safety
    ///
    /// The queue must contain only valid timer entities whose backing storage
    /// outlives the queue entry. Any RT timer entity in the queue must refer to
    /// a live `RtThread`, and the global wait queue must not be concurrently
    /// mutated while expired wait timers are processed.
    ///
    /// `elapsed` must be the elapsed tick count for the scheduler interval
    /// being dispatched. Callers must hold exclusive access to this queue and
    /// serialize dispatch against scheduler interrupts and other queue
    /// mutations.
    pub unsafe fn dispatch_expired(&mut self, elapsed: u32) -> *mut KTimerEntity {
        unsafe {
            loop {
                let expired = self.first();
                let first_active = self.first_active();
                if expired.is_null() || !(*expired).is_expired_at(self.now_ticks) {
                    return if first_active.is_null() {
                        ptr::null_mut()
                    } else {
                        first_active
                    };
                }

                self.remove(expired);

                if is_wait_ktimer(expired) {
                    wake_wait_thread(self, elapsed);
                    update_wait_ktimer_deadline(expired);
                    self.enqueue_inactive(expired);
                } else if is_cfs_ktimer(expired) {
                    if (*expired).is_active() {
                        (*expired).set_expire_after(
                            self.now_ticks,
                            (*expired).period_ticks().saturating_sub(elapsed),
                        );
                        self.enqueue_inactive(expired);
                    } else {
                        (*expired).set_expire_after(self.now_ticks, (*expired).period_ticks());
                        self.enqueue_active(expired);
                    }
                } else {
                    let thread_ctx = (*KTimerEntity::container_of(expired)).thread_ctx();
                    let rt_thread =
                        rt_thread_from_handle(ThreadHandle::from_thread_ctx(thread_ctx));
                    if (*expired).is_active() && is_real_rt_deadline_miss(expired, rt_thread) {
                        crate::diagnostics::ktimer::record_rt_deadline_miss(
                            self, expired, rt_thread,
                        );
                    }
                    (*rt_thread).runtime = 0;
                    (*expired)
                        .set_expire_after(self.now_ticks, (*expired).relative_deadline_ticks());
                    if !(*expired).is_active() {
                        (*thread_ctx).set_state(ThreadState::ReadyActive);
                    }
                    self.enqueue_active(expired);
                }
            }
        }
    }

    /// Enqueue a detached timer as eligible for scheduler selection.
    ///
    /// # Safety
    ///
    /// `entity` must be non-null, valid for mutation, and backed by storage
    /// that outlives its queue membership. It must not already be linked into
    /// this or any other timer queue. Callers must hold exclusive access to the
    /// queue and serialize insertion against scheduler interrupts and other
    /// queue mutations. Its expiration must remain unchanged until it is removed.
    pub unsafe fn enqueue_active(&mut self, entity: *mut KTimerEntity) {
        unsafe { self.insert(entity, true) };
    }

    /// Enqueue a detached timer as an inactive release or wakeup event.
    ///
    /// # Safety
    ///
    /// `entity` must be non-null, valid for mutation, and backed by storage
    /// that outlives its queue membership. It must not already be linked into
    /// this or any other timer queue. Callers must hold exclusive access to the
    /// queue and serialize insertion against scheduler interrupts and other
    /// queue mutations. Its expiration must remain unchanged until it is removed.
    pub unsafe fn enqueue_inactive(&mut self, entity: *mut KTimerEntity) {
        unsafe { self.insert(entity, false) };
    }

    unsafe fn insert(&mut self, entity: *mut KTimerEntity, active: bool) {
        unsafe {
            debug_assert!(
                !self.contains(entity.cast_const()),
                "entity is already linked into this tree"
            );
            let (tree, left_most) = if active {
                (&mut self.active_tree, &mut self.first_active)
            } else {
                (&mut self.inactive_tree, &mut self.first_inactive)
            };
            tree.insert(entity);
            (*entity).active = active;
            if left_most.is_null()
                || <KTimerEntity as RBTreeNode>::cmp(
                    entity.cast_const(),
                    <KTimerEntity as RBTreeNode>::entity_of_const(*left_most),
                ) == Ordering::Less
            {
                *left_most = <KTimerEntity as RBTreeNode>::node(entity);
            }
        }
    }

    /// Temporarily remove a timer, preserving its previous active state for
    /// expiry processing and deadline updates. Use `dequeue` when blocking it.
    ///
    /// # Safety
    ///
    /// `entity` must be non-null and currently linked into this queue. Callers
    /// must hold exclusive access to the queue and serialize removal against
    /// scheduler interrupts and other queue mutations. Its active state and
    /// expiration must still match the values used when it was inserted.
    pub unsafe fn remove(&mut self, entity: *mut KTimerEntity) -> *mut KTimerEntity {
        if entity.is_null() {
            return ptr::null_mut();
        }

        unsafe {
            let (tree, left_most) = if (*entity).is_active() {
                (&mut self.active_tree, &mut self.first_active)
            } else {
                (&mut self.inactive_tree, &mut self.first_inactive)
            };
            let removing_left_most = ptr::eq(
                <KTimerEntity as RBTreeNode>::node(entity).cast_const(),
                (*left_most).cast_const(),
            );
            let next_left_most = if removing_left_most {
                <KTimerEntity as RBTreeNode>::node(tree.next(entity))
            } else {
                ptr::null_mut()
            };

            let removed = tree.remove(entity);
            if removing_left_most {
                *left_most = next_left_most;
            }

            removed
        }
    }

    /// Remove a timer and clear its scheduling eligibility, for example when
    /// its thread blocks. A null timer is returned unchanged.
    ///
    /// # Safety
    ///
    /// A non-null `entity` must satisfy the requirements of `Self::remove`.
    pub unsafe fn dequeue(&mut self, entity: *mut KTimerEntity) -> *mut KTimerEntity {
        unsafe {
            let removed = self.remove(entity);
            if !removed.is_null() {
                (*removed).active = false;
            }
            removed
        }
    }

    /// Remove and return the earliest ktimer entity in the queue.
    ///
    /// # Safety
    ///
    /// Every entity currently linked in the queue must still be valid for
    /// mutation and backed by storage that outlives the returned borrow.
    /// Callers must hold exclusive access to the queue and serialize removal
    /// against scheduler interrupts and other queue mutations.
    #[cfg(feature = "host")]
    pub unsafe fn pop_first(&mut self) -> Option<&mut KTimerEntity> {
        let first = self.first();
        if first.is_null() {
            return None;
        }

        unsafe {
            self.remove(first);
            Some(&mut *first)
        }
    }

    fn earlier_of(a: *mut KTimerEntity, b: *mut KTimerEntity) -> *mut KTimerEntity {
        if a.is_null() {
            return b;
        }
        if b.is_null() || unsafe { <KTimerEntity as RBTreeNode>::cmp(a, b) } == Ordering::Less {
            a
        } else {
            b
        }
    }
}

impl Default for KTimerQueue {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "host")]
#[path = "../tests/support/ktimer.rs"]
pub mod test_support;