summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_errors/src/lock.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
commit1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch)
tree3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /compiler/rustc_errors/src/lock.rs
parentReleasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz
rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_errors/src/lock.rs')
-rw-r--r--compiler/rustc_errors/src/lock.rs81
1 files changed, 36 insertions, 45 deletions
diff --git a/compiler/rustc_errors/src/lock.rs b/compiler/rustc_errors/src/lock.rs
index a73472021..7db262abf 100644
--- a/compiler/rustc_errors/src/lock.rs
+++ b/compiler/rustc_errors/src/lock.rs
@@ -16,10 +16,12 @@ pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
use std::ffi::CString;
use std::io;
- use winapi::shared::ntdef::HANDLE;
- use winapi::um::handleapi::CloseHandle;
- use winapi::um::synchapi::{CreateMutexA, ReleaseMutex, WaitForSingleObject};
- use winapi::um::winbase::{INFINITE, WAIT_ABANDONED, WAIT_OBJECT_0};
+ use windows::{
+ core::PCSTR,
+ Win32::Foundation::{CloseHandle, HANDLE, WAIT_ABANDONED, WAIT_OBJECT_0},
+ Win32::System::Threading::{CreateMutexA, ReleaseMutex, WaitForSingleObject},
+ Win32::System::WindowsProgramming::INFINITE,
+ };
struct Handle(HANDLE);
@@ -42,49 +44,38 @@ pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
}
let cname = CString::new(name).unwrap();
- unsafe {
- // Create a named mutex, with no security attributes and also not
- // acquired when we create it.
- //
- // This will silently create one if it doesn't already exist, or it'll
- // open up a handle to one if it already exists.
- let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr());
- if mutex.is_null() {
- panic!(
- "failed to create global mutex named `{}`: {}",
- name,
- io::Error::last_os_error()
- );
- }
- let mutex = Handle(mutex);
+ // Create a named mutex, with no security attributes and also not
+ // acquired when we create it.
+ //
+ // This will silently create one if it doesn't already exist, or it'll
+ // open up a handle to one if it already exists.
+ let mutex = unsafe { CreateMutexA(None, false, PCSTR::from_raw(cname.as_ptr().cast())) }
+ .unwrap_or_else(|_| panic!("failed to create global mutex named `{}`", name));
+ let mutex = Handle(mutex);
- // Acquire the lock through `WaitForSingleObject`.
- //
- // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
- //
- // A return value of `WAIT_ABANDONED` means that the previous holder of
- // the thread exited without calling `ReleaseMutex`. This can happen,
- // for example, when the compiler crashes or is interrupted via ctrl-c
- // or the like. In this case, however, we are still transferred
- // ownership of the lock so we continue.
- //
- // If an error happens.. well... that's surprising!
- match WaitForSingleObject(mutex.0, INFINITE) {
- WAIT_OBJECT_0 | WAIT_ABANDONED => {}
- code => {
- panic!(
- "WaitForSingleObject failed on global mutex named \
- `{}`: {} (ret={:x})",
- name,
- io::Error::last_os_error(),
- code
- );
- }
- }
-
- // Return a guard which will call `ReleaseMutex` when dropped.
- Box::new(Guard(mutex))
+ // Acquire the lock through `WaitForSingleObject`.
+ //
+ // A return value of `WAIT_OBJECT_0` means we successfully acquired it.
+ //
+ // A return value of `WAIT_ABANDONED` means that the previous holder of
+ // the thread exited without calling `ReleaseMutex`. This can happen,
+ // for example, when the compiler crashes or is interrupted via ctrl-c
+ // or the like. In this case, however, we are still transferred
+ // ownership of the lock so we continue.
+ //
+ // If an error happens.. well... that's surprising!
+ match unsafe { WaitForSingleObject(mutex.0, INFINITE) } {
+ WAIT_OBJECT_0 | WAIT_ABANDONED => (),
+ err => panic!(
+ "WaitForSingleObject failed on global mutex named `{}`: {} (ret={:x})",
+ name,
+ io::Error::last_os_error(),
+ err.0
+ ),
}
+
+ // Return a guard which will call `ReleaseMutex` when dropped.
+ Box::new(Guard(mutex))
}
#[cfg(not(windows))]