Skip to content

Commit

Permalink
Migrate to parking_lot
Browse files Browse the repository at this point in the history
Migrate from std implementations of Mutex and RwLock to the parking_lot
implementations. They are faster and more flexible than the those found
in the Rust standard library.
  • Loading branch information
StefanBossbaly committed Apr 7, 2024
1 parent 2c1a47f commit 6876b96
Show file tree
Hide file tree
Showing 33 changed files with 421 additions and 337 deletions.
22 changes: 11 additions & 11 deletions core/launcher/src/manager/app_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use std::{
collections::HashMap,
sync::{Arc, RwLock},
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};

Expand Down Expand Up @@ -45,6 +45,7 @@ use ripple_sdk::{
extn::extn_client_message::ExtnResponse,
framework::ripple_contract::RippleContract,
log::{debug, error, info, warn},
parking_lot::RwLock,
tokio::{
self,
time::{sleep, Duration},
Expand Down Expand Up @@ -117,7 +118,6 @@ impl AppLauncherState {
Some(t) => self
.apps
.read()
.unwrap()
.iter()
.filter(|(_app_id, app)| app.launch_params._type.eq(&t))
.count(),
Expand All @@ -129,11 +129,11 @@ impl AppLauncherState {
}

fn get_app_len(&self) -> usize {
self.apps.read().unwrap().len()
self.apps.read().len()
}

fn get_app_by_id(&self, app_id: &str) -> Option<App> {
let v = self.apps.read().unwrap();
let v = self.apps.read();
let r = v.get(app_id);
if let Some(r) = r {
let v = r.clone();
Expand All @@ -143,47 +143,47 @@ impl AppLauncherState {
}

fn get_apps(&self) -> Vec<App> {
let r = self.apps.read().unwrap();
let r = self.apps.read();
r.iter().map(|(_s, app)| app.clone()).collect()
}

fn set_app_state(&self, container_id: &str, lifecycle_state: LifecycleState) {
let mut v = self.apps.write().unwrap();
let mut v = self.apps.write();
let r = v.get_mut(container_id);
if let Some(r) = r {
r.state = lifecycle_state
}
}

fn set_app_ready(&self, app_id: &str) {
let mut v = self.apps.write().unwrap();
let mut v = self.apps.write();
let r = v.get_mut(app_id);
if let Some(r) = r {
r.ready = true;
}
}

fn set_app_viewid(&self, container_id: &str, view_id: Uuid) {
let mut v = self.apps.write().unwrap();
let mut v = self.apps.write();
let r = v.get_mut(container_id);
if let Some(r) = r {
r.container_props.view_id = view_id
}
}

fn add_app(&self, key: String, app: App) {
let mut r = self.apps.write().unwrap();
let mut r = self.apps.write();
r.insert(key, app);
}

fn remove_app(&self, key: &str) -> Option<App> {
let mut r = self.apps.write().unwrap();
let mut r = self.apps.write();
r.remove(key)
}

fn always_retained_apps(&self, policy: RetentionPolicy) -> Vec<App> {
let mut candidates = Vec::new();
for (_id, app) in self.apps.read().unwrap().iter() {
for (_id, app) in self.apps.read().iter() {
if policy.always_retained.contains(&app.app_id) {
continue;
}
Expand Down
26 changes: 12 additions & 14 deletions core/launcher/src/manager/container_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use std::{collections::HashMap, sync::Arc};

use ripple_sdk::{
api::{
apps::{Dimensions, StateChange, ViewId},
firebolt::fb_lifecycle::LifecycleState,
},
log::{debug, error},
parking_lot::RwLock,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -63,56 +61,56 @@ pub struct ContainerState {
impl ContainerState {
fn get_prev_stack(&self) -> Option<String> {
let prev_container = {
let stack = self.stack.read().unwrap();
let stack = self.stack.read();
stack.peek().cloned()
};
prev_container
}

fn get_container_by_name(&self, id: &String) -> Option<ContainerProperties> {
{
let container = self.containers.read().unwrap();
let container = self.containers.read();
container.get(id).cloned()
}
}

fn add_container(&self, k: String, v: ContainerProperties) {
let mut containers = self.containers.write().unwrap();
let mut containers = self.containers.write();
containers.insert(k, v);
}

fn remove_container(&self, k: String) {
let mut containers = self.containers.write().unwrap();
let mut containers = self.containers.write();
containers.remove(&k);
}

fn contains_stack_by_name(&self, id: &String) -> bool {
let mut stack = self.stack.write().unwrap();
let mut stack = self.stack.write();
stack.contains(id)
}

fn stack_len(&self) -> usize {
let stack = self.stack.write().unwrap();
let stack = self.stack.write();
stack.len()
}

fn add_stack(&self, id: String) {
let mut stack = self.stack.write().unwrap();
let mut stack = self.stack.write();
stack.push(id);
}

fn pop_stack_by_name(&self, name: &str) {
let mut stack = self.stack.write().unwrap();
let mut stack = self.stack.write();
stack.pop_item(name);
}

fn bring_stack_to_front(&self, name: &str) {
let mut stack = self.stack.write().unwrap();
let mut stack = self.stack.write();
stack.bring_to_front(name);
}

fn send_stack_to_back(&self, name: &str) {
let mut stack = self.stack.write().unwrap();
let mut stack = self.stack.write();
stack.send_to_back(name);
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/launcher/src/manager/container_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::sync::{Arc, RwLock};
use std::sync::Arc;

use ripple_sdk::{
parking_lot::RwLock,
tokio::sync::{mpsc, oneshot},
uuid::Uuid,
};
Expand Down
45 changes: 20 additions & 25 deletions core/launcher/src/manager/view_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use std::{collections::HashMap, sync::Arc};

use ripple_sdk::{
api::{
Expand All @@ -30,6 +27,7 @@ use ripple_sdk::{
manifest::apps::AppProperties,
},
log::error,
parking_lot::RwLock,
tokio::sync::oneshot,
utils::{channel_utils::oneshot_send_and_log, error::RippleError},
uuid::Uuid,
Expand Down Expand Up @@ -87,12 +85,13 @@ impl ViewRequest {
}

pub fn send_response(&self, response: ViewResponse) -> Result<(), RippleError> {
let mut sender = self.resp_tx.write().unwrap();
if sender.is_some() {
oneshot_send_and_log(sender.take().unwrap(), response, "ViewManager response");
Ok(())
} else {
Err(RippleError::SenderMissing)
let mut sender = self.resp_tx.write();
match sender.take() {
Some(tx) => {
oneshot_send_and_log(tx, response, "ViewManager response");
Ok(())
}
None => Err(RippleError::SenderMissing),
}
}
}
Expand Down Expand Up @@ -142,27 +141,23 @@ pub struct ViewState {

impl ViewState {
fn insert_view(&self, key: String, view: ViewId) {
let _ = self.view_pool.write().unwrap().insert(key, view);
let _ = self.view_pool.write().insert(key, view);
}

fn get_name(&self, key: ViewId) -> Option<String> {
self.view_pool
.read()
.unwrap()
.iter()
.find_map(
|(name, &id)| {
if id == key {
Some(name.clone())
} else {
None
}
},
)
self.view_pool.read().iter().find_map(
|(name, &id)| {
if id == key {
Some(name.clone())
} else {
None
}
},
)
}

fn remove(&self, key: &str) {
let _ = self.view_pool.write().unwrap().remove(key);
let _ = self.view_pool.write().remove(key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/main/src/bootstrap/extn/load_extn_metadata_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Bootstep<BootstrapState> for LoadExtensionMetadataStep {
})
.collect();
unsafe {
let mut loaded_extns = state.extn_state.loaded_libraries.write().unwrap();
let mut loaded_extns = state.extn_state.loaded_libraries.write();
for (extn_path, entry) in extn_paths {
debug!("");
debug!("");
Expand Down
6 changes: 3 additions & 3 deletions core/main/src/bootstrap/extn/load_extn_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Bootstep<BootstrapState> for LoadExtensionsStep {
"LoadExtensionsStep".into()
}
async fn setup(&self, state: BootstrapState) -> Result<(), RippleError> {
let loaded_extensions = state.extn_state.loaded_libraries.read().unwrap();
let loaded_extensions = state.extn_state.loaded_libraries.read();
let mut deferred_channels: Vec<PreLoadedExtnChannel> = Vec::new();
let mut device_channels: Vec<PreLoadedExtnChannel> = Vec::new();
let mut jsonrpsee_extns: Methods = Methods::new();
Expand Down Expand Up @@ -122,13 +122,13 @@ impl Bootstep<BootstrapState> for LoadExtensionsStep {
}

{
let mut device_channel_state = state.extn_state.device_channels.write().unwrap();
let mut device_channel_state = state.extn_state.device_channels.write();
info!("{} Device channels extension loaded", device_channels.len());
let _ = device_channel_state.extend(device_channels);
}

{
let mut deferred_channel_state = state.extn_state.deferred_channels.write().unwrap();
let mut deferred_channel_state = state.extn_state.deferred_channels.write();
info!(
"{} Deferred channels extension loaded",
deferred_channels.len()
Expand Down
4 changes: 2 additions & 2 deletions core/main/src/bootstrap/extn/start_extn_channel_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Bootstep<BootstrapState> for StartExtnChannelsStep {
async fn setup(&self, state: BootstrapState) -> Result<(), RippleError> {
let mut extn_ids = Vec::new();
{
let mut device_channels = state.extn_state.device_channels.write().unwrap();
let mut device_channels = state.extn_state.device_channels.write();
while let Some(device_channel) = device_channels.pop() {
let id = device_channel.extn_id.clone();
extn_ids.push(id);
Expand All @@ -65,7 +65,7 @@ impl Bootstep<BootstrapState> for StartExtnChannelsStep {
}

{
let mut deferred_channels = state.extn_state.deferred_channels.write().unwrap();
let mut deferred_channels = state.extn_state.deferred_channels.write();
while let Some(deferred_channel) = deferred_channels.pop() {
let id = deferred_channel.extn_id.clone();
extn_ids.push(id);
Expand Down
8 changes: 5 additions & 3 deletions core/main/src/firebolt/rpc_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use jsonrpsee::{
},
types::{error::ErrorCode, Id, Params},
};

use ripple_sdk::{
api::{
apps::EffectiveTransport,
Expand All @@ -36,11 +37,12 @@ use ripple_sdk::{
chrono::Utc,
extn::extn_client_message::{ExtnMessage, ExtnResponse},
log::{error, info},
parking_lot::RwLock,
serde_json::{self, Result as SResult},
tokio::{self},
utils::error::RippleError,
};
use std::sync::{Arc, RwLock};
use std::sync::Arc;

use crate::{
service::telemetry_builder::TelemetryBuilder,
Expand All @@ -64,12 +66,12 @@ impl RouterState {
}

pub fn update_methods(&self, methods: Methods) {
let mut methods_state = self.methods.write().unwrap();
let mut methods_state = self.methods.write();
let _ = methods_state.merge(methods.initialize_resources(&self.resources).unwrap());
}

fn get_methods(&self) -> Methods {
self.methods.read().unwrap().clone()
self.methods.read().clone()
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/main/src/processor/main_context_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//

use std::{
sync::{Arc, Once, RwLock},
sync::{Arc, Once},
time::Duration,
};

Expand All @@ -41,6 +41,7 @@ use ripple_sdk::{
extn_client_message::{ExtnMessage, ExtnResponse},
},
log::{debug, error, info},
parking_lot::RwLock,
tokio::{
self,
sync::{mpsc::Receiver as MReceiver, mpsc::Sender as MSender},
Expand Down Expand Up @@ -332,7 +333,7 @@ impl ExtnEventProcessor for MainContextProcessor {
_ => {}
}
{
let mut context = state.current_context.write().unwrap();
let mut context = state.current_context.write();
context.deep_copy(extracted_message);
}
}
Expand Down
Loading

0 comments on commit 6876b96

Please sign in to comment.