Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 260432b

Browse files
committed
Wrap static backend Exclusive data in Mutex
Reduces unnecessary separation of lock and data storage. Changed signature of with_exclusive_access delegate functions to allow for distinction between inner data borrowed lifetime and the total lifetime of the Exclusive object.
1 parent 6f3c6fd commit 260432b

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

wee_alloc/src/imp_static_array.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use const_init::ConstInit;
22
use core::alloc::{AllocErr, Opaque};
3-
use core::cell::UnsafeCell;
43
#[cfg(feature = "extra_assertions")]
54
use core::cell::Cell;
65
use core::ptr::NonNull;
@@ -25,17 +24,15 @@ pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<Opaque>, AllocE
2524
}
2625

2726
pub(crate) struct Exclusive<T> {
28-
lock: Mutex<bool>,
29-
inner: UnsafeCell<T>,
27+
inner: Mutex<T>,
3028

3129
#[cfg(feature = "extra_assertions")]
3230
in_use: Cell<bool>,
3331
}
3432

3533
impl<T: ConstInit> ConstInit for Exclusive<T> {
3634
const INIT: Self = Exclusive {
37-
lock: Mutex::new(false),
38-
inner: UnsafeCell::new(T::INIT),
35+
inner: Mutex::new(T::INIT),
3936

4037
#[cfg(feature = "extra_assertions")]
4138
in_use: Cell::new(false),
@@ -73,13 +70,12 @@ impl<T> Exclusive<T> {
7370
#[inline]
7471
pub(crate) unsafe fn with_exclusive_access<'a, F, U>(&'a self, f: F) -> U
7572
where
76-
F: FnOnce(&'a mut T) -> U,
73+
for<'x> F: FnOnce(&'x mut T) -> U,
7774
{
78-
let _l = self.lock.lock();
75+
let mut guard = self.inner.lock();
7976
assert_not_in_use(self);
8077
set_in_use(self);
81-
let data = &mut *self.inner.get();
82-
let result = f(data);
78+
let result = f(&mut guard);
8379
set_not_in_use(self);
8480
result
8581
}

wee_alloc/src/imp_unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<T> Exclusive<T> {
5050
#[inline]
5151
pub(crate) unsafe fn with_exclusive_access<'a, F, U>(&'a self, f: F) -> U
5252
where
53-
F: FnOnce(&'a mut T) -> U,
53+
for<'x> F: FnOnce(&'x mut T) -> U,
5454
{
5555
let code = libc::pthread_mutex_lock(&mut *self.lock.get());
5656
extra_assert_eq!(code, 0, "pthread_mutex_lock should run OK");

wee_alloc/src/imp_wasm32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<T> Exclusive<T> {
6868
#[inline]
6969
pub(crate) unsafe fn with_exclusive_access<'a, F, U>(&'a self, f: F) -> U
7070
where
71-
F: FnOnce(&'a mut T) -> U,
71+
for<'x> F: FnOnce(&'x mut T) -> U,
7272
{
7373
assert_not_in_use(self);
7474
set_in_use(self);

0 commit comments

Comments
 (0)