From 44ca713da1c322e957aca2aff77c8995e2c6459b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Jul 2020 16:13:06 +0200 Subject: [PATCH] Make salsa buildable on power rust-analyzer fails in rust-lang/rust repo on the power target, because it doesn't have `AtomicU64`. So, lets just use `AtomicUsize` as a revision number. Semantically this is wrong, as we are not using revision to address arrays, but, practically, it's a nice compromise to build on targets without AtomicU64 and to have large revision numbers on most relevant targets. --- src/revision.rs | 26 +++++++++++++------------- src/runtime.rs | 10 +++++----- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/revision.rs b/src/revision.rs index ca7bb108..d1f6a3f9 100644 --- a/src/revision.rs +++ b/src/revision.rs @@ -1,9 +1,9 @@ -use std::num::NonZeroU64; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::num::NonZeroUsize; +use std::sync::atomic::{AtomicUsize, Ordering}; /// Value of the initial revision, as a u64. We don't use 0 -/// because we want to use a `NonZeroU64`. -const START_U64: u64 = 1; +/// because we want to use a `NonZeroUsize`. +const START: usize = 1; /// A unique identifier for the current version of the database; each /// time an input is changed, the revision number is incremented. @@ -12,17 +12,17 @@ const START_U64: u64 = 1; /// directly as a user of salsa. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Revision { - generation: NonZeroU64, + generation: NonZeroUsize, } impl Revision { pub(crate) fn start() -> Self { - Self::from(START_U64) + Self::from(START) } - pub(crate) fn from(g: u64) -> Self { + pub(crate) fn from(g: usize) -> Self { Self { - generation: NonZeroU64::new(g).unwrap(), + generation: NonZeroUsize::new(g).unwrap(), } } @@ -30,7 +30,7 @@ impl Revision { Self::from(self.generation.get() + 1) } - fn as_u64(self) -> u64 { + fn as_usize(self) -> usize { self.generation.get() } } @@ -43,13 +43,13 @@ impl std::fmt::Debug for Revision { #[derive(Debug)] pub(crate) struct AtomicRevision { - data: AtomicU64, + data: AtomicUsize, } impl AtomicRevision { pub(crate) fn start() -> Self { Self { - data: AtomicU64::new(START_U64), + data: AtomicUsize::new(START), } } @@ -58,13 +58,13 @@ impl AtomicRevision { } pub(crate) fn store(&self, r: Revision) { - self.data.store(r.as_u64(), Ordering::SeqCst); + self.data.store(r.as_usize(), Ordering::SeqCst); } /// Increment by 1, returning previous value. pub(crate) fn fetch_then_increment(&self) -> Revision { let v = self.data.fetch_add(1, Ordering::SeqCst); - assert!(v != u64::max_value(), "revision overflow"); + assert!(v != usize::max_value(), "revision overflow"); Revision::from(v) } } diff --git a/src/runtime.rs b/src/runtime.rs index 362b38f5..68ec675c 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -10,7 +10,7 @@ use parking_lot::{Mutex, RwLock}; use rustc_hash::{FxHashMap, FxHasher}; use smallvec::SmallVec; use std::hash::{BuildHasherDefault, Hash}; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; pub(crate) type FxIndexSet = indexmap::IndexSet>; @@ -558,14 +558,14 @@ struct SharedState { storage: DB::DatabaseStorage, /// Stores the next id to use for a snapshotted runtime (starts at 1). - next_id: AtomicU64, + next_id: AtomicUsize, /// Whenever derived queries are executing, they acquire this lock /// in read mode. Mutating inputs (and thus creating a new /// revision) requires a write lock (thus guaranteeing that no /// derived queries are in progress). Note that this is not needed /// to prevent **race conditions** -- the revision counter itself - /// is stored in an `AtomicU64` so it can be cheaply read + /// is stored in an `AtomicUsize` so it can be cheaply read /// without acquiring the lock. Rather, the `query_lock` is used /// to ensure a higher-level consistency property. query_lock: RwLock<()>, @@ -594,7 +594,7 @@ struct SharedState { impl SharedState { fn with_durabilities(durabilities: usize) -> Self { SharedState { - next_id: AtomicU64::new(1), + next_id: AtomicUsize::new(1), storage: Default::default(), query_lock: Default::default(), revisions: (0..durabilities).map(|_| AtomicRevision::start()).collect(), @@ -715,7 +715,7 @@ impl ActiveQuery { /// complete, its `RuntimeId` may potentially be re-used. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct RuntimeId { - counter: u64, + counter: usize, } #[derive(Clone, Debug)]