1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Bindings to acquire a global named lock.
13 //! This is intended to be used to synchronize multiple compiler processes to
14 //! ensure that we can output complete errors without interleaving on Windows.
15 //! Note that this is currently only needed for allowing only one 32-bit MSVC
16 //! linker to execute at once on MSVC hosts, so this is only implemented for
17 //! `cfg(windows)`. Also note that this may not always be used on Windows,
18 //! only when targeting 32-bit MSVC.
20 //! For more information about why this is necessary, see where this is called.
25 #[allow(nonstandard_style)]
26 pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
27 use std::ffi::CString;
30 type LPSECURITY_ATTRIBUTES = *mut u8;
32 type LPCSTR = *const u8;
33 type HANDLE = *mut u8;
36 const INFINITE: DWORD = !0;
37 const WAIT_OBJECT_0: DWORD = 0;
38 const WAIT_ABANDONED: DWORD = 0x00000080;
41 fn CreateMutexA(lpMutexAttributes: LPSECURITY_ATTRIBUTES,
45 fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
46 fn ReleaseMutex(hMutex: HANDLE) -> BOOL;
47 fn CloseHandle(hObject: HANDLE) -> BOOL;
50 struct Handle(HANDLE);
52 impl Drop for Handle {
65 ReleaseMutex((self.0).0);
70 let cname = CString::new(name).unwrap();
72 // Create a named mutex, with no security attributes and also not
73 // acquired when we create it.
75 // This will silently create one if it doesn't already exist, or it'll
76 // open up a handle to one if it already exists.
77 let mutex = CreateMutexA(0 as *mut _, 0, cname.as_ptr() as *const u8);
79 panic!("failed to create global mutex named `{}`: {}",
81 io::Error::last_os_error());
83 let mutex = Handle(mutex);
85 // Acquire the lock through `WaitForSingleObject`.
87 // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
89 // A return value of `WAIT_ABANDONED` means that the previous holder of
90 // the thread exited without calling `ReleaseMutex`. This can happen,
91 // for example, when the compiler crashes or is interrupted via ctrl-c
92 // or the like. In this case, however, we are still transferred
93 // ownership of the lock so we continue.
95 // If an error happens.. well... that's surprising!
96 match WaitForSingleObject(mutex.0, INFINITE) {
97 WAIT_OBJECT_0 | WAIT_ABANDONED => {}
99 panic!("WaitForSingleObject failed on global mutex named \
100 `{}`: {} (ret={:x})",
102 io::Error::last_os_error(),
107 // Return a guard which will call `ReleaseMutex` when dropped.
108 Box::new(Guard(mutex))
113 pub fn acquire_global_lock(_name: &str) -> Box<dyn Any> {