Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
Auto merge of #447 - paulrouget:initonce, r=jdm
Browse files Browse the repository at this point in the history
Introduce feature to only allow initialization once.

See #22039

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/447)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Nov 14, 2018
2 parents c16e7e2 + 2ccdca9 commit 5baf536
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ doctest = false

[features]
debugmozjs = ['mozjs_sys/debugmozjs']
init_once = []

[dependencies]
lazy_static = "1"
Expand Down
12 changes: 9 additions & 3 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ lazy_static! {
static ref PARENT: AtomicPtr<JSRuntime> = AtomicPtr::new(ptr::null_mut());
static ref OUTSTANDING_RUNTIMES: AtomicUsize = AtomicUsize::new(0);
static ref SHUT_DOWN: AtomicBool = AtomicBool::new(false);
static ref JS_INIT_CALLED: AtomicBool = AtomicBool::new(false);
}

/// A wrapper for the `JSContext` structure in SpiderMonkey.
Expand Down Expand Up @@ -157,7 +158,10 @@ impl Runtime {
let js_context = if outstanding == 0 {
// We are creating the first JSContext, so we need to initialize
// the runtime.
assert!(JS_Init());
if cfg!(not(feature = "init_once")) || !JS_INIT_CALLED.load(Ordering::SeqCst) {
assert!(JS_Init());
JS_INIT_CALLED.store(true, Ordering::SeqCst);
}
let js_context = JS_NewContext(default_heapsize, ChunkSize as u32, ptr::null_mut());
let parent_runtime = JS_GetRuntime(js_context);
assert!(!parent_runtime.is_null());
Expand Down Expand Up @@ -265,8 +269,10 @@ impl Drop for Runtime {

if OUTSTANDING_RUNTIMES.fetch_sub(1, Ordering::SeqCst) == 1 {
PARENT.store(ptr::null_mut(), Ordering::SeqCst);
SHUT_DOWN.store(true, Ordering::SeqCst);
JS_ShutDown();
if cfg!(not(feature = "init_once")) {
SHUT_DOWN.store(true, Ordering::SeqCst);
JS_ShutDown();
}
}
}
}
Expand Down

0 comments on commit 5baf536

Please sign in to comment.