use lazy_static

This commit is contained in:
Folkert 2022-03-12 00:18:06 +01:00
parent e1d5b748b1
commit 28ce49c273
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 7 additions and 37 deletions

1
Cargo.lock generated
View file

@ -3335,6 +3335,7 @@ name = "roc_builtins"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dunce", "dunce",
"lazy_static",
"roc_collections", "roc_collections",
"roc_module", "roc_module",
"roc_region", "roc_region",

View file

@ -11,6 +11,7 @@ roc_region = { path = "../region" }
roc_module = { path = "../module" } roc_module = { path = "../module" }
roc_types = { path = "../types" } roc_types = { path = "../types" }
roc_target = { path = "../roc_target" } roc_target = { path = "../roc_target" }
lazy_static = "1.4.0"
[build-dependencies] [build-dependencies]
# dunce can be removed once ziglang/zig#5109 is fixed # dunce can be removed once ziglang/zig#5109 is fixed

View file

@ -13,45 +13,13 @@ use roc_types::subs::VarId;
use roc_types::types::RecordField; use roc_types::types::RecordField;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::{AtomicU8, Ordering}; lazy_static::lazy_static! {
static ref STDLIB: StdLib = standard_stdlib();
}
/// A global static that stores our initialized standard library definitions /// A global static that stores our initialized standard library definitions
static mut STDLIB: Option<StdLib> = None;
static mut STDLIB_STATE: AtomicU8 = AtomicU8::new(STDLIB_STATE_EMPTY);
const STDLIB_STATE_EMPTY: u8 = 0;
const STDLIB_STATE_LOCKED: u8 = 1;
const STDLIB_STATE_READY: u8 = 2;
pub fn borrow_stdlib() -> &'static StdLib { pub fn borrow_stdlib() -> &'static StdLib {
let state = unsafe { &STDLIB_STATE }; &STDLIB
let ce = state.compare_exchange(
STDLIB_STATE_EMPTY,
STDLIB_STATE_LOCKED,
Ordering::Acquire,
Ordering::Relaxed,
);
match ce {
Ok(_) => {
// the STDLIB static is empty, and we have the write lock
let stdlib = unsafe { &mut STDLIB };
*stdlib = Some(standard_stdlib());
state.store(STDLIB_STATE_READY, Ordering::SeqCst);
stdlib.as_ref().unwrap()
}
Err(_) => {
// someone else has the write lock. They will soon move into the ready state
while state.load(Ordering::SeqCst) != STDLIB_STATE_READY {
// do nothng
}
unsafe { &STDLIB }.as_ref().unwrap()
}
}
} }
/// Example: /// Example:

View file

@ -9,7 +9,7 @@ use roc_can::operator;
use roc_can::scope::Scope; use roc_can::scope::Scope;
use roc_collections::all::{ImMap, MutMap, SendSet}; use roc_collections::all::{ImMap, MutMap, SendSet};
use roc_constrain::expr::constrain_expr; use roc_constrain::expr::constrain_expr;
use roc_constrain::module::{introduce_builtin_imports, Import}; use roc_constrain::module::introduce_builtin_imports;
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds}; use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_parse::parser::{SourceError, SyntaxError}; use roc_parse::parser::{SourceError, SyntaxError};
use roc_problem::can::Problem; use roc_problem::can::Problem;