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
//
// Copyright 2023, Colias Group, LLC
// Copyright (c) 2020 Arm Limited
//
// SPDX-License-Identifier: MIT
//

use core::sync::atomic::{fence, AtomicIsize, Ordering};

use sel4_immediate_sync_once_cell::ImmediateSyncOnceCell;

pub struct GenericRawMutex<O> {
    sync_ops: O,
    value: AtomicIsize,
}

impl<O> GenericRawMutex<O> {
    pub const fn new(sync_ops: O) -> Self {
        Self {
            sync_ops,
            value: AtomicIsize::new(1),
        }
    }
}

pub trait MutexSyncOpsWithInteriorMutability {
    type ModifyInput;
    type ModifyOutput;

    fn modify(&self, input: Self::ModifyInput) -> Self::ModifyOutput;
}

impl<O: MutexSyncOpsWithInteriorMutability> GenericRawMutex<O> {
    pub fn modify(&self, input: O::ModifyInput) -> O::ModifyOutput {
        self.sync_ops.modify(input)
    }
}

pub trait MutexSyncOps {
    fn signal(&self);
    fn wait(&self);
}

unsafe impl<O: MutexSyncOps> lock_api::RawMutex for GenericRawMutex<O> {
    type GuardMarker = lock_api::GuardNoSend; // TODO

    #[allow(clippy::declare_interior_mutable_const)]
    const INIT: Self = unimplemented!();

    fn lock(&self) {
        let old_value = self.value.fetch_sub(1, Ordering::Acquire);
        if old_value <= 0 {
            self.sync_ops.wait();
            fence(Ordering::Acquire);
        }
    }

    fn try_lock(&self) -> bool {
        unimplemented!()
    }

    unsafe fn unlock(&self) {
        let old_value = self.value.fetch_add(1, Ordering::Release);
        if old_value < 0 {
            self.sync_ops.signal();
        }
    }
}

pub trait MutexSyncOpsWithNotification {
    fn notification(&self) -> sel4::cap::Notification;
}

impl<O: MutexSyncOpsWithNotification> MutexSyncOps for O {
    fn signal(&self) {
        self.notification().signal()
    }

    fn wait(&self) {
        let _badge = self.notification().wait();
    }
}

impl MutexSyncOpsWithNotification for sel4::cap::Notification {
    fn notification(&self) -> sel4::cap::Notification {
        *self
    }
}

impl<F: Fn() -> sel4::cap::Notification> MutexSyncOpsWithNotification for F {
    fn notification(&self) -> sel4::cap::Notification {
        (self)()
    }
}

pub struct DeferredNotificationMutexSyncOps {
    inner: ImmediateSyncOnceCell<sel4::cap::Notification>,
}

impl DeferredNotificationMutexSyncOps {
    pub const fn new() -> Self {
        Self {
            inner: ImmediateSyncOnceCell::new(),
        }
    }
}

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

impl MutexSyncOpsWithNotification for DeferredNotificationMutexSyncOps {
    fn notification(&self) -> sel4::cap::Notification {
        *self.inner.get().unwrap()
    }
}

impl MutexSyncOpsWithInteriorMutability for DeferredNotificationMutexSyncOps {
    type ModifyInput = sel4::cap::Notification;
    type ModifyOutput = ();

    fn modify(&self, input: Self::ModifyInput) -> Self::ModifyOutput {
        self.inner.set(input).unwrap()
    }
}