Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed May 9, 2023
1 parent 32cc9f6 commit bffaafe
Show file tree
Hide file tree
Showing 31 changed files with 1,278 additions and 654 deletions.
7 changes: 1 addition & 6 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ demo = []
## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
glam = ["re_log_types/glam"]

## Add the `global_session` method.
global_session = ["dep:once_cell"]

## Integration with the [`image`](https://crates.io/crates/image/) crate.
image = ["re_log_types/image"]

Expand All @@ -44,12 +41,10 @@ re_sdk_comms = { workspace = true, features = ["client"] }
ahash.workspace = true
crossbeam.workspace = true
document-features = "0.2"
once_cell = "1.12"
parking_lot.workspace = true
thiserror.workspace = true

# Optional dependencies:
once_cell = { version = "1.12", optional = true }


[dev-dependencies]
arrow2_convert.workspace = true
Expand Down
90 changes: 90 additions & 0 deletions crates/re_sdk/src/global.rs
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
1 change: 1 addition & 0 deletions crates/re_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// ----------------
// Private modules:

mod global; // TODO
mod log_sink;
mod msg_sender;
mod recording_stream;
Expand Down
4 changes: 0 additions & 4 deletions crates/rerun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ demo = ["re_sdk?/demo"]
## Only relevant if feature `sdk` is enabled.
glam = ["re_sdk?/glam"]

## Add the `global_session` method.
## Only makes sense together with the `sdk` feature.
global_session = ["re_sdk?/global_session"]

## Integration with the [`image`](https://crates.io/crates/image/) crate.
image = ["re_log_types/image"]

Expand Down
Loading

0 comments on commit bffaafe

Please sign in to comment.