1 //! Bindings to acquire a global named lock.
3 //! This is intended to be used to synchronize multiple compiler processes to
4 //! ensure that we can output complete errors without interleaving on Windows.
5 //! Note that this is currently only needed for allowing only one 32-bit MSVC
6 //! linker to execute at once on MSVC hosts, so this is only implemented for
7 //! `cfg(windows)`. Also note that this may not always be used on Windows,
8 //! only when targeting 32-bit MSVC.
10 //! For more information about why this is necessary, see where this is called.
15 pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
16 use std::ffi::CString;
19 use winapi::shared::ntdef::HANDLE;
20 use winapi::um::handleapi::CloseHandle;
21 use winapi::um::synchapi::{CreateMutexA, ReleaseMutex, WaitForSingleObject};
22 use winapi::um::winbase::{INFINITE, WAIT_ABANDONED, WAIT_OBJECT_0};
24 struct Handle(HANDLE);
26 impl Drop for Handle {
39 ReleaseMutex((self.0).0);
44 let cname = CString::new(name).unwrap();
46 // Create a named mutex, with no security attributes and also not
47 // acquired when we create it.
49 // This will silently create one if it doesn't already exist, or it'll
50 // open up a handle to one if it already exists.
51 let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr());
54 "failed to create global mutex named `{}`: {}",
56 io::Error::last_os_error()
59 let mutex = Handle(mutex);
61 // Acquire the lock through `WaitForSingleObject`.
63 // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
65 // A return value of `WAIT_ABANDONED` means that the previous holder of
66 // the thread exited without calling `ReleaseMutex`. This can happen,
67 // for example, when the compiler crashes or is interrupted via ctrl-c
68 // or the like. In this case, however, we are still transferred
69 // ownership of the lock so we continue.
71 // If an error happens.. well... that's surprising!
72 match WaitForSingleObject(mutex.0, INFINITE) {
73 WAIT_OBJECT_0 | WAIT_ABANDONED => {}
76 "WaitForSingleObject failed on global mutex named \
79 io::Error::last_os_error(),
85 // Return a guard which will call `ReleaseMutex` when dropped.
86 Box::new(Guard(mutex))
91 pub fn acquire_global_lock(_name: &str) -> Box<dyn Any> {