Skip to content

Commit

Permalink
refactor: replace std lib rwlock with parking_lot rwlock
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsan committed Feb 26, 2024
1 parent e5d9585 commit 1660411
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 39 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ itertools = "0.12.1"
log = "0.4.20"
merge = "0.1.0"
miette = { version = "7.1.0", features = ["fancy"] }
parking_lot = { version = "0.12.1", features = ["deadlock_detection"] }
rayon = "1.8.1"
rusqlite = { version = "0.31.0", features = ["bundled", "chrono", "uuid"], optional = true }
serde = "1.0.197"
Expand Down
2 changes: 0 additions & 2 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ pub enum ActivityLogErrorKind {
NoActivityToHold,
/// Failed to unwrap Arc
ArcUnwrapFailed,
/// RwLock locking failed, it has been poisoned
RwLockHasBeenPoisoned,
/// There are no unfinished activities to end
NoUnfinishedActivities,
/// There is no cache to sync
Expand Down
51 changes: 14 additions & 37 deletions crates/core/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::BTreeMap,
sync::{Arc, RwLock},
};
use std::{collections::BTreeMap, sync::Arc};

use parking_lot::RwLock;

use merge::Merge;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
Expand Down Expand Up @@ -67,9 +66,7 @@ impl InMemoryActivityStorage {
///
/// Returns an error if the mutex has been poisoned
pub fn get_activity_log(&self) -> PaceResult<ActivityLog> {
let Ok(activity_log) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activity_log = self.log.read();

Ok(activity_log.clone())
}
Expand All @@ -95,9 +92,7 @@ impl SyncStorage for InMemoryActivityStorage {

impl ActivityReadOps for InMemoryActivityStorage {
fn read_activity(&self, activity_id: ActivityGuid) -> PaceResult<ActivityItem> {
let Ok(activities) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activities = self.log.read();

let activity = activities
.get(&activity_id)
Expand All @@ -110,9 +105,7 @@ impl ActivityReadOps for InMemoryActivityStorage {
}

fn list_activities(&self, filter: ActivityFilter) -> PaceOptResult<FilteredActivities> {
let Ok(activity_log) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activity_log = self.log.read();

let filtered = activity_log
.par_iter()
Expand Down Expand Up @@ -153,9 +146,7 @@ impl ActivityReadOps for InMemoryActivityStorage {

impl ActivityWriteOps for InMemoryActivityStorage {
fn create_activity(&self, activity: Activity) -> PaceResult<ActivityItem> {
let Ok(activities) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activities = self.log.read();

let activity_item = ActivityItem::from(activity.clone());

Expand All @@ -172,9 +163,7 @@ impl ActivityWriteOps for InMemoryActivityStorage {

drop(activities);

let Ok(mut activities) = self.log.write() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let mut activities = self.log.write();

// We don't check for None here, because we know that the ID was not existing in the list of
// activities.
Expand All @@ -193,9 +182,7 @@ impl ActivityWriteOps for InMemoryActivityStorage {
updated_activity: Activity,
_update_opts: UpdateOptions,
) -> PaceResult<ActivityItem> {
let Ok(activities) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activities = self.log.read();

let original_activity = activities
.get(&activity_id)
Expand All @@ -204,9 +191,7 @@ impl ActivityWriteOps for InMemoryActivityStorage {

drop(activities);

let Ok(mut activities) = self.log.write() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let mut activities = self.log.write();

let _ = activities.entry(activity_id).and_modify(|activity| {
activity.merge(updated_activity);
Expand All @@ -222,9 +207,7 @@ impl ActivityWriteOps for InMemoryActivityStorage {
activity_id: ActivityGuid,
_delete_opts: DeleteOptions,
) -> PaceResult<ActivityItem> {
let Ok(mut activities) = self.log.write() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let mut activities = self.log.write();

let activity = activities
.remove(&activity_id)
Expand All @@ -242,9 +225,7 @@ impl ActivityStateManagement for InMemoryActivityStorage {
activity_id: ActivityGuid,
end_opts: EndOptions,
) -> PaceResult<ActivityItem> {
let Ok(mut activities) = self.log.write() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let mut activities = self.log.write();

let _ = activities.entry(activity_id).and_modify(|activity| {
match calculate_duration(activity.begin(), *end_opts.end_time()) {
Expand Down Expand Up @@ -281,9 +262,7 @@ impl ActivityStateManagement for InMemoryActivityStorage {
&self,
end_opts: EndOptions,
) -> PaceOptResult<Vec<ActivityItem>> {
let Ok(activities) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activities = self.log.read();

let active_activities = activities
.par_iter()
Expand Down Expand Up @@ -504,9 +483,7 @@ impl ActivityQuerying for InMemoryActivityStorage {
}

fn list_activities_by_id(&self) -> PaceOptResult<BTreeMap<ActivityGuid, Activity>> {
let Ok(activities) = self.log.read() else {
return Err(ActivityLogErrorKind::RwLockHasBeenPoisoned.into());
};
let activities = self.log.read();

let activities_by_id = activities.activities().clone();

Expand Down

0 comments on commit 1660411

Please sign in to comment.