Skip to content

Commit

Permalink
crates/sel4-root-task: Allow setting heap lock
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Jan 16, 2024
1 parent 533b699 commit a925433
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/sel4-root-task/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mk {
dependencies = {
inherit (localCrates)
sel4
sel4-immediate-sync-once-cell
sel4-panicking
sel4-panicking-env
sel4-dlmalloc
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-root-task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ unwinding = ["sel4-panicking/unwinding", "sel4-runtime-common/unwinding"]
[dependencies]
sel4 = { path = "../sel4" }
sel4-dlmalloc = { path = "../sel4-dlmalloc" }
sel4-immediate-sync-once-cell = { path = "../sel4-immediate-sync-once-cell" }
sel4-panicking = { path = "../sel4-panicking" }
sel4-panicking-env = { path = "../sel4-panicking/env" }
sel4-root-task-macros = { path = "macros" }
Expand Down
24 changes: 21 additions & 3 deletions crates/sel4-root-task/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
//

use sel4_dlmalloc::StaticDlmallocGlobalAlloc;
use sel4_sync::{DeferredNotificationMutexSyncOps, GenericRawMutex};
use sel4_immediate_sync_once_cell::ImmediateSyncOnceCell;
use sel4_panicking_env::abort;
use sel4_sync::{GenericRawMutex, IndirectNotificationMutexSyncOps};

pub use sel4_dlmalloc::StaticHeap;

pub fn set_global_allocator_mutex_notification(nfn: sel4::Notification) {
GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION
.set(nfn)
.unwrap_or_else(|_| abort!("global allocator mutex notification already initialized"))
}

static GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION: ImmediateSyncOnceCell<sel4::Notification> =
ImmediateSyncOnceCell::new();

#[doc(hidden)]
pub type GlobalAllocator<const N: usize> = StaticDlmallocGlobalAlloc<
GenericRawMutex<DeferredNotificationMutexSyncOps>,
GenericRawMutex<IndirectNotificationMutexSyncOps<fn() -> sel4::Notification>>,
&'static StaticHeap<N>,
>;

Expand All @@ -20,11 +31,18 @@ pub const fn new_global_allocator<const N: usize>(
bounds: &'static StaticHeap<N>,
) -> GlobalAllocator<N> {
StaticDlmallocGlobalAlloc::new(
GenericRawMutex::new(DeferredNotificationMutexSyncOps::new()),
GenericRawMutex::new(IndirectNotificationMutexSyncOps::new(|| {
*GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION
.get()
.unwrap_or_else(|| {
abort!("global allocator contention before mutex notification initialization")
})
})),
bounds,
)
}

#[doc(hidden)]
#[macro_export]
macro_rules! declare_heap {
($size:expr) => {
Expand Down
3 changes: 2 additions & 1 deletion crates/sel4-root-task/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use sel4_panicking as panicking;
mod heap;
mod termination;

pub use heap::set_global_allocator_mutex_notification;
pub use termination::{Never, Termination};

#[cfg(target_thread_local)]
Expand Down Expand Up @@ -75,7 +76,7 @@ where
let result = panicking::catch_unwind(|| f(bootinfo).report());
match result {
Ok(err) => abort!("main thread terminated with error: {err:?}"),
Err(_) => abort!("main thread panicked"),
Err(_) => abort!("uncaught panic in main thread"),
}
}

Expand Down

0 comments on commit a925433

Please sign in to comment.