Skip to content

Commit

Permalink
Replace spinning_top with parking_lot when std is available
Browse files Browse the repository at this point in the history
  • Loading branch information
andrisaar committed Mar 20, 2024
1 parent 9802c17 commit b469576
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions oak_functions_service/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ impl DataBuilder {
}
}

#[cfg(feature = "std")]
mod mutexes {
pub use parking_lot::{Mutex, RwLock};
}

#[cfg(not(feature = "std"))]
mod mutexes {
pub use spinning_top::{RwSpinlock as RwLock, Spinlock as Mutex};
}

/// Utility for managing lookup data.
///
/// `LookupDataManager` can be used to create `LookupData` instances that share
Expand All @@ -82,33 +92,21 @@ impl DataBuilder {
/// In the future we may replace both the mutex and the hash map with something
/// like RCU.
pub struct LookupDataManager {
#[cfg(feature = "std")]
data: parking_lot::RwLock<Arc<Data>>,
#[cfg(not(feature = "std"))]
data: spinning_top::RwSpinlock<Arc<Data>>,
data: mutexes::RwLock<Arc<Data>>,
// Behind a lock, because we have multiple references to LookupDataManager and need to mutate
// data builder.
#[cfg(feature = "std")]
data_builder: parking_lot::Mutex<DataBuilder>,
#[cfg(not(feature = "std"))]
data_builder: spinning_top::Spinlock<DataBuilder>,
data_builder: mutexes::Mutex<DataBuilder>,
logger: Arc<dyn OakLogger>,
}

impl LookupDataManager {
/// Creates a new instance with empty backing data.
pub fn new_empty(logger: Arc<dyn OakLogger>) -> Self {
Self {
#[cfg(feature = "std")]
data: parking_lot::RwLock::new(Arc::new(Data::default())),
#[cfg(not(feature = "std"))]
data: spinning_top::RwSpinlock::new(Arc::new(Data::default())),
data: mutexes::RwLock::new(Arc::new(Data::default())),
// Incrementally builds the backing data that will be used by new `LookupData`
// instances when finished.
#[cfg(feature = "std")]
data_builder: parking_lot::Mutex::new(DataBuilder::default()),
#[cfg(not(feature = "std"))]
data_builder: spinning_top::Spinlock::new(DataBuilder::default()),
data_builder: mutexes::Mutex::new(DataBuilder::default()),
logger,
}
}
Expand Down

0 comments on commit b469576

Please sign in to comment.