Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8// #![allow(unused_imports,)]
9
10cfg_if::cfg_if! {
11 if #[cfg(loom)] {
12 pub(crate) use loom::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
13 pub(crate) use loom::cell::UnsafeCell;
14 pub(crate) use loom::thread;
15 pub(crate) use loom::model;
16 pub(crate) use loom::sync::Arc;
17 } else {
18 pub(crate) use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
19
20 #[derive(Debug)]
21 #[repr(transparent)]
22 pub(crate) struct UnsafeCell<T: ?Sized>(core::cell::UnsafeCell<T>);
23
24 impl<T> UnsafeCell<T> {
25 pub const fn new(data: T) -> UnsafeCell<T> {
26 UnsafeCell(core::cell::UnsafeCell::new(data))
27 }
28 }
29
30 impl<T: ?Sized> UnsafeCell<T> {
31 #[inline(always)]
32 pub fn with<F, R>(&self, f: F) -> R
33 where
34 F: FnOnce(*const T) -> R,
35 {
36 f(self.0.get())
37 }
38 #[inline(always)]
39 pub fn with_mut<F, R>(&self, f: F) -> R
40 where
41 F: FnOnce(*mut T) -> R,
42 {
43 f(self.0.get())
44 }
45 }
46 impl<T> UnsafeCell<T> {
47 #[inline(always)]
48 #[must_use]
49 pub(crate) fn into_inner(self) -> T {
50 self.0.into_inner()
51 }
52 }
53
54 #[derive(Debug)]
55 #[repr(transparent)]
56 pub(crate) struct AtomicU8(core::sync::atomic::AtomicU8);
57
58 impl AtomicU8 {
59 #[inline(always)]
60 pub const fn new(val: u8) -> Self {
61 Self(core::sync::atomic::AtomicU8::new(val))
62 }
63 #[inline(always)]
64 pub fn load(&self, order: Ordering) -> u8 {
65 self.0.load(order)
66 }
67 #[inline(always)]
68 pub fn store(& self, val: u8, order: Ordering) {
69 self.0.store(val, order);
70 }
71 #[inline(always)]
72 pub fn compare_exchange(& self, current: u8, new: u8, success: Ordering, failure: Ordering) -> Result<u8, u8> {
73 self.0.compare_exchange(current, new,success, failure)
74 }
75 #[inline(always)]
76 pub fn with_mut<R>(&mut self, f: impl FnOnce(&mut u8) -> R) -> R {
77 f(self.0.get_mut())
78 }
79 }
80
81 #[cfg(test)]
82 pub(crate) use std::sync::Arc;
83 #[cfg(test)]
84 pub(crate) use std::thread;
85
86 #[cfg(test)]
87 #[inline(always)]
88 pub(crate) fn model<F>(f: F)
89 where
90 F: Fn() + Sync + Send + 'static,
91 {
92 f()
93 }
94 }
95}