1use super::{EnterRuntime, CONTEXT};
23/// Returns true if in a runtime context.
4pub(crate) fn current_enter_context() -> EnterRuntime {
5 CONTEXT.with(|c| c.runtime.get())
6}
78/// Forces the current "entered" state to be cleared while the closure
9/// is executed.
10pub(crate) fn exit_runtime<F: FnOnce() -> R, R>(f: F) -> R {
11// Reset in case the closure panics
12struct Reset(EnterRuntime);
1314impl Drop for Reset {
15fn drop(&mut self) {
16 CONTEXT.with(|c| {
17assert!(
18 !c.runtime.get().is_entered(),
19"closure claimed permanent executor"
20);
21 c.runtime.set(self.0);
22 });
23 }
24 }
2526let was = CONTEXT.with(|c| {
27let e = c.runtime.get();
28assert!(e.is_entered(), "asked to exit when not entered");
29 c.runtime.set(EnterRuntime::NotEntered);
30 e
31 });
3233let _reset = Reset(was);
34// dropping _reset after f() will reset ENTERED
35f()
36}