Next Generation WASM Microkernel Operating System
wasm
os
rust
microkernel
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
8cfg_if::cfg_if! {
9 if #[cfg(loom)] {
10 pub(crate) use loom::sync;
11 pub(crate) use loom::cell;
12 pub(crate) use loom::thread;
13 pub(crate) use loom::model;
14 pub(crate) use loom::lazy_static;
15 pub(crate) use loom::MAX_THREADS;
16 } else {
17 #[cfg(not(test))]
18 pub(crate) use core::sync;
19 #[cfg(test)]
20 pub(crate) use std::sync;
21 #[cfg(test)]
22 pub(crate) use std::thread;
23 #[cfg(test)]
24 pub(crate) use lazy_static::lazy_static;
25 #[cfg(test)]
26 pub const MAX_THREADS: usize = 8;
27
28 pub(crate) mod cell {
29 #[derive(Debug)]
30 #[repr(transparent)]
31 pub(crate) struct UnsafeCell<T: ?Sized>(core::cell::UnsafeCell<T>);
32
33 impl<T> UnsafeCell<T> {
34 pub const fn new(data: T) -> UnsafeCell<T> {
35 UnsafeCell(core::cell::UnsafeCell::new(data))
36 }
37 }
38
39 impl<T: ?Sized> UnsafeCell<T> {
40 #[inline(always)]
41 pub fn with<F, R>(&self, f: F) -> R
42 where
43 F: FnOnce(*const T) -> R,
44 {
45 f(self.0.get())
46 }
47 #[inline(always)]
48 pub fn with_mut<F, R>(&self, f: F) -> R
49 where
50 F: FnOnce(*mut T) -> R,
51 {
52 f(self.0.get())
53 }
54 }
55 impl<T> UnsafeCell<T> {
56 #[inline(always)]
57 #[must_use]
58 pub(crate) fn into_inner(self) -> T {
59 self.0.into_inner()
60 }
61 }
62 }
63
64 #[cfg(test)]
65 #[inline(always)]
66 pub(crate) fn model<F>(f: F)
67 where
68 F: Fn() + Sync + Send + 'static,
69 {
70 f()
71 }
72 }
73}