diff options
author | David LeGare <legare@google.com> | 2022-03-07 08:50:17 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-03-07 08:50:17 +0000 |
commit | 6d6dff10c2010df2767139895bd6eedef76ceb48 (patch) | |
tree | 849d5ded097a262df9792e1bf91ccda3dd0567e0 /src/parking_lot.rs | |
parent | a32358d393493f018088bd8b35d9aeaa0555e459 (diff) | |
parent | 1a57ec67647b32db3871789480ce989115aa4f80 (diff) | |
download | parking_lot_core-6d6dff10c2010df2767139895bd6eedef76ceb48.tar.gz |
Update parking_lot_core to 0.9.1 am: 2aced6505c am: c77e379f53 am: 6dfa086a04 am: 1a57ec6764aml_uwb_331910010aml_uwb_331820070aml_uwb_331613010aml_uwb_331611010aml_uwb_331410010aml_uwb_331310030aml_uwb_331115000aml_uwb_331015040aml_uwb_330810010aml_tz4_332714070aml_tz4_332714050aml_tz4_332714010aml_tz4_331910000aml_tz4_331314030aml_tz4_331314020aml_tz4_331314010aml_tz4_331012050aml_tz4_331012040aml_tz4_331012000aml_ase_331311020aml_ase_331112000aml_ase_331011020android13-mainline-uwb-releaseandroid13-mainline-tzdata4-releaseandroid13-mainline-appsearch-releaseaml_tz4_332714010
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/parking_lot_core/+/2005974
Change-Id: Ia14168b914463168950f3194aadf5a8275780c90
Diffstat (limited to 'src/parking_lot.rs')
-rw-r--r-- | src/parking_lot.rs | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/parking_lot.rs b/src/parking_lot.rs index 519ce9e..9b84525 100644 --- a/src/parking_lot.rs +++ b/src/parking_lot.rs @@ -12,9 +12,33 @@ use core::{ ptr, sync::atomic::{AtomicPtr, AtomicUsize, Ordering}, }; -use instant::Instant; use smallvec::SmallVec; -use std::time::Duration; +use std::time::{Duration, Instant}; + +// Don't use Instant on wasm32-unknown-unknown, it just panics. +cfg_if::cfg_if! { + if #[cfg(all( + target_family = "wasm", + target_os = "unknown", + target_vendor = "unknown" + ))] { + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] + struct TimeoutInstant; + impl TimeoutInstant { + fn now() -> TimeoutInstant { + TimeoutInstant + } + } + impl core::ops::Add<Duration> for TimeoutInstant { + type Output = Self; + fn add(self, _rhs: Duration) -> Self::Output { + TimeoutInstant + } + } + } else { + use std::time::Instant as TimeoutInstant; + } +} static NUM_THREADS: AtomicUsize = AtomicUsize::new(0); @@ -47,7 +71,7 @@ impl HashTable { let new_size = (num_threads * LOAD_FACTOR).next_power_of_two(); let hash_bits = 0usize.leading_zeros() - new_size.leading_zeros() - 1; - let now = Instant::now(); + let now = TimeoutInstant::now(); let mut entries = Vec::with_capacity(new_size); for i in 0..new_size { // We must ensure the seed is not zero @@ -77,7 +101,7 @@ struct Bucket { impl Bucket { #[inline] - pub fn new(timeout: Instant, seed: u32) -> Self { + pub fn new(timeout: TimeoutInstant, seed: u32) -> Self { Self { mutex: WordLock::new(), queue_head: Cell::new(ptr::null()), @@ -89,7 +113,7 @@ impl Bucket { struct FairTimeout { // Next time at which point be_fair should be set - timeout: Instant, + timeout: TimeoutInstant, // the PRNG state for calculating the next timeout seed: u32, @@ -97,14 +121,14 @@ struct FairTimeout { impl FairTimeout { #[inline] - fn new(timeout: Instant, seed: u32) -> FairTimeout { + fn new(timeout: TimeoutInstant, seed: u32) -> FairTimeout { FairTimeout { timeout, seed } } // Determine whether we should force a fair unlock, and update the timeout #[inline] fn should_timeout(&mut self) -> bool { - let now = Instant::now(); + let now = TimeoutInstant::now(); if now > self.timeout { // Time between 0 and 1ms. let nanos = self.gen_u32() % 1_000_000; |