From fecb2e3960e2ac37767e363296355f82fb773770 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 21 Jul 2015 08:47:14 -0400 Subject: [PATCH] Make the global options more resilient to unit tests running in parallel. --- components/servo/Cargo.lock | 15 +++++++------ components/util/Cargo.toml | 2 +- components/util/lib.rs | 2 ++ components/util/opts.rs | 45 +++++++++++++++++++++---------------- ports/cef/Cargo.lock | 15 +++++++------ ports/cef/core.rs | 2 +- ports/gonk/Cargo.lock | 11 ++++----- 7 files changed, 52 insertions(+), 40 deletions(-) diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 065d5ba5a57..568a41f5e7f 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -513,7 +513,7 @@ dependencies = [ "gl_generator 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "khronos_api 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "osmesa-sys 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -752,7 +752,7 @@ dependencies = [ [[package]] name = "lazy_static" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -944,7 +944,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1235,7 +1235,7 @@ name = "shared_library" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1283,7 +1283,7 @@ name = "string_cache" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "phf_macros 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1297,7 +1297,7 @@ name = "string_cache_plugin" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "mac 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_shared 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1316,7 +1316,7 @@ dependencies = [ "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1428,6 +1428,7 @@ dependencies = [ "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index 4ec010aef40..c7457ffa276 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -35,4 +35,4 @@ url = "0.2.36" euclid = "0.1" serde = "0.4" serde_macros = "0.4" - +lazy_static = "0.1" diff --git a/components/util/lib.rs b/components/util/lib.rs index ea3caba8a3d..4e7759795c9 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -4,6 +4,7 @@ #![feature(alloc)] #![feature(box_syntax)] +#![feature(box_raw)] #![feature(core_intrinsics)] #![feature(custom_derive)] #![feature(fnbox)] @@ -22,6 +23,7 @@ #![plugin(serde_macros)] #[macro_use] extern crate log; +#[macro_use] extern crate lazy_static; extern crate azure; extern crate alloc; diff --git a/components/util/opts.rs b/components/util/opts.rs index 2ec9bb43794..6787c9e7492 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -15,10 +15,8 @@ use std::cmp; use std::env; use std::io::{self, Write}; use std::fs::PathExt; -use std::mem; use std::path::Path; use std::process; -use std::ptr; use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT}; use url::{self, Url}; @@ -444,7 +442,7 @@ pub fn from_cmdline_args(args: &[String]) { exit_after_load: opt_match.opt_present("x"), }; - set(opts); + set_defaults(opts); } static EXPERIMENTAL_ENABLED: AtomicBool = ATOMIC_BOOL_INIT; @@ -463,27 +461,36 @@ pub fn experimental_enabled() -> bool { // Make Opts available globally. This saves having to clone and pass // opts everywhere it is used, which gets particularly cumbersome // when passing through the DOM structures. -static mut OPTIONS: *mut Opts = 0 as *mut Opts; +static mut DEFAULT_OPTIONS: *mut Opts = 0 as *mut Opts; +const INVALID_OPTIONS: *mut Opts = 0x01 as *mut Opts; -pub fn set(opts: Opts) { - unsafe { - assert!(OPTIONS.is_null()); +lazy_static! { + static ref OPTIONS: Opts = { + let opts = unsafe { + let initial = if !DEFAULT_OPTIONS.is_null() { + let opts = Box::from_raw(DEFAULT_OPTIONS); + *opts + } else { + default_opts() + }; + DEFAULT_OPTIONS = INVALID_OPTIONS; + initial + }; set_experimental_enabled(opts.enable_experimental); + opts + }; +} + +pub fn set_defaults(opts: Opts) { + unsafe { + assert!(DEFAULT_OPTIONS.is_null()); + assert!(DEFAULT_OPTIONS != INVALID_OPTIONS); let box_opts = box opts; - OPTIONS = mem::transmute(box_opts); + DEFAULT_OPTIONS = Box::into_raw(box_opts); } } #[inline] -pub fn get<'a>() -> &'a Opts { - unsafe { - // If code attempts to retrieve the options and they haven't - // been set by the platform init code, just return a default - // set of options. This is mostly useful for unit tests that - // run through a code path which queries the cmd line options. - if OPTIONS == ptr::null_mut() { - set(default_opts()); - } - mem::transmute(OPTIONS) - } +pub fn get() -> &'static Opts { + &OPTIONS } diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 4215f77dde8..5ef0cf62916 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -505,7 +505,7 @@ dependencies = [ "gl_generator 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "khronos_api 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "osmesa-sys 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -744,7 +744,7 @@ dependencies = [ [[package]] name = "lazy_static" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -924,7 +924,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1233,7 +1233,7 @@ name = "shared_library" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1281,7 +1281,7 @@ name = "string_cache" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "phf_macros 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1295,7 +1295,7 @@ name = "string_cache_plugin" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "mac 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_shared 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1314,7 +1314,7 @@ dependencies = [ "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1412,6 +1412,7 @@ dependencies = [ "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/cef/core.rs b/ports/cef/core.rs index 3f929b84e51..786bfa4439c 100644 --- a/ports/cef/core.rs +++ b/ports/cef/core.rs @@ -76,7 +76,7 @@ pub extern "C" fn cef_initialize(args: *const cef_main_args_t, temp_opts.enable_canvas_antialiasing = true; temp_opts.resources_path = None; temp_opts.url = None; - opts::set(temp_opts); + opts::set_defaults(temp_opts); if unsafe { (*settings).windowless_rendering_enabled != 0 } { init_window(); diff --git a/ports/gonk/Cargo.lock b/ports/gonk/Cargo.lock index 5eb778e90b5..c55071856d4 100644 --- a/ports/gonk/Cargo.lock +++ b/ports/gonk/Cargo.lock @@ -678,7 +678,7 @@ dependencies = [ [[package]] name = "lazy_static" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -841,7 +841,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1179,7 +1179,7 @@ name = "string_cache" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "phf_macros 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1193,7 +1193,7 @@ name = "string_cache_plugin" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "mac 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_shared 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1212,7 +1212,7 @@ dependencies = [ "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1301,6 +1301,7 @@ dependencies = [ "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",