Skip to content

Commit b42aeb1

Browse files
committed
refcell instead of mutex
1 parent 15e1153 commit b42aeb1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/pool.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use lazy_static::lazy_static;
22

33
use std::convert::{AsRef, AsMut};
4+
use std::cell::RefCell;
45
use std::collections::HashMap;
56
use std::sync::Mutex;
67
use std::sync::atomic::{AtomicBool,Ordering};
@@ -10,9 +11,9 @@ use crate::DType;
1011

1112
static USE_POOL: AtomicBool = AtomicBool::new(true);
1213
thread_local! {
13-
static POOL: Mutex<MemoryPool> = {
14+
static POOL: RefCell<MemoryPool> = {
1415
let m = MemoryPool::new();
15-
Mutex::new(m)
16+
RefCell::new(m)
1617
};
1718
}
1819

@@ -56,8 +57,7 @@ fn should_use_pool() -> bool {
5657
pub fn allocate_vec(size: usize) -> MPVec {
5758
if should_use_pool() {
5859
POOL.with(|p| {
59-
let mut pool = p.lock()
60-
.expect("Error accessing memory pool!");
60+
let mut pool = p.borrow_mut();
6161
pool.get(size)
6262
})
6363
} else {
@@ -67,17 +67,15 @@ pub fn allocate_vec(size: usize) -> MPVec {
6767

6868
pub fn clear_pool() {
6969
POOL.with(|p| {
70-
let mut pool = p.lock()
71-
.expect("Error accessing memory pool!");
70+
let mut pool = p.borrow_mut();
7271
pool.clear();
7372
});
7473
}
7574

7675
fn return_vec(v: Vec<DType>) {
7776
if should_use_pool() {
7877
POOL.with(|p| {
79-
let mut pool = p.lock()
80-
.expect("Error accessing memory pool!");
78+
let mut pool = p.borrow_mut();
8179
pool.ret(v);
8280
});
8381
}

0 commit comments

Comments
 (0)