Skip to content

Commit c668202

Browse files
authored
feat: add methods to get the current used parallelism (#24)
1 parent bff09ef commit c668202

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/parallelism.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,25 @@ impl Parallelism {
6464
///
6565
/// Note that increasing the parallelism beyond the max
6666
/// parallelism won't do anything.
67-
pub fn set_parallelism(&self, parallelism: NonZeroUsize) {
67+
pub fn set_max(&self, parallelism: NonZeroUsize) {
6868
self.sempahore.set_max(parallelism);
6969
}
70+
71+
#[deprecated = "use set_max instead"]
72+
pub fn set_parallelism(&self, parallelism: NonZeroUsize) {
73+
self.set_max(parallelism);
74+
}
75+
76+
/// Current set parallelism.
77+
pub fn current_max(&self) -> usize {
78+
self.sempahore.current_max()
79+
}
80+
81+
/// Current amount of active parallelism which may be greater
82+
/// or less than the max parallelism.
83+
pub fn current_used(&self) -> usize {
84+
self.sempahore.current_used()
85+
}
7086
}
7187

7288
impl ParallelismProvider for Parallelism {

src/utils/semaphore.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ impl Semaphore {
4242
}
4343
}
4444

45+
pub fn current_used(&self) -> usize {
46+
self.permits.lock().used
47+
}
48+
49+
pub fn current_max(&self) -> usize {
50+
self.permits.lock().max
51+
}
52+
4553
pub fn set_max(&self, n: NonZeroUsize) {
4654
let mut permits = self.permits.lock();
4755
let is_greater = n.get() > permits.max;

0 commit comments

Comments
 (0)