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