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
//! The low-level kernel interface to be implemented by a kernel implementor.
//!
//! # Safety
//!
//! Most traits in this method are `unsafe trait` because they have to be
//! trustworthy to be able to build sound memory-safety-critical abstractions on
//! top of them.
//!
//! The trait methods that operate on a given [`Id`] are all defined as `unsafe
//! fn` as the caller is responsible for maintaining [object safety][1].
//!
//! [1]: crate#object-safety
use core::{fmt, hash::Hash, ops::Range};

use crate::{
    kernel::error::*,
    time::{Duration, Time},
};

/// A group of traits that must be implemented by kernel object ID types,
/// including [`KernelBase::RawTaskId`].
pub trait Id: fmt::Debug + Copy + Eq + Ord + Hash + Send + Sync + 'static {}
impl<T: ?Sized + fmt::Debug + Copy + Eq + Ord + Hash + Send + Sync + 'static> Id for T {}

/// Provides access to the minimal API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelBase: fmt::Debug + Copy + Sized + 'static {
    type RawDebugPrinter: fmt::Debug + Send + Sync;

    /// The type to identify tasks.
    type RawTaskId: Id;

    /// Used by [`QueueOrder::is_supported`].
    ///
    /// `None` elements don't match any values of `QueueOrder`. This might be
    /// useful for conditionally enabling some of them.
    ///
    /// The default value is an empty slice.
    const RAW_SUPPORTED_QUEUE_ORDERS: &'static [Option<QueueOrderKind>] = &[];

    /// Implements [`Kernel::debug`][1].
    ///
    /// [1]: crate::kernel::Kernel::debug
    fn raw_debug() -> Self::RawDebugPrinter;

    /// Implements [`Kernel::acquire_cpu_lock`][1].
    ///
    /// [1]: crate::kernel::Kernel::acquire_cpu_lock
    fn raw_acquire_cpu_lock() -> Result<(), CpuLockError>;

    /// Implements [`Kernel::release_cpu_lock`][1].
    ///
    /// # Safety
    ///
    /// See the [Safety][1] section of `Kernel::release_cpu_lock`'s
    /// documentation.
    ///
    /// [1]: crate::kernel::Kernel::release_cpu_lock
    unsafe fn raw_release_cpu_lock() -> Result<(), CpuLockError>;

    /// Return a flag indicating whether CPU Lock is currently active.
    fn raw_has_cpu_lock() -> bool;

    /// Implements [`Kernel::unboost_priority`][1].
    ///
    /// # Safety
    ///
    /// See the [Safety][1] section of `Kernel::unboost_priority`'s
    /// documentation.
    ///
    /// [1]: crate::kernel::Kernel::unboost_priority
    unsafe fn raw_unboost_priority() -> Result<(), BoostPriorityError>;

    /// Implements [`Kernel::is_priority_boost_active`][1].
    ///
    /// [1]: crate::kernel::Kernel::is_priority_boost_active
    fn raw_is_priority_boost_active() -> bool;

    /// Implements [`Kernel::is_task_context`][1].
    ///
    /// [1]: crate::kernel::Kernel::is_task_context
    fn raw_is_task_context() -> bool;

    /// Implements [`Kernel::is_interrupt_context`][1].
    ///
    /// [1]: crate::kernel::Kernel::is_interrupt_context
    fn raw_is_interrupt_context() -> bool;

    /// Implements [`Kernel::is_boot_complete`][1].
    ///
    /// [1]: crate::kernel::Kernel::is_boot_complete
    fn raw_is_boot_complete() -> bool;

    /// Implements [`Kernel::set_time`][1].
    ///
    /// [1]: crate::kernel::Kernel::set_time
    fn raw_set_time(time: Time) -> Result<(), TimeError>;

    // TODO: get time resolution?

    /// Implements [`Kernel::exit_task`][1].
    ///
    /// # Safety
    ///
    /// See the [Safety][1] section of `Kernel::exit_task`'s
    /// documentation.
    ///
    /// [1]: crate::kernel::Kernel::exit_task
    unsafe fn raw_exit_task() -> Result<!, ExitTaskError>;

    /// Implements [`Kernel::park`][1].
    ///
    /// [1]: crate::kernel::Kernel::park
    fn raw_park() -> Result<(), ParkError>;

    /// Implements [`Kernel::park_timeout`][1].
    ///
    /// [1]: crate::kernel::Kernel::park_timeout
    fn raw_park_timeout(timeout: Duration) -> Result<(), ParkTimeoutError>;

    /// Implements [`Kernel::sleep`][1].
    ///
    /// [1]: crate::kernel::Kernel::sleep
    fn raw_sleep(duration: Duration) -> Result<(), SleepError>;

    /// Get the current task.
    fn raw_task_current() -> Result<Self::RawTaskId, GetCurrentTaskError>;

    /// Implements [`Task::activate`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::activate
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_activate(this: Self::RawTaskId) -> Result<(), ActivateTaskError>;

    /// Implements [`Task::interrupt`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::interrupt
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_interrupt(this: Self::RawTaskId) -> Result<(), InterruptTaskError>;

    /// Implements [`Task::unpark_exact`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::unpark_exact
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_unpark_exact(this: Self::RawTaskId) -> Result<(), UnparkExactError>;

    /// Implements [`Task::priority`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::priority
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_priority(this: Self::RawTaskId) -> Result<usize, GetTaskPriorityError>;

    /// Implements [`Task::effective_priority`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::effective_priority
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_effective_priority(
        this: Self::RawTaskId,
    ) -> Result<usize, GetTaskPriorityError>;
}

/// Provides the `time` method.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelTime: KernelBase {
    /// Implements [`Kernel::time`][1].
    ///
    /// [1]: crate::kernel::Kernel::time
    fn raw_time() -> Result<Time, TimeError>;
}

/// Provides the `boost_priority` method.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelBoostPriority: KernelBase {
    /// Implements [`Kernel::boost_priority`][1].
    ///
    /// [1]: crate::kernel::Kernel::boost_priority
    fn raw_boost_priority() -> Result<(), BoostPriorityError>;
}

/// Provides the `task_set_priority` method.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelTaskSetPriority: KernelBase {
    /// Implements [`Task::set_priority`][1].
    ///
    /// [1]: crate::kernel::task::TaskMethods::set_priority
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_task_set_priority(
        this: Self::RawTaskId,
        priority: usize,
    ) -> Result<(), SetTaskPriorityError>;
}

/// Provides the `adjust_time` method.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelAdjustTime: KernelBase {
    /// Implements [`Kernel::time_user_headroom`][1].
    ///
    /// [1]: crate::kernel::Kernel::time_user_headroom
    const RAW_TIME_USER_HEADROOM: Duration = Duration::from_secs(1);

    /// Implements [`Kernel::adjust_time`][1].
    ///
    /// [1]: crate::kernel::Kernel::adjust_time
    fn raw_adjust_time(delta: Duration) -> Result<(), AdjustTimeError>;
}

/// Specifies the sorting order of a wait queue.
///
/// This `enum` type is defined as `#[non_exhaustive]` to allow for potential
/// future extensions.
/// The function [`QueueOrder::is_supported`][] indicates whether a
/// `QueueOrder` is supported by the kernel. The behavior is
/// implementation-defined (preferably approximating the request or falling back
/// to a supported option) if a specified `QueueOrder` is not supported.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum QueueOrder {
    /// The wait queue is processed in a FIFO order.
    Fifo,
    /// The wait queue is processed in a task priority order. Tasks with the
    /// same priorities follow a FIFO order.
    TaskPriority,
}

impl QueueOrder {
    /// Return a flag indicating whether the kernel supports this `QueueOrder`.
    ///
    /// It's possible for this function to return `false` for all possible
    /// values of `QueueOrder` if the kernel doesn't precisely implement any
    /// possible options of `QueueOrder`.
    #[inline]
    pub const fn is_supported<System: KernelBase>(&self) -> bool {
        let kind = match self {
            QueueOrder::Fifo => QueueOrderKind::Fifo,
            QueueOrder::TaskPriority => QueueOrderKind::TaskPriority,
        };

        // `[T]::iter` is unusable in `const fn` [ref:const_slice_iter]
        let values = System::RAW_SUPPORTED_QUEUE_ORDERS;
        // FIXME: `needless_range_loop` false positive
        // <https://github.com/rust-lang/rust-clippy/issues/10524>
        #[expect(clippy::needless_range_loop)]
        for i in 0..values.len() {
            // `#[derive(PartialEq)]` doesn't derive `const PartialEq`
            // [ref:derive_const_partial_eq]
            if let Some(value) = values[i] {
                if value as u8 == kind as u8 {
                    return true;
                }
            }
        }
        false
    }
}

/// Indicates a variant of [`QueueOrder`][] supported by a kernel.
///
/// This type is used as the element type of
/// [`KernelBase::RAW_SUPPORTED_QUEUE_ORDERS`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum QueueOrderKind {
    /// [`QueueOrder::Fifo`][]
    Fifo,
    /// [`QueueOrder::TaskPriority`][]
    TaskPriority,
}

/// Provides access to the event group API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelEventGroup: KernelBase {
    /// The type to identify event groups.
    type RawEventGroupId: Id;

    /// Implements [`EventGroup::set`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::set
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_set(
        this: Self::RawEventGroupId,
        bits: EventGroupBits,
    ) -> Result<(), UpdateEventGroupError>;

    /// Implements [`EventGroup::clear`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::clear
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_clear(
        this: Self::RawEventGroupId,
        bits: EventGroupBits,
    ) -> Result<(), UpdateEventGroupError>;

    /// Implements [`EventGroup::get`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::get
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_get(
        this: Self::RawEventGroupId,
    ) -> Result<EventGroupBits, GetEventGroupError>;

    /// Implements [`EventGroup::wait`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::wait
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_wait(
        this: Self::RawEventGroupId,
        bits: EventGroupBits,
        flags: EventGroupWaitFlags,
    ) -> Result<EventGroupBits, WaitEventGroupError>;

    /// Implements [`EventGroup::wait_timeout`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::wait_timeout
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_wait_timeout(
        this: Self::RawEventGroupId,
        bits: EventGroupBits,
        flags: EventGroupWaitFlags,
        timeout: Duration,
    ) -> Result<EventGroupBits, WaitEventGroupTimeoutError>;

    /// Implements [`EventGroup::poll`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::poll
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_event_group_poll(
        this: Self::RawEventGroupId,
        bits: EventGroupBits,
        flags: EventGroupWaitFlags,
    ) -> Result<EventGroupBits, PollEventGroupError>;
}

bitflags::bitflags! {
    /// Options for [`EventGroup::wait`][1].
    ///
    /// [1]: crate::kernel::event_group::EventGroupMethods::wait
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct EventGroupWaitFlags: u8 {
        /// Wait for all of the specified bits to be set.
        const ALL = 1 << 0;

        /// Clear the specified bits after waiting for them.
        const CLEAR = 1 << 1;
    }
}

// TODO: Support changing `EventGroupBits`?
/// Unsigned integer type backing event groups.
pub type EventGroupBits = u32;

/// Provides access to the mutex API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelMutex: KernelBase {
    /// The type to identify mutexes.
    type RawMutexId: Id;

    /// Used by [`MutexProtocol::is_supported`].
    ///
    /// `None` elements don't match any values of `MutexProtocol`. This might be
    /// useful for conditionally enabling some of them.
    ///
    /// The default value is an empty slice.
    const RAW_SUPPORTED_MUTEX_PROTOCOLS: &'static [Option<MutexProtocolKind>] = &[];

    /// Implements [`Mutex::is_locked`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::is_locked
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_is_locked(this: Self::RawMutexId) -> Result<bool, QueryMutexError>;

    /// Implements [`Mutex::unlock`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::unlock
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_unlock(this: Self::RawMutexId) -> Result<(), UnlockMutexError>;

    /// Implements [`Mutex::lock`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::lock
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_lock(this: Self::RawMutexId) -> Result<(), LockMutexError>;

    /// Implements [`Mutex::lock_timeout`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::lock_timeout
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_lock_timeout(
        this: Self::RawMutexId,
        timeout: Duration,
    ) -> Result<(), LockMutexTimeoutError>;

    /// Implements [`Mutex::try_lock`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::try_lock
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_try_lock(this: Self::RawMutexId) -> Result<(), TryLockMutexError>;

    /// Implements [`Mutex::mark_consistent`][1].
    ///
    /// [1]: crate::kernel::mutex::MutexMethods::mark_consistent
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_mutex_mark_consistent(
        this: Self::RawMutexId,
    ) -> Result<(), MarkConsistentMutexError>;
}

/// Specifies the locking protocol to be followed by a [mutex].
///
/// This `enum` type is defined as `#[non_exhaustive]` to allow for potential
/// future extensions.
/// The function [`MutexProtocol::is_supported`][] indicates whether a
/// `MutexProtocol` is supported by the kernel. The behavior is
/// implementation-defined (preferably approximating the request or falling back
/// to a supported option) if a specified `MutexProtocol` is not supported.
///
/// [mutex]: crate::kernel::Mutex
///
/// <div class="admonition-follows"></div>
///
/// > **Relation to Other Specifications:** The operating systems and operating
/// > system specifications providing an interface for specifying a mutex
/// > protocol include (but are not limited to) the following: POSIX
/// > (`pthread_mutexattr_setprotocol` and `PTHREAD_PRIO_PROTECT`, etc.), RTEMS
/// > Classic API (`RTEMS_PRIORITY_CEILING`, etc.), and μITRON4.0 (`TA_CEILING`,
/// > etc.).
///
/// <div class="admonition-follows"></div>
///
/// > **Rationale:**
/// > When this enumerate type was added, the plan was to only support the
/// > priority ceiling protocol, so having a method
/// > `CfgMutexBuilder::ceiling_priority` taking a priority ceiling value would
/// > have been simpler. Nevertheless, it was decided to use this enumerate
/// > type to accomodate other protocols in the future and to allow specifying
/// > protocol-specific parameters.
#[doc = include_str!("../common.md")]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum MutexProtocol {
    /// Locking the mutex does not affect the priority of the owning task.
    None,
    /// Locking the mutex raises the effective priority of the owning task
    /// to the mutex's priority ceiling according to
    /// [the immediate priority ceiling protocol]. The inner value specifies the
    /// priority ceiling.
    ///
    /// The value must be in range `0..`[`num_task_priority_levels`].
    ///
    /// [`num_task_priority_levels`]: crate::kernel::Cfg::num_task_priority_levels
    /// [the immediate priority ceiling protocol]: https://en.wikipedia.org/wiki/Priority_ceiling_protocol
    Ceiling(usize),
}

impl MutexProtocol {
    /// Return a flag indicating whether the kernel supports this `MutexProtocol`.
    ///
    /// It's possible for this function to return `false` for all possible
    /// values of `MutexProtocol` if the kernel doesn't precisely implement any
    /// possible options of `MutexProtocol`.
    #[inline]
    pub const fn is_supported<System: KernelMutex>(&self) -> bool {
        let kind = match self {
            MutexProtocol::None => MutexProtocolKind::None,
            MutexProtocol::Ceiling(_) => MutexProtocolKind::Ceiling,
        };

        // `[T]::iter` is unusable in `const fn` [ref:const_slice_iter]
        let values = System::RAW_SUPPORTED_MUTEX_PROTOCOLS;
        // FIXME: `needless_range_loop` false positive
        // <https://github.com/rust-lang/rust-clippy/issues/10524>
        #[expect(clippy::needless_range_loop)]
        for i in 0..values.len() {
            // `#[derive(PartialEq)]` doesn't derive `const PartialEq`
            // [ref:derive_const_partial_eq]
            if let Some(value) = values[i] {
                if value as u8 == kind as u8 {
                    return true;
                }
            }
        }
        false
    }
}

/// Indicates a variant of [`MutexProtocol`][] supported by a kernel.
///
/// This type is used as the element type of
/// [`KernelBase::RAW_SUPPORTED_QUEUE_ORDERS`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum MutexProtocolKind {
    /// [`MutexProtocol::None`][]
    None,
    /// [`MutexProtocol::Ceiling`][]`(_)`
    Ceiling,
}

/// Provides access to the semaphore API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelSemaphore: KernelBase {
    /// The type to identify semaphores.
    type RawSemaphoreId: Id;

    /// Implements [`Semaphore::drain`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::drain
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_drain(this: Self::RawSemaphoreId) -> Result<(), DrainSemaphoreError>;

    /// Implements [`Semaphore::get`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::get
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_get(
        this: Self::RawSemaphoreId,
    ) -> Result<SemaphoreValue, GetSemaphoreError>;

    /// Implements [`Semaphore::signal`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::signal
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_signal(
        this: Self::RawSemaphoreId,
        count: SemaphoreValue,
    ) -> Result<(), SignalSemaphoreError>;

    /// Implements [`Semaphore::signal_one`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::signal_one
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_signal_one(
        this: Self::RawSemaphoreId,
    ) -> Result<(), SignalSemaphoreError>;

    /// Implements [`Semaphore::wait_one`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::wait_one
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_wait_one(this: Self::RawSemaphoreId) -> Result<(), WaitSemaphoreError>;

    /// Implements [`Semaphore::wait_one_timeout`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::wait_one_timeout
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_wait_one_timeout(
        this: Self::RawSemaphoreId,
        timeout: Duration,
    ) -> Result<(), WaitSemaphoreTimeoutError>;

    /// Implements [`Semaphore::poll_one`][1].
    ///
    /// [1]: crate::kernel::semaphore::SemaphoreMethods::poll_one
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_semaphore_poll_one(this: Self::RawSemaphoreId) -> Result<(), PollSemaphoreError>;
}

/// Unsigned integer type representing the number of permits held by a
/// [semaphore][].
///
/// [semaphore]: crate::kernel::Semaphore
///
/// <div class="admonition-follows"></div>
///
/// > **Rationale:** On the one hand, using a data type with a target-dependent
/// > size can hurt portability. On the other hand, a fixed-size data type such
/// > as `u32` can significantly increase the runtime overhead on extremely
/// > constrained targets such as AVR and MSP430. In addition, many RISC targets
/// > handle small data types less efficiently. The portability issue shouldn't
/// > pose a problem in practice.
#[doc = include_str!("../common.md")]
pub type SemaphoreValue = usize;

/// Provides access to the timer API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelTimer: KernelBase {
    /// The type to identify timers.
    type RawTimerId: Id;

    /// Implements [`Timer::start`][1].
    ///
    /// [1]: crate::kernel::timer::TimerMethods::start
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_timer_start(this: Self::RawTimerId) -> Result<(), StartTimerError>;

    /// Implements [`Timer::stop`][1].
    ///
    /// [1]: crate::kernel::timer::TimerMethods::stop
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_timer_stop(this: Self::RawTimerId) -> Result<(), StopTimerError>;

    /// Implements [`Timer::set_delay`][1].
    ///
    /// [1]: crate::kernel::timer::TimerMethods::set_delay
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_timer_set_delay(
        this: Self::RawTimerId,
        delay: Option<Duration>,
    ) -> Result<(), SetTimerDelayError>;

    /// Implements [`Timer::set_period`][1].
    ///
    /// [1]: crate::kernel::timer::TimerMethods::set_period
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_timer_set_period(
        this: Self::RawTimerId,
        period: Option<Duration>,
    ) -> Result<(), SetTimerPeriodError>;
}

/// Provides access to the interrupt line API exposed by a kernel.
///
/// # Safety
///
/// See the [Safety](self#safety) section of the module documentation.
pub unsafe trait KernelInterruptLine: KernelBase {
    /// The range of interrupt priority values considered [managed].
    ///
    /// Defaults to `0..0` (empty) when unspecified.
    ///
    /// [managed]: crate#interrupt-handling-framework
    #[allow(clippy::reversed_empty_ranges)] // on purpose
    const RAW_MANAGED_INTERRUPT_PRIORITY_RANGE: Range<InterruptPriority> = 0..0;

    /// The list of interrupt lines which are considered [managed].
    ///
    /// Defaults to `&[]` (empty) when unspecified.
    ///
    /// This is useful when the driver employs a fixed priority scheme and
    /// doesn't support changing interrupt line priorities.
    ///
    /// [managed]: crate#interrupt-handling-framework
    const RAW_MANAGED_INTERRUPT_LINES: &'static [InterruptNum] = &[];

    /// Implements [`InterruptLine::set_priority`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::set_priority
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_set_priority(
        this: InterruptNum,
        value: InterruptPriority,
    ) -> Result<(), SetInterruptLinePriorityError>;

    /// Implements [`InterruptLine::enable`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::enable
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_enable(this: InterruptNum)
        -> Result<(), EnableInterruptLineError>;

    /// Implements [`InterruptLine::disable`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::disable
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_disable(
        this: InterruptNum,
    ) -> Result<(), EnableInterruptLineError>;

    /// Implements [`InterruptLine::pend`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::pend
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_pend(this: InterruptNum) -> Result<(), PendInterruptLineError>;

    /// Implements [`InterruptLine::clear`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::clear
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_clear(this: InterruptNum) -> Result<(), ClearInterruptLineError>;

    /// Implements [`InterruptLine::is_pending`][1].
    ///
    /// [1]: crate::kernel::InterruptLine::is_pending
    ///
    /// # Safety
    ///
    /// See the [Safety](self#safety) section of the module documentation.
    unsafe fn raw_interrupt_line_is_pending(
        this: InterruptNum,
    ) -> Result<bool, QueryInterruptLineError>;
}

/// Numeric value used to identify interrupt lines.
///
/// The meaning of this value is defined by a kernel and target hardware. They
/// are not necessarily tightly packed from zero.
pub type InterruptNum = usize;

/// Priority value for an interrupt line.
pub type InterruptPriority = i16;

/// A combined second-level interrupt handler.
///
/// # Safety
///
/// Only meant to be called from a first-level interrupt handler. CPU Lock must
/// be inactive.
pub type InterruptHandlerFn = unsafe extern "C" fn();