MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / try_init

Method try_init

crates/wasmtime/src/sync_nostd.rs:73–99  ·  view source on GitHub ↗
(&self, f: impl FnOnce() -> Result<T, E>)

Source from the content-addressed store, hash-verified

71
72 #[cold]
73 fn try_init<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
74 let _guard = OnceLockGuard::new(&self.mutex);
75
76 // Check state again under lock
77 match self.state.load(Ordering::Acquire) {
78 UNINITIALIZED => {
79 self.state.store(INITIALIZING, Ordering::Release);
80 match f() {
81 Ok(val) => {
82 // SAFETY: We hold the lock and state is INITIALIZING
83 let ret = unsafe { &*(*self.val.get()).write(val) };
84 self.state.store(INITIALIZED, Ordering::Release);
85 Ok(ret)
86 }
87 Err(e) => {
88 self.state.store(UNINITIALIZED, Ordering::Release);
89 Err(e)
90 }
91 }
92 }
93 INITIALIZED => {
94 // SAFETY: State is INITIALIZED, so val has been written
95 Ok(unsafe { (*self.val.get()).assume_init_ref() })
96 }
97 _ => panic!("concurrent initialization"),
98 }
99 }
100}
101
102impl<T> Drop for OnceLock<T> {

Callers 1

get_or_try_initMethod · 0.80

Calls 7

OkFunction · 0.85
newFunction · 0.50
fFunction · 0.50
loadMethod · 0.45
storeMethod · 0.45
writeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected