1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::sync::atomic::{AtomicUsize, Ordering};
use num_cpus;
const DEFAULT_STACK_SIZE: usize = 0x1000;
const DEFAULT_POOL_CAPACITY: usize = 100;
static WORKERS: AtomicUsize = AtomicUsize::new(0);
static STACK_SIZE: AtomicUsize = AtomicUsize::new(DEFAULT_STACK_SIZE);
static POOL_CAPACITY: AtomicUsize = AtomicUsize::new(DEFAULT_POOL_CAPACITY);
pub struct Config;
pub fn config() -> Config {
Config
}
impl Config {
pub fn set_workers(&self, workers: usize) -> &Self {
assert!(workers <= 64);
info!("set workers={:?}", workers);
WORKERS.store(workers, Ordering::Relaxed);
self
}
pub fn get_workers(&self) -> usize {
let workers = WORKERS.load(Ordering::Relaxed);
if workers != 0 {
workers
} else {
let num = num_cpus::get();
WORKERS.store(num, Ordering::Relaxed);
num
}
}
#[deprecated(since = "0.3.13", note = "use `set_workers` only")]
pub fn set_io_workers(&self, _workers: usize) -> &Self {
self
}
pub fn set_pool_capacity(&self, capacity: usize) -> &Self {
info!("set pool capacity={:?}", capacity);
POOL_CAPACITY.store(capacity, Ordering::Release);
self
}
pub fn get_pool_capacity(&self) -> usize {
let size = POOL_CAPACITY.load(Ordering::Acquire);
if size != 0 {
size
} else {
DEFAULT_POOL_CAPACITY
}
}
pub fn set_stack_size(&self, size: usize) -> &Self {
info!("set stack size={:?}", size);
STACK_SIZE.store(size, Ordering::Release);
self
}
pub fn get_stack_size(&self) -> usize {
STACK_SIZE.load(Ordering::Acquire)
}
}