sel4_atomic_ptr/
ops.rs

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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//

use core::sync::atomic::Ordering;

use super::{generic, Atomic, AtomicPtr};

impl<T> AtomicPtr<'_, T> {
    fn as_mut_ptr(self) -> *mut T {
        self.pointer.as_ptr()
    }

    fn as_const_ptr(self) -> *const T {
        self.as_mut_ptr().cast_const()
    }
}

impl<T: Atomic> AtomicPtr<'_, T> {
    #[inline]
    pub fn load(self, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_load(self.as_const_ptr(), order.into()) }
    }

    #[inline]
    pub fn store(self, val: T, order: Ordering) {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe {
            generic::atomic_store(self.as_mut_ptr(), val, order.into());
        }
    }

    #[inline]
    pub fn swap(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_swap(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn compare_exchange(
        self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T> {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe {
            generic::atomic_compare_exchange(
                self.as_mut_ptr(),
                current,
                new,
                success.into(),
                failure.into(),
            )
        }
    }

    #[inline]
    pub fn compare_exchange_weak(
        self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T> {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe {
            generic::atomic_compare_exchange_weak(
                self.as_mut_ptr(),
                current,
                new,
                success.into(),
                failure.into(),
            )
        }
    }

    #[inline]
    pub fn fetch_add(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_add(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_sub(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_sub(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_and(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_and(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_nand(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_nand(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_or(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_or(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_xor(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe { generic::atomic_xor(self.as_mut_ptr(), val, order.into()) }
    }

    #[inline]
    pub fn fetch_update<F>(
        self,
        set_order: Ordering,
        fetch_order: Ordering,
        mut f: F,
    ) -> Result<T, T>
    where
        F: FnMut(T) -> Option<T>,
    {
        let mut prev = self.load(fetch_order);
        while let Some(next) = f(prev) {
            match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
                x @ Ok(_) => return x,
                Err(next_prev) => prev = next_prev,
            }
        }
        Err(prev)
    }

    #[inline]
    pub fn fetch_max(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe {
            if T::IS_SIGNED {
                generic::atomic_max(self.as_mut_ptr(), val, order.into())
            } else {
                generic::atomic_umax(self.as_mut_ptr(), val, order.into())
            }
        }
    }

    #[inline]
    pub fn fetch_min(self, val: T, order: Ordering) -> T {
        // SAFETY: data races are prevented by atomic intrinsics.
        unsafe {
            if T::IS_SIGNED {
                generic::atomic_min(self.as_mut_ptr(), val, order.into())
            } else {
                generic::atomic_umin(self.as_mut_ptr(), val, order.into())
            }
        }
    }
}