-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,278 additions
and
654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::cell::RefCell; | ||
|
||
use once_cell::sync::OnceCell; | ||
use parking_lot::Mutex; | ||
|
||
use crate::RecordingStream; | ||
|
||
// TODO: RwLock...? | ||
|
||
// TODO: doc | ||
|
||
static GLOBAL_DATA_RECORDING: OnceCell<Mutex<Option<RecordingStream>>> = OnceCell::new(); | ||
thread_local! { | ||
static LOCAL_DATA_RECORDING: RefCell<Option<RecordingStream>> = RefCell::new(None); | ||
} | ||
|
||
// TODO: pass in RecordingType so we can have the same thing for blueprint and co? | ||
|
||
impl RecordingStream { | ||
pub fn global() -> Option<RecordingStream> { | ||
let rec: Option<RecordingStream> = GLOBAL_DATA_RECORDING | ||
.get_or_init(Default::default) | ||
.lock() | ||
.clone(); | ||
|
||
if rec.is_none() { | ||
re_log::warn_once!( | ||
"There is no active global recording available: \ | ||
have you called `set_global_data_recording`?" | ||
); | ||
} | ||
|
||
rec | ||
} | ||
|
||
pub fn global_or_opt(fallback: Option<RecordingStream>) -> Option<RecordingStream> { | ||
let rec: Option<RecordingStream> = GLOBAL_DATA_RECORDING | ||
.get_or_init(Default::default) | ||
.lock() | ||
.clone(); | ||
|
||
let rec = rec.or(fallback); | ||
|
||
if rec.is_none() { | ||
re_log::warn_once!( | ||
"There is no active global recording available: \ | ||
have you called `set_global_data_recording`?" | ||
); | ||
} | ||
|
||
rec | ||
} | ||
|
||
pub fn global_or(fallback: RecordingStream) -> RecordingStream { | ||
let rec: Option<RecordingStream> = GLOBAL_DATA_RECORDING | ||
.get_or_init(Default::default) | ||
.lock() | ||
.clone(); | ||
|
||
rec.unwrap_or(fallback) | ||
} | ||
|
||
pub fn set_global(rec: RecordingStream) { | ||
*GLOBAL_DATA_RECORDING.get_or_init(Default::default).lock() = Some(rec); | ||
} | ||
|
||
pub fn thread_local() -> Option<RecordingStream> { | ||
let rec: Option<RecordingStream> = LOCAL_DATA_RECORDING.with(|rec| rec.borrow().clone()); | ||
|
||
if rec.is_none() { | ||
re_log::warn_once!( | ||
"There is no active recording available for the current thread ({:?}): \ | ||
have you called `set_global_data_recording`?", | ||
std::thread::current().id(), | ||
); | ||
} | ||
|
||
rec | ||
} | ||
|
||
pub fn thread_local_or(fallback: RecordingStream) -> RecordingStream { | ||
let rec: Option<RecordingStream> = LOCAL_DATA_RECORDING.with(|rec| rec.borrow().clone()); | ||
|
||
rec.unwrap_or(fallback) | ||
} | ||
} | ||
|
||
// TODO: | ||
// - no active recording | ||
// - active recording is disabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.