summaryrefslogtreecommitdiff
path: root/src/parking_lot.rs
diff options
context:
space:
mode:
authorDavid LeGare <legare@google.com>2022-03-07 08:50:17 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-03-07 08:50:17 +0000
commit6d6dff10c2010df2767139895bd6eedef76ceb48 (patch)
tree849d5ded097a262df9792e1bf91ccda3dd0567e0 /src/parking_lot.rs
parenta32358d393493f018088bd8b35d9aeaa0555e459 (diff)
parent1a57ec67647b32db3871789480ce989115aa4f80 (diff)
downloadparking_lot_core-6d6dff10c2010df2767139895bd6eedef76ceb48.tar.gz
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.rs38
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;