in glean-core/src/lib.rs [229:252]
fn setup_state(state: State) {
// The `OnceCell` type wrapping our state is thread-safe and can only be set once.
// Therefore even if our check for it being empty succeeds, setting it could fail if a
// concurrent thread is quicker in setting it.
// However this will not cause a bigger problem, as the second `set` operation will just fail.
// We can log it and move on.
//
// For all wrappers this is not a problem, as the State object is intialized exactly once on
// calling `initialize` on the global singleton and further operations check that it has been
// initialized.
if STATE.get().is_none() {
if STATE.set(Mutex::new(state)).is_err() {
log::error!(
"Global Glean state object is initialized already. This probably happened concurrently."
);
}
} else {
// We allow overriding the global State object to support test mode.
// In test mode the State object is fully destroyed and recreated.
// This all happens behind a mutex and is therefore also thread-safe.
let mut lock = STATE.get().unwrap().lock().unwrap();
*lock = state;
}
}