Skip to content

Commit

Permalink
fix: lock contention on acquiring the arena stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiKaiWi committed Sep 11, 2023
1 parent 6f90a7c commit 901106a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions components/arena/src/mono_inc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
sync::Arc,
};

use parking_lot::Mutex;
use parking_lot::RwLock;

use crate::arena_trait::{Arena, BasicStats, Collector, CollectorRef};

Expand All @@ -39,13 +39,13 @@ const DEFAULT_ALIGN: usize = 8;
/// allocated memory as blocks.
#[derive(Clone)]
pub struct MonoIncArena {
core: Arc<Mutex<ArenaCore>>,
core: Arc<RwLock<ArenaCore>>,
}

impl MonoIncArena {
pub fn new(regular_block_size: usize) -> Self {
Self {
core: Arc::new(Mutex::new(ArenaCore::new(
core: Arc::new(RwLock::new(ArenaCore::new(
regular_block_size,
Arc::new(NoopCollector {}),
))),
Expand All @@ -54,7 +54,7 @@ impl MonoIncArena {

pub fn with_collector(regular_block_size: usize, collector: CollectorRef) -> Self {
Self {
core: Arc::new(Mutex::new(ArenaCore::new(regular_block_size, collector))),
core: Arc::new(RwLock::new(ArenaCore::new(regular_block_size, collector))),
}
}
}
Expand All @@ -63,15 +63,15 @@ impl Arena for MonoIncArena {
type Stats = BasicStats;

fn try_alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
Some(self.core.lock().alloc(layout))
Some(self.core.write().alloc(layout))
}

fn stats(&self) -> Self::Stats {
self.core.lock().stats
self.core.read().stats
}

fn alloc(&self, layout: Layout) -> NonNull<u8> {
self.core.lock().alloc(layout)
self.core.write().alloc(layout)
}
}

Expand Down

0 comments on commit 901106a

Please sign in to comment.