Skip to content

Commit 08b5bfb

Browse files
authored
Implement Condition#waiting_count. (#456)
1 parent 886d62c commit 08b5bfb

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎fixtures/async/a_condition.rb‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ module Async
5858
expect(order).to be == [0, 1, 2, 3, 4]
5959
end
6060

61+
it "counts waiting fibers" do
62+
expect(condition.waiting_count).to be == 0
63+
64+
task1 = reactor.async do
65+
condition.wait
66+
end
67+
task2 = reactor.async do
68+
condition.wait
69+
end
70+
71+
expect(condition.waiting_count).to be == 2
72+
73+
task3 = reactor.async do
74+
condition.wait
75+
end
76+
77+
expect(condition.waiting_count).to be == 3
78+
79+
condition.signal
80+
task1.wait
81+
task2.wait
82+
task3.wait
83+
expect(condition.waiting_count).to be == 0
84+
end
85+
6186
with "timeout" do
6287
let(:ready) {Async::Variable.new(condition)}
6388
let(:waiting) {Async::Variable.new(subject.new)}

‎lib/async/condition.rb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def waiting?
3232
!self.empty?
3333
end
3434

35+
# @returns [Integer] Number of fibers waiting on this condition.
36+
def waiting_count
37+
@ready.num_waiting
38+
end
39+
3540
# Signal to a given task that it should resume operations.
3641
# @parameter value [Object | Nil] The value to return to the waiting fibers.
3742
def signal(value = nil)

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Introduce `Async::Condition#waiting_count`. This allows you to see how many tasks are currently waiting on the condition, which can be useful for debugging and monitoring purposes.
6+
37
## v2.39.0
48

59
- `Async::Barrier#wait` now returns the number of tasks that were waited for, or `nil` if there were no tasks to wait for. This provides better feedback about the operation, and allows you to know how many tasks were involved in the wait.

0 commit comments

Comments
 (0)