swc_common/errors/lock.rs
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.
4//
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.
10
11//! Bindings to acquire a global named lock.
12//!
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.
19//!
20//! For more information about why this is necessary, see where this is called.
21
22use std::any::Any;
23
24#[cfg(windows)]
25#[allow(nonstandard_style)]
26pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
27 use std::{ffi::CString, io};
28
29 type LPSECURITY_ATTRIBUTES = *mut u8;
30 #[allow(clippy::upper_case_acronyms)]
31 type BOOL = i32;
32 #[allow(clippy::upper_case_acronyms)]
33 type LPCSTR = *const u8;
34 #[allow(clippy::upper_case_acronyms)]
35 type HANDLE = *mut u8;
36 #[allow(clippy::upper_case_acronyms)]
37 type DWORD = u32;
38
39 const INFINITE: DWORD = !0;
40 const WAIT_OBJECT_0: DWORD = 0;
41 const WAIT_ABANDONED: DWORD = 0x00000080;
42
43 extern "system" {
44 fn CreateMutexA(
45 lpMutexAttributes: LPSECURITY_ATTRIBUTES,
46 bInitialOwner: BOOL,
47 lpName: LPCSTR,
48 ) -> HANDLE;
49 fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
50 fn ReleaseMutex(hMutex: HANDLE) -> BOOL;
51 fn CloseHandle(hObject: HANDLE) -> BOOL;
52 }
53
54 struct Handle(HANDLE);
55
56 impl Drop for Handle {
57 fn drop(&mut self) {
58 unsafe {
59 CloseHandle(self.0);
60 }
61 }
62 }
63
64 struct Guard(Handle);
65
66 impl Drop for Guard {
67 fn drop(&mut self) {
68 unsafe {
69 ReleaseMutex((self.0).0);
70 }
71 }
72 }
73
74 let cname = CString::new(name).unwrap();
75 unsafe {
76 // Create a named mutex, with no security attributes and also not
77 // acquired when we create it.
78 //
79 // This will silently create one if it doesn't already exist, or it'll
80 // open up a handle to one if it already exists.
81 let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr() as *const u8);
82 if mutex.is_null() {
83 panic!(
84 "failed to create global mutex named `{}`: {}",
85 name,
86 io::Error::last_os_error()
87 );
88 }
89 let mutex = Handle(mutex);
90
91 // Acquire the lock through `WaitForSingleObject`.
92 //
93 // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
94 //
95 // A return value of `WAIT_ABANDONED` means that the previous holder of
96 // the thread exited without calling `ReleaseMutex`. This can happen,
97 // for example, when the compiler crashes or is interrupted via ctrl-c
98 // or the like. In this case, however, we are still transferred
99 // ownership of the lock so we continue.
100 //
101 // If an error happens.. well... that's surprising!
102 match WaitForSingleObject(mutex.0, INFINITE) {
103 WAIT_OBJECT_0 | WAIT_ABANDONED => {}
104 code => {
105 panic!(
106 "WaitForSingleObject failed on global mutex named `{}`: {} (ret={:x})",
107 name,
108 io::Error::last_os_error(),
109 code
110 );
111 }
112 }
113
114 // Return a guard which will call `ReleaseMutex` when dropped.
115 Box::new(Guard(mutex))
116 }
117}
118
119#[cfg(not(windows))]
120pub fn acquire_global_lock(_name: &str) -> Box<dyn Any> {
121 Box::new(())
122}