|
11 | 11 | #![deny(missing_debug_implementations)]
|
12 | 12 | #![deny(unsafe_op_in_unsafe_fn)]
|
13 | 13 |
|
14 |
| -use core::cell::UnsafeCell; |
15 |
| -use core::sync::atomic::{AtomicBool, Ordering}; |
16 |
| - |
17 | 14 | pub use crate::addr::{align_down, align_up, PhysAddr, VirtAddr};
|
18 | 15 |
|
19 | 16 | pub mod addr;
|
@@ -66,60 +63,3 @@ impl PrivilegeLevel {
|
66 | 63 | }
|
67 | 64 | }
|
68 | 65 | }
|
69 |
| - |
70 |
| -/// A wrapper that can be used to safely create one mutable reference `&'static mut T` from a static variable. |
71 |
| -/// |
72 |
| -/// `SingleUseCell` is safe because it ensures that it only ever gives out one reference. |
73 |
| -/// |
74 |
| -/// ``SingleUseCell<T>` is a safe alternative to `static mut` or a static `UnsafeCell<T>`. |
75 |
| -#[derive(Debug)] |
76 |
| -pub struct SingleUseCell<T> { |
77 |
| - used: AtomicBool, |
78 |
| - value: UnsafeCell<T>, |
79 |
| -} |
80 |
| - |
81 |
| -impl<T> SingleUseCell<T> { |
82 |
| - /// Construct a new SingleUseCell. |
83 |
| - pub const fn new(value: T) -> Self { |
84 |
| - Self { |
85 |
| - used: AtomicBool::new(false), |
86 |
| - value: UnsafeCell::new(value), |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - /// Try to acquire a mutable reference to the wrapped value. |
91 |
| - /// This will only succeed the first time the function is |
92 |
| - /// called and fail on all following calls. |
93 |
| - /// |
94 |
| - /// ``` |
95 |
| - /// use x86_64::SingleUseCell; |
96 |
| - /// |
97 |
| - /// static FOO: SingleUseCell<i32> = SingleUseCell::new(0); |
98 |
| - /// |
99 |
| - /// // Call `try_get_mut` for the first time and get a reference. |
100 |
| - /// let first: &'static mut i32 = FOO.try_get_mut().unwrap(); |
101 |
| - /// assert_eq!(first, &0); |
102 |
| - /// |
103 |
| - /// // Calling `try_get_mut` again will return `None`. |
104 |
| - /// assert_eq!(FOO.try_get_mut(), None); |
105 |
| - /// ``` |
106 |
| - pub fn try_get_mut(&self) -> Option<&mut T> { |
107 |
| - let already_used = self.used.swap(true, Ordering::AcqRel); |
108 |
| - if already_used { |
109 |
| - None |
110 |
| - } else { |
111 |
| - Some(unsafe { |
112 |
| - // SAFETY: no reference has been given out yet and we won't give out another. |
113 |
| - &mut *self.value.get() |
114 |
| - }) |
115 |
| - } |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -// SAFETY: Sharing a `SingleUseCell<T>` between threads is safe regardless of whether `T` is `Sync` |
120 |
| -// because we only expose the inner value once to one thread. The `T: Send` bound makes sure that |
121 |
| -// sending a unique reference to another thread is safe. |
122 |
| -unsafe impl<T: Send> Sync for SingleUseCell<T> {} |
123 |
| - |
124 |
| -// SAFETY: It's safe to send a `SingleUseCell<T>` to another thread if it's safe to send `T`. |
125 |
| -unsafe impl<T: Send> Send for SingleUseCell<T> {} |
0 commit comments