diff --git a/Cargo.toml b/Cargo.toml index e064239..42c0577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,4 +38,6 @@ winapi = "0.2" gdi32-sys = "0.2" user32-sys = "0.2" kernel32-sys = "0.2" -lazy_static = "0.2" + +[target.'cfg(any(target_os="macos", target_os="windows"))'.dependencies] +lazy_static = "0.2" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 6d99464..825891e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ extern crate kernel32; extern crate gdi32; #[cfg(target_os = "windows")] extern crate user32; -#[cfg(target_os = "windows")] +#[cfg(any(target_os="macos", target_os="windows"))] #[macro_use] extern crate lazy_static; diff --git a/src/platform/with_cgl/native_gl_context.rs b/src/platform/with_cgl/native_gl_context.rs index 5809272..f364e7b 100644 --- a/src/platform/with_cgl/native_gl_context.rs +++ b/src/platform/with_cgl/native_gl_context.rs @@ -5,9 +5,14 @@ use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFuncti use core_foundation::base::TCFType; use core_foundation::string::CFString; use std::str::FromStr; +use std::sync::Mutex; use platform::NativeGLContextMethods; +lazy_static! { + static ref CHOOSE_PIXEL_FORMAT_MUTEX: Mutex<()> = Mutex::new(()); +} + pub struct NativeGLContextHandle(CGLContextObj); unsafe impl Send for NativeGLContextHandle {} @@ -94,6 +99,12 @@ impl NativeGLContextMethods for NativeGLContext { } fn create_shared(with: Option<&Self::Handle>) -> Result { + // CGLChoosePixelFormat fails if multiple threads try to open a display connection + // simultaneously. The following error is returned by CGLChoosePixelFormat: + // kCGLBadConnection - Invalid connection to Core Graphics. + // We use a static mutex guard to fix this issue + let _guard = CHOOSE_PIXEL_FORMAT_MUTEX.lock().unwrap(); + let mut attributes = [ 0 ];