From b16f723d71faf52e226dfbf08f2a48908397459b Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Mon, 5 Feb 2024 09:25:17 +0100 Subject: Upgrade parking_lot_core to 0.9.9 This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update external/rust/crates/parking_lot_core For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md Test: TreeHugger Change-Id: Iee6f8fa3504a9619fbc56e6820c336b50ca25bca --- .cargo_vcs_info.json | 2 +- Android.bp | 4 +-- Cargo.toml | 18 ++++++------- Cargo.toml.orig | 15 ++++++----- METADATA | 23 ++++++++--------- src/parking_lot.rs | 4 +-- src/thread_parker/unix.rs | 8 +++--- src/thread_parker/windows/bindings.rs | 31 +++++++++++++++++++++++ src/thread_parker/windows/keyed_event.rs | 10 ++------ src/thread_parker/windows/mod.rs | 43 +++++++++++--------------------- src/thread_parker/windows/waitaddress.rs | 36 ++++++++++---------------- 11 files changed, 96 insertions(+), 98 deletions(-) create mode 100644 src/thread_parker/windows/bindings.rs diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index bba71f7..a94875d 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "f549eacc36168db632f63731c701ac0521b58e50" + "sha1": "8d92826bdcc8f7a507a2803ecbe5d98747f1df34" }, "path_in_vcs": "core" } \ No newline at end of file diff --git a/Android.bp b/Android.bp index 966a86f..2a47174 100644 --- a/Android.bp +++ b/Android.bp @@ -44,7 +44,7 @@ rust_library { host_supported: true, crate_name: "parking_lot_core", cargo_env_compat: true, - cargo_pkg_version: "0.9.7", + cargo_pkg_version: "0.9.9", srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ @@ -67,7 +67,7 @@ rust_test { host_supported: true, crate_name: "parking_lot_core", cargo_env_compat: true, - cargo_pkg_version: "0.9.7", + cargo_pkg_version: "0.9.9", srcs: ["src/lib.rs"], test_suites: ["general-tests"], auto_gen_config: true, diff --git a/Cargo.toml b/Cargo.toml index 361186b..83d9f23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,9 @@ [package] edition = "2018" +rust-version = "1.49.0" name = "parking_lot_core" -version = "0.9.7" +version = "0.9.9" authors = ["Amanieu d'Antras "] description = "An advanced API for creating custom synchronization primitives." keywords = [ @@ -26,6 +27,9 @@ categories = ["concurrency"] license = "MIT OR Apache-2.0" repository = "https://github.com/Amanieu/parking_lot" +[package.metadata.docs.rs] +rustdoc-args = ["--generate-link-to-definition"] + [dependencies.backtrace] version = "0.3.60" optional = true @@ -53,16 +57,10 @@ deadlock_detection = [ nightly = [] [target."cfg(target_os = \"redox\")".dependencies.redox_syscall] -version = "0.2.8" +version = "0.4" [target."cfg(unix)".dependencies.libc] version = "0.2.95" -[target."cfg(windows)".dependencies.windows-sys] -version = "0.45.0" -features = [ - "Win32_Foundation", - "Win32_System_LibraryLoader", - "Win32_System_SystemServices", - "Win32_System_WindowsProgramming", -] +[target."cfg(windows)".dependencies.windows-targets] +version = "0.48.0" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index c1fd530..a899fc5 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "parking_lot_core" -version = "0.9.7" +version = "0.9.9" authors = ["Amanieu d'Antras "] description = "An advanced API for creating custom synchronization primitives." license = "MIT OR Apache-2.0" @@ -8,6 +8,10 @@ repository = "https://github.com/Amanieu/parking_lot" keywords = ["mutex", "condvar", "rwlock", "once", "thread"] categories = ["concurrency"] edition = "2018" +rust-version = "1.49.0" + +[package.metadata.docs.rs] +rustdoc-args = ["--generate-link-to-definition"] [dependencies] cfg-if = "1.0.0" @@ -20,15 +24,10 @@ backtrace = { version = "0.3.60", optional = true } libc = "0.2.95" [target.'cfg(target_os = "redox")'.dependencies] -redox_syscall = "0.2.8" +redox_syscall = "0.4" [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.45.0", features = [ - "Win32_Foundation", - "Win32_System_LibraryLoader", - "Win32_System_SystemServices", - "Win32_System_WindowsProgramming", -] } +windows-targets = "0.48.0" [features] nightly = [] diff --git a/METADATA b/METADATA index 7d7a997..ae5b535 100644 --- a/METADATA +++ b/METADATA @@ -1,23 +1,20 @@ # This project was upgraded with external_updater. -# Usage: tools/external_updater/updater.sh update rust/crates/parking_lot_core -# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md +# Usage: tools/external_updater/updater.sh update external/rust/crates/parking_lot_core +# For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md name: "parking_lot_core" description: "An advanced API for creating custom synchronization primitives." third_party { - url { - type: HOMEPAGE - value: "https://crates.io/crates/parking_lot_core" - } - url { - type: ARCHIVE - value: "https://static.crates.io/crates/parking_lot_core/parking_lot_core-0.9.7.crate" - } - version: "0.9.7" license_type: NOTICE last_upgrade_date { - year: 2023 + year: 2024 month: 2 - day: 3 + day: 5 + } + homepage: "https://crates.io/crates/parking_lot_core" + identifier { + type: "Archive" + value: "https://static.crates.io/crates/parking_lot_core/parking_lot_core-0.9.9.crate" + version: "0.9.9" } } diff --git a/src/parking_lot.rs b/src/parking_lot.rs index b388619..d74b439 100644 --- a/src/parking_lot.rs +++ b/src/parking_lot.rs @@ -248,7 +248,7 @@ fn create_hashtable() -> &'static HashTable { // Free the table we created // SAFETY: `new_table` is created from `Box::into_raw` above and only freed here. unsafe { - Box::from_raw(new_table); + let _ = Box::from_raw(new_table); } old_table } @@ -1476,7 +1476,7 @@ mod tests { test! { unpark_all_one_fast( - repeats: 10000, latches: 1, delay: 0, threads: 1, single_unparks: 0 + repeats: 1000, latches: 1, delay: 0, threads: 1, single_unparks: 0 ); unpark_all_hundred_fast( repeats: 100, latches: 1, delay: 0, threads: 100, single_unparks: 0 diff --git a/src/thread_parker/unix.rs b/src/thread_parker/unix.rs index 7f28603..fdc1ed9 100644 --- a/src/thread_parker/unix.rs +++ b/src/thread_parker/unix.rs @@ -5,7 +5,7 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. -#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))] +#[cfg(any(target_os = "macos", target_os = "tvos", target_os = "ios", target_os = "watchos"))] use core::ptr; use core::{ cell::{Cell, UnsafeCell}, @@ -130,6 +130,7 @@ impl ThreadParker { #[cfg(any( target_os = "macos", target_os = "ios", + target_os = "tvos", target_os = "watchos", target_os = "android", target_os = "espidf" @@ -141,6 +142,7 @@ impl ThreadParker { #[cfg(not(any( target_os = "macos", target_os = "ios", + target_os = "tvos", target_os = "watchos", target_os = "android", target_os = "espidf" @@ -195,7 +197,7 @@ impl super::UnparkHandleT for UnparkHandle { } // Returns the current time on the clock used by pthread_cond_t as a timespec. -#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))] +#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))] #[inline] fn timespec_now() -> libc::timespec { let mut now = MaybeUninit::::uninit(); @@ -208,7 +210,7 @@ fn timespec_now() -> libc::timespec { tv_nsec: now.tv_usec as tv_nsec_t * 1000, } } -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))] +#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos")))] #[inline] fn timespec_now() -> libc::timespec { let mut now = MaybeUninit::::uninit(); diff --git a/src/thread_parker/windows/bindings.rs b/src/thread_parker/windows/bindings.rs new file mode 100644 index 0000000..3c37dfa --- /dev/null +++ b/src/thread_parker/windows/bindings.rs @@ -0,0 +1,31 @@ +//! Manual bindings to the win32 API to avoid dependencies on windows-sys or winapi +//! as these bindings will **never** change and parking_lot_core is a foundational +//! dependency for the Rust ecosystem, so the dependencies used by it have an +//! outsize affect + +pub const INFINITE: u32 = 4294967295; +pub const ERROR_TIMEOUT: u32 = 1460; +pub const GENERIC_READ: u32 = 2147483648; +pub const GENERIC_WRITE: u32 = 1073741824; +pub const STATUS_SUCCESS: i32 = 0; +pub const STATUS_TIMEOUT: i32 = 258; + +pub type HANDLE = isize; +pub type HINSTANCE = isize; +pub type BOOL = i32; +pub type BOOLEAN = u8; +pub type NTSTATUS = i32; +pub type FARPROC = Option isize>; +pub type WaitOnAddress = unsafe extern "system" fn( + Address: *const std::ffi::c_void, + CompareAddress: *const std::ffi::c_void, + AddressSize: usize, + dwMilliseconds: u32, +) -> BOOL; +pub type WakeByAddressSingle = unsafe extern "system" fn(Address: *const std::ffi::c_void); + +windows_targets::link!("kernel32.dll" "system" fn GetLastError() -> u32); +windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hObject: HANDLE) -> BOOL); +windows_targets::link!("kernel32.dll" "system" fn GetModuleHandleA(lpModuleName: *const u8) -> HINSTANCE); +windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hModule: HINSTANCE, lpProcName: *const u8) -> FARPROC); +windows_targets::link!("kernel32.dll" "system" fn Sleep(dwMilliseconds: u32) -> ()); diff --git a/src/thread_parker/windows/keyed_event.rs b/src/thread_parker/windows/keyed_event.rs index 302bab5..84a71d0 100644 --- a/src/thread_parker/windows/keyed_event.rs +++ b/src/thread_parker/windows/keyed_event.rs @@ -13,18 +13,12 @@ use core::{ use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::Instant; -use windows_sys::Win32::{ - Foundation::{CloseHandle, BOOLEAN, HANDLE, NTSTATUS, STATUS_SUCCESS, STATUS_TIMEOUT}, - System::{ - LibraryLoader::{GetModuleHandleA, GetProcAddress}, - SystemServices::{GENERIC_READ, GENERIC_WRITE}, - }, -}; - const STATE_UNPARKED: usize = 0; const STATE_PARKED: usize = 1; const STATE_TIMED_OUT: usize = 2; +use super::bindings::*; + #[allow(non_snake_case)] pub struct KeyedEvent { handle: HANDLE, diff --git a/src/thread_parker/windows/mod.rs b/src/thread_parker/windows/mod.rs index 1f5ed23..27028a1 100644 --- a/src/thread_parker/windows/mod.rs +++ b/src/thread_parker/windows/mod.rs @@ -11,6 +11,7 @@ use core::{ }; use std::time::Instant; +mod bindings; mod keyed_event; mod waitaddress; @@ -60,7 +61,7 @@ impl Backend { Err(global_backend_ptr) => { unsafe { // We lost the race, free our object and return the global one - Box::from_raw(backend_ptr); + let _ = Box::from_raw(backend_ptr); &*global_backend_ptr } } @@ -74,11 +75,13 @@ pub struct ThreadParker { backend: &'static Backend, } -impl ThreadParker { - pub const IS_CHEAP_TO_CONSTRUCT: bool = true; +impl super::ThreadParkerT for ThreadParker { + type UnparkHandle = UnparkHandle; + + const IS_CHEAP_TO_CONSTRUCT: bool = true; #[inline] - pub fn new() -> ThreadParker { + fn new() -> ThreadParker { // Initialize the backend here to ensure we don't get any panics // later on, which could leave synchronization primitives in a broken // state. @@ -90,7 +93,7 @@ impl ThreadParker { // Prepares the parker. This should be called before adding it to the queue. #[inline] - pub fn prepare_park(&self) { + unsafe fn prepare_park(&self) { match *self.backend { Backend::KeyedEvent(ref x) => x.prepare_park(&self.key), Backend::WaitAddress(ref x) => x.prepare_park(&self.key), @@ -100,7 +103,7 @@ impl ThreadParker { // Checks if the park timed out. This should be called while holding the // queue lock after park_until has returned false. #[inline] - pub fn timed_out(&self) -> bool { + unsafe fn timed_out(&self) -> bool { match *self.backend { Backend::KeyedEvent(ref x) => x.timed_out(&self.key), Backend::WaitAddress(ref x) => x.timed_out(&self.key), @@ -110,7 +113,7 @@ impl ThreadParker { // Parks the thread until it is unparked. This should be called after it has // been added to the queue, after unlocking the queue. #[inline] - pub unsafe fn park(&self) { + unsafe fn park(&self) { match *self.backend { Backend::KeyedEvent(ref x) => x.park(&self.key), Backend::WaitAddress(ref x) => x.park(&self.key), @@ -121,7 +124,7 @@ impl ThreadParker { // should be called after it has been added to the queue, after unlocking // the queue. Returns true if we were unparked and false if we timed out. #[inline] - pub unsafe fn park_until(&self, timeout: Instant) -> bool { + unsafe fn park_until(&self, timeout: Instant) -> bool { match *self.backend { Backend::KeyedEvent(ref x) => x.park_until(&self.key, timeout), Backend::WaitAddress(ref x) => x.park_until(&self.key, timeout), @@ -132,7 +135,7 @@ impl ThreadParker { // necessary to ensure that thread-local ThreadData objects remain valid. // This should be called while holding the queue lock. #[inline] - pub unsafe fn unpark_lock(&self) -> UnparkHandle { + unsafe fn unpark_lock(&self) -> UnparkHandle { match *self.backend { Backend::KeyedEvent(ref x) => UnparkHandle::KeyedEvent(x.unpark_lock(&self.key)), Backend::WaitAddress(ref x) => UnparkHandle::WaitAddress(x.unpark_lock(&self.key)), @@ -148,11 +151,11 @@ pub enum UnparkHandle { WaitAddress(waitaddress::UnparkHandle), } -impl UnparkHandle { +impl super::UnparkHandleT for UnparkHandle { // Wakes up the parked thread. This should be called after the queue lock is // released to avoid blocking the queue for too long. #[inline] - pub unsafe fn unpark(self) { + unsafe fn unpark(self) { match self { UnparkHandle::KeyedEvent(x) => x.unpark(), UnparkHandle::WaitAddress(x) => x.unpark(), @@ -163,26 +166,10 @@ impl UnparkHandle { // Yields the rest of the current timeslice to the OS #[inline] pub fn thread_yield() { - // Note that this is manually defined here rather than using the definition - // through `winapi`. The `winapi` definition comes from the `synchapi` - // header which enables the "synchronization.lib" library. It turns out, - // however that `Sleep` comes from `kernel32.dll` so this activation isn't - // necessary. - // - // This was originally identified in rust-lang/rust where on MinGW the - // libsynchronization.a library pulls in a dependency on a newer DLL not - // present in older versions of Windows. (see rust-lang/rust#49438) - // - // This is a bit of a hack for now and ideally we'd fix MinGW's own import - // libraries, but that'll probably take a lot longer than patching this here - // and avoiding the `synchapi` feature entirely. - extern "system" { - fn Sleep(a: u32); - } unsafe { // We don't use SwitchToThread here because it doesn't consider all // threads in the system and the thread we are waiting for may not get // selected. - Sleep(0); + bindings::Sleep(0); } } diff --git a/src/thread_parker/windows/waitaddress.rs b/src/thread_parker/windows/waitaddress.rs index ef6cb44..41df45f 100644 --- a/src/thread_parker/windows/waitaddress.rs +++ b/src/thread_parker/windows/waitaddress.rs @@ -10,30 +10,18 @@ use core::{ sync::atomic::{AtomicUsize, Ordering}, }; use std::{ffi, time::Instant}; -use windows_sys::Win32::{ - Foundation::{GetLastError, BOOL, ERROR_TIMEOUT}, - System::{ - LibraryLoader::{GetModuleHandleA, GetProcAddress}, - WindowsProgramming::INFINITE, - }, -}; + +use super::bindings::*; #[allow(non_snake_case)] pub struct WaitAddress { - WaitOnAddress: extern "system" fn( - Address: *mut ffi::c_void, - CompareAddress: *mut ffi::c_void, - AddressSize: usize, - dwMilliseconds: u32, - ) -> BOOL, - WakeByAddressSingle: extern "system" fn(Address: *mut ffi::c_void), + WaitOnAddress: WaitOnAddress, + WakeByAddressSingle: WakeByAddressSingle, } impl WaitAddress { #[allow(non_snake_case)] pub fn create() -> Option { - // MSDN claims that that WaitOnAddress and WakeByAddressSingle are - // located in kernel32.dll, but they are lying... let synch_dll = unsafe { GetModuleHandleA(b"api-ms-win-core-synch-l1-2-0.dll\0".as_ptr()) }; if synch_dll == 0 { return None; @@ -108,12 +96,14 @@ impl WaitAddress { #[inline] fn wait_on_address(&'static self, key: &AtomicUsize, timeout: u32) -> BOOL { let cmp = 1usize; - (self.WaitOnAddress)( - key as *const _ as *mut ffi::c_void, - &cmp as *const _ as *mut ffi::c_void, - mem::size_of::(), - timeout, - ) + unsafe { + (self.WaitOnAddress)( + key as *const _ as *mut ffi::c_void, + &cmp as *const _ as *mut ffi::c_void, + mem::size_of::(), + timeout, + ) + } } } @@ -130,6 +120,6 @@ impl UnparkHandle { // released to avoid blocking the queue for too long. #[inline] pub fn unpark(self) { - (self.waitaddress.WakeByAddressSingle)(self.key as *mut ffi::c_void); + unsafe { (self.waitaddress.WakeByAddressSingle)(self.key as *mut ffi::c_void) }; } } -- cgit v1.2.3