]>
git.proxmox.com Git - rustc.git/blob - src/librustc_errors/lock.rs
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 #[allow(nonstandard_style)]
16 pub fn acquire_global_lock(name
: &str) -> Box
<dyn Any
> {
17 use std
::ffi
::CString
;
20 type LPSECURITY_ATTRIBUTES
= *mut u8;
22 type LPCSTR
= *const u8;
23 type HANDLE
= *mut u8;
26 const INFINITE
: DWORD
= !0;
27 const WAIT_OBJECT_0
: DWORD
= 0;
28 const WAIT_ABANDONED
: DWORD
= 0x00000080;
31 fn CreateMutexA(lpMutexAttributes
: LPSECURITY_ATTRIBUTES
,
35 fn WaitForSingleObject(hHandle
: HANDLE
, dwMilliseconds
: DWORD
) -> DWORD
;
36 fn ReleaseMutex(hMutex
: HANDLE
) -> BOOL
;
37 fn CloseHandle(hObject
: HANDLE
) -> BOOL
;
40 struct Handle(HANDLE
);
42 impl Drop
for Handle
{
55 ReleaseMutex((self.0).0);
60 let cname
= CString
::new(name
).unwrap();
62 // Create a named mutex, with no security attributes and also not
63 // acquired when we create it.
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);
69 panic
!("failed to create global mutex named `{}`: {}",
71 io
::Error
::last_os_error());
73 let mutex
= Handle(mutex
);
75 // Acquire the lock through `WaitForSingleObject`.
77 // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
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.
85 // If an error happens.. well... that's surprising!
86 match WaitForSingleObject(mutex
.0, INFINITE
) {
87 WAIT_OBJECT_0
| WAIT_ABANDONED
=> {}
89 panic
!("WaitForSingleObject failed on global mutex named \
92 io
::Error
::last_os_error(),
97 // Return a guard which will call `ReleaseMutex` when dropped.
98 Box
::new(Guard(mutex
))
103 pub fn acquire_global_lock(_name
: &str) -> Box
<dyn Any
> {