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
//! Mutexes
use core::{fmt, marker::PhantomData, mem::MaybeUninit};

use crate::{
    abi,
    error::{Error, ErrorCode, ErrorKind, Kind},
    time::Timeout,
};

define_error_kind! {
    /// Error type for [`MutexRef::lock`].
    pub enum LockError {
        #[cfg(not(feature = "none"))]
        BadContext,
        /// The task is a restricted task.
        #[cfg(all(not(feature = "none"), feature = "rstr_task"))]
        NotSupported,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
        #[cfg(not(feature = "none"))]
        Released,
        #[cfg(not(feature = "none"))]
        TerminateRequest,
        #[cfg(all(not(feature = "none"), feature = "dcre"))]
        Deleted,
        /// The calling task's priority is higher than the mutex's priority
        /// ceiling.
        ///
        /// # Rationale
        ///
        /// The `EINVAL` error of `pthread_mutex_lock`. This error kind is
        /// designed to accomodate any precondition violations that may occur
        /// in yet-to-be-seen kernels to be supported.
        #[cfg(not(feature = "none"))]
        BadParam,
        /// The calling task already owns the mutex.
        #[cfg(not(feature = "none"))]
        Deadlock,
    }
}

impl ErrorKind for LockError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(all(not(feature = "none"), feature = "rstr_task"))]
            abi::E_NOSPT => Some(Self::NotSupported(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_RLWAI => Some(Self::Released(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_RASTER => Some(Self::TerminateRequest(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(all(not(feature = "none"), feature = "dcre"))]
            abi::E_DLT => Some(Self::Deleted(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ILUSE => Some(Self::BadParam(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_OBJ => Some(Self::Deadlock(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::lock_timeout`].
    pub enum LockTimeoutError {
        #[cfg(not(feature = "none"))]
        BadContext,
        /// The task is a restricted task.
        #[cfg(all(not(feature = "none"), feature = "rstr_task"))]
        NotSupported,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
        #[cfg(not(feature = "none"))]
        Timeout,
        #[cfg(not(feature = "none"))]
        Released,
        #[cfg(not(feature = "none"))]
        TerminateRequest,
        #[cfg(all(not(feature = "none"), feature = "dcre"))]
        Deleted,
        /// The calling task's priority is higher than the mutex's priority
        /// ceiling.
        ///
        /// # Rationale
        ///
        /// The `EINVAL` error of `pthread_mutex_lock`. This error kind is
        /// designed to accomodate any precondition violations that may occur
        /// in yet-to-be-seen kernels to be supported.
        #[cfg(not(feature = "none"))]
        BadParam,
        /// The calling task already owns the mutex.
        #[cfg(not(feature = "none"))]
        Deadlock,
    }
}

impl ErrorKind for LockTimeoutError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            // E_PAR is considered critial, hence excluded
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(all(not(feature = "none"), feature = "rstr_task"))]
            abi::E_NOSPT => Some(Self::NotSupported(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_TMOUT => Some(Self::Timeout(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_RLWAI => Some(Self::Released(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_RASTER => Some(Self::TerminateRequest(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(all(not(feature = "none"), feature = "dcre"))]
            abi::E_DLT => Some(Self::Deleted(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ILUSE => Some(Self::BadParam(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_OBJ => Some(Self::Deadlock(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::try_lock`].
    pub enum TryLockError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
        /// The calling task's priority is higher than the mutex's priority
        /// ceiling.
        ///
        /// # Rationale
        ///
        /// The `EINVAL` error of `pthread_mutex_lock`. This error kind is
        /// designed to accomodate any precondition violations that may occur
        /// in yet-to-be-seen kernels to be supported.
        #[cfg(not(feature = "none"))]
        BadParam,
        /// The calling task already owns the mutex.
        #[cfg(not(feature = "none"))]
        Deadlock,
        #[cfg(not(feature = "none"))]
        Timeout,
    }
}

impl ErrorKind for TryLockError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ILUSE => Some(Self::BadParam(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_OBJ => Some(Self::Deadlock(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_TMOUT => Some(Self::Timeout(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::unlock`].
    pub enum UnlockError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
        /// The mutex is not the lastly-locked mutex currently owned by the
        /// calling task (TOPPERS third-generation kernels, `E_OBJ`). The mutex
        /// is not currently owned by the calling task (μITRON 4.0 and
        /// μT-Kernel, `E_ILUSE`).
        ///
        /// # Rationale
        ///
        /// The name was inspired by the FTP and SMTP error 503 (bad sequence of
        /// commands) and SOLID `SOLID_ERR_BADSEQUENCE`. A mutex is intended
        /// to be used in a specific sequence (lock followed by unlock). The
        /// TOPPERS third-generation kernels impose a more stringent requirement
        /// on the sequence: mutexes must be unlocked in a lock-reverse order.
        #[cfg(not(feature = "none"))]
        BadSequence,
    }
}

impl ErrorKind for UnlockError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_OBJ => Some(Self::BadSequence(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::initialize`].
    pub enum InitializeError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
    }
}

impl ErrorKind for InitializeError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::info`].
    pub enum InfoError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
    }
}

impl ErrorKind for InfoError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`Mutex::build`].
    #[cfg(feature = "dcre")]
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    pub enum BuildError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(any())]
        AccessDenied,
        /// Ran out of mutex IDs.
        #[cfg(not(feature = "none"))]
        OutOfMemory,
        /// Bad parameter.
        ///
        ///  - The priority ceiling is out of range (NGKI2037, `E_PAR`).
        ///
        ///  - The priority ceiling refers to a priority value which is
        ///    configured to use subpriorities (NGKI3682, `E_ILUSE`).
        ///
        ///  - Unrecognized flags are specified (NGKI2025, `E_RSATR`).
        ///
        #[cfg(not(feature = "none"))]
        BadParam,
    }
}

#[cfg(feature = "dcre")]
impl ErrorKind for BuildError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            // `E_MACV` is considered critical, hence excluded
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_NOID => Some(Self::OutOfMemory(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_PAR | abi::E_RSATR => Some(Self::BadParam(Kind::from_error_code(code))),
            #[cfg(any(
                all(feature = "asp3", feature = "subprio"),
                feature = "fmp3",
                feature = "solid_fmp3"
            ))]
            abi::E_ILUSE => Some(Self::BadParam(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

define_error_kind! {
    /// Error type for [`MutexRef::delete`].
    #[cfg(feature = "dcre")]
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    pub enum DeleteError {
        #[cfg(not(feature = "none"))]
        BadContext,
        #[cfg(not(feature = "none"))]
        BadId,
        #[cfg(any())]
        AccessDenied,
        #[cfg(not(feature = "none"))]
        BadState,
    }
}

#[cfg(feature = "dcre")]
impl ErrorKind for DeleteError {
    fn from_error_code(code: ErrorCode) -> Option<Self> {
        match code.get() {
            #[cfg(not(feature = "none"))]
            abi::E_CTX => Some(Self::BadContext(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_ID | abi::E_NOEXS => Some(Self::BadId(Kind::from_error_code(code))),
            #[cfg(any())]
            abi::E_OACV => Some(Self::AccessDenied(Kind::from_error_code(code))),
            #[cfg(not(feature = "none"))]
            abi::E_OBJ => Some(Self::BadState(Kind::from_error_code(code))),
            _ => None,
        }
    }
}

/// Mutex information returned by [`MutexRef::info`].
#[derive(Debug, Clone, Copy)]
pub struct Info {
    #[cfg(not(feature = "none"))]
    raw: abi::T_RMTX,
}

impl Info {
    /// Get the owning task's ID.
    #[inline]
    pub fn owning_task_id(&self) -> Option<abi::NonNullID> {
        match () {
            #[cfg(not(feature = "none"))]
            () => abi::NonNullID::new(self.raw.htskid),
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// Get the first waiting task's ID.
    #[inline]
    pub fn first_waiting_task_id(&self) -> Option<abi::NonNullID> {
        match () {
            #[cfg(not(feature = "none"))]
            () => abi::NonNullID::new(self.raw.wtskid),
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }
}

/// A borrowed reference to a mutex.
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct MutexRef<'a> {
    id: abi::NonNullID,
    _phantom: PhantomData<&'a ()>,
}

impl fmt::Debug for MutexRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Mutex({})", self.id)
    }
}

/// # Object ID conversion
impl MutexRef<'_> {
    /// Construct a `MutexRef` from a raw object ID.
    ///
    /// # Safety
    ///
    /// See [Object ID Wrappers](crate#object-id-wrappers).
    #[inline]
    pub const unsafe fn from_raw_nonnull(id: abi::NonNullID) -> Self {
        Self {
            id,
            _phantom: PhantomData,
        }
    }

    /// Get the raw object ID.
    #[inline]
    pub const fn as_raw(self) -> abi::ID {
        self.id.get()
    }

    /// Get the raw object ID as [` abi::NonNullID`].
    #[inline]
    pub const fn as_raw_nonnull(self) -> abi::NonNullID {
        self.id
    }
}

/// # Management
impl MutexRef<'_> {
    /// `del_mtx`: Delete the mutex.
    ///
    /// # Safety
    ///
    /// See [Object ID Wrappers](crate#object-id-wrappers).
    #[inline]
    #[doc(alias = "del_mtx")]
    #[cfg(feature = "dcre")]
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    pub unsafe fn delete(self) -> Result<(), Error<DeleteError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::del_mtx(self.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// `ref_mtx`: Get the mutex's general information.
    #[inline]
    #[doc(alias = "ref_mtx")]
    pub fn info(self) -> Result<Info, Error<InfoError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                let mut pri = MaybeUninit::uninit();
                Error::err_if_negative(abi::ref_mtx(self.as_raw(), pri.as_mut_ptr()))?;
                Ok(Info {
                    raw: pri.assume_init(),
                })
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }
}

/// # Mutex Operations
impl MutexRef<'_> {
    /// `loc_mtx`: Lock the mutex.
    #[inline]
    #[doc(alias = "loc_mtx")]
    pub fn lock(self) -> Result<(), Error<LockError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::loc_mtx(self.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// `tloc_mtx`: Lock the mutex with timeout.
    #[inline]
    #[doc(alias = "tloc_mtx")]
    pub fn lock_timeout(self, tmo: Timeout) -> Result<(), Error<LockTimeoutError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::tloc_mtx(self.as_raw(), tmo.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// `ploc_mtx`: Attempt to lock the mutex. Returns immediately if it's
    /// already locked.
    #[inline]
    #[doc(alias = "ploc_mtx")]
    pub fn try_lock(self) -> Result<(), Error<TryLockError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::ploc_mtx(self.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// `unl_mtx`: Unlock the mutex.
    #[inline]
    #[doc(alias = "unl_mtx")]
    pub fn unlock(self) -> Result<(), Error<UnlockError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::unl_mtx(self.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }

    /// `ini_mtx`: Initialize the mutex.
    #[inline]
    #[doc(alias = "ini_mtx")]
    pub fn initialize(self) -> Result<(), Error<InitializeError>> {
        match () {
            #[cfg(not(feature = "none"))]
            () => unsafe {
                Error::err_if_negative(abi::ini_mtx(self.as_raw()))?;
                Ok(())
            },
            #[cfg(feature = "none")]
            () => unimplemented!(),
        }
    }
}

#[cfg(feature = "dcre")]
pub use self::owned::*;

#[cfg(feature = "dcre")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
mod owned {
    use super::*;
    use crate::wait::QueueOrder;

    /// Specifies a priority protection protocol used by a [mutex](Mutex).
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    pub enum PriorityProtection {
        /// No priority protection.
        None,
        /// The priority ceiling protocol.
        Ceiling {
            /// Specifies the priority ceiling.
            priority: crate::task::Priority,
        },
        #[cfg(any(feature = "none", all(feature = "solid_asp3", feature = "pi_mutex")))]
        #[cfg_attr(
            feature = "doc_cfg",
            doc(cfg(any(feature = "none", all(feature = "solid_asp3", feature = "pi_mutex"))))
        )]
        /// The priority inheritance protocol.
        Inherit,
    }

    impl PriorityProtection {
        /// Return `Some(Self::Inherit)` if it's supported by the target kernel.
        ///
        /// # Examples
        ///
        /// ```
        /// use itron::mutex::PriorityProtection;
        /// let priority_protection = PriorityProtection::inherit()
        ///     .unwrap_or(PriorityProtection::None);
        /// ```
        #[inline]
        #[allow(unreachable_code)]
        pub const fn inherit() -> Option<Self> {
            #[cfg(any(feature = "none", all(feature = "solid_asp3", feature = "pi_mutex")))]
            {
                return Some(Self::Inherit);
            }
            None
        }
    }

    /// The builder type for [mutexes](Mutex). Created by [`Mutex::build`].
    ///
    /// Its generic parameters are an implementation detail.
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    #[must_use = "`Builder` creates nothing unless you call `.finish()`"]
    pub struct Builder {
        #[cfg(not(feature = "none"))]
        raw: abi::T_CMTX,
        priority_protection: PriorityProtection,
    }

    impl Mutex {
        /// `acre_mtx`: Create a builder for `Mutex`.
        ///
        /// # Examples
        ///
        /// ```rust,no_run
        /// use itron::mutex::Mutex;
        /// let mutex = Mutex::build()
        ///     .finish()
        ///     .expect("failed to create a mutex");
        ///
        /// mutex.as_ref().lock()
        ///    .expect("failed to perform a lock operation");
        /// mutex.as_ref().unlock()
        ///    .expect("failed to perform a unlock operation");
        /// ```
        ///
        /// ```rust,no_run
        /// use itron::mutex::{Mutex, PriorityProtection};
        /// let mutex = Mutex::build()
        ///     .priority_protection(PriorityProtection::Ceiling { priority: 4 })
        ///     .finish()
        ///     .expect("failed to create a priority-ceiling mutex");
        ///
        /// mutex.as_ref().lock()
        ///    .expect("failed to perform a lock operation");
        /// mutex.as_ref().unlock()
        ///    .expect("failed to perform a unlock operation");
        /// ```
        #[inline]
        #[doc(alias = "acre_mtx")]
        pub fn build() -> Builder {
            Builder {
                #[cfg(not(feature = "none"))]
                raw: abi::T_CMTX {
                    mtxatr: abi::TA_NULL,
                    ceilpri: 0,
                },
                priority_protection: PriorityProtection::None,
            }
        }
    }

    impl Builder {
        /// Specify the priority protection mechanism to use.
        /// Defaults to [`None`] when unspecified.
        ///
        /// [`None`]: PriorityProtection::None
        #[inline]
        pub fn priority_protection(self, value: PriorityProtection) -> Builder {
            Builder {
                #[cfg(not(feature = "none"))]
                priority_protection: value,
                ..self
            }
        }

        /// Specify the queue order. Defaults to `Fifo` when unspecified.
        /// Ignored if
        #[inline]
        pub fn queue_order(self, value: QueueOrder) -> Builder {
            Builder {
                #[cfg(not(feature = "none"))]
                raw: abi::T_CMTX {
                    mtxatr: value.as_raw_atr(),
                    ..self.raw
                },
                ..self
            }
        }
    }

    impl Builder {
        /// Create a mutex using the specified parameters.
        #[allow(unused_mut)]
        pub fn finish(mut self) -> Result<Mutex, Error<BuildError>> {
            match self.priority_protection {
                #[cfg(not(feature = "none"))]
                PriorityProtection::None => {}
                #[cfg(not(feature = "none"))]
                PriorityProtection::Ceiling { priority } => {
                    self.raw.mtxatr = abi::TA_CEILING;
                    self.raw.ceilpri = priority;
                }
                #[cfg(all(feature = "solid_asp3", feature = "pi_mutex"))]
                PriorityProtection::Inherit => {
                    self.raw.mtxatr = abi::TA_INHERIT;
                }
                #[cfg(feature = "none")]
                _ => unimplemented!(),
            }
            match () {
                #[cfg(not(feature = "none"))]
                () => unsafe {
                    let id = Error::err_if_negative(abi::acre_mtx(&self.raw))?;
                    // Safety: We own the mutex we create
                    Ok(Mutex::from_raw_nonnull(abi::NonNullID::new_unchecked(id)))
                },
                #[cfg(feature = "none")]
                () => unimplemented!(),
            }
        }
    }

    /// An owned mutex.
    ///
    /// [Deletes] the mutex automatically when dropped. The destructor will
    /// panic if the deletion fails.
    ///
    /// [Deletes]: MutexRef::delete
    #[derive(PartialEq, Eq)]
    #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "dcre")))]
    pub struct Mutex(MutexRef<'static>);

    impl fmt::Debug for Mutex {
        #[inline]
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            self.0.fmt(f)
        }
    }

    impl Drop for Mutex {
        #[inline]
        fn drop(&mut self) {
            unsafe { self.0.delete().unwrap() };
        }
    }

    impl Mutex {
        /// Construct a `Mutex` from a raw object ID.
        ///
        /// # Safety
        ///
        /// See [Object ID Wrappers](crate#object-id-wrappers).
        #[inline]
        pub const unsafe fn from_raw_nonnull(id: abi::NonNullID) -> Self {
            Self(unsafe { MutexRef::from_raw_nonnull(id) })
        }

        /// Consume and "leak" `self`, returning a reference `MutexRef<'a>`.
        #[inline]
        pub const fn leak<'a>(self) -> MutexRef<'a> {
            let out = self.0;
            core::mem::forget(self);
            out
        }

        /// Get the raw object ID.
        #[inline]
        pub const fn as_raw(&self) -> abi::ID {
            self.0.as_raw()
        }

        /// Get the raw object ID as [` abi::NonNullID`].
        #[inline]
        pub const fn as_raw_nonnull(&self) -> abi::NonNullID {
            self.0.as_raw_nonnull()
        }

        /// Borrow `Mutex` as [`MutexRef`].
        ///
        /// Use this to perform operations on mutexes because most of the
        /// methods are implemented on `MutexRef` but not `Mutex`.
        #[inline]
        pub const fn as_ref(&self) -> MutexRef<'_> {
            self.0
        }
    }
}