Skip to content

Commit

Permalink
RwLock in PrivateServices.free_ids replaced with Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSherbinin committed Feb 27, 2024
1 parent decda45 commit 51d37bd
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions rustyrpc/src/server/private_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod service_ref;
use crate::{format::EncodingFormat, service::Service};
use core::ops::Deref;
use derive_where::derive_where;
use tokio::sync::{RwLock, RwLockReadGuard};
use tokio::sync::{Mutex, RwLock, RwLockReadGuard};

pub use allocator::PrivateServiceAllocator;
pub use service_ref::ServiceRef;
Expand All @@ -25,12 +25,12 @@ impl<Format: EncodingFormat> Deref for ServiceRefLock<'_, Format> {
#[derive_where(Default)]
pub(super) struct PrivateServices<Format: EncodingFormat> {
services: boxcar::Vec<RwLock<Option<Box<dyn Service<Format>>>>>,
free_ids: RwLock<Vec<usize>>,
free_ids: Mutex<Vec<usize>>,
}

impl<Format: EncodingFormat> PrivateServices<Format> {
pub(super) async fn push(&self, service: Box<dyn Service<Format>>) -> usize {
if let Some(service_id) = self.free_ids.write().await.pop() {
if let Some(service_id) = self.free_ids.lock().await.pop() {
#[allow(clippy::unwrap_used)]
let mut service_entry = self.services.get(service_id).unwrap().write().await;
*service_entry = Some(service);
Expand All @@ -52,7 +52,7 @@ impl<Format: EncodingFormat> PrivateServices<Format> {
let mut service_entry = self.services.get(id)?.write().await;

if service_entry.is_some() {
self.free_ids.write().await.push(id);
self.free_ids.lock().await.push(id);
}

service_entry.take()
Expand Down

0 comments on commit 51d37bd

Please sign in to comment.