Skip to content

Commit 38e302e

Browse files
committed
Code formatting.
1 parent 4c0bfbc commit 38e302e

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

fixtures/async/chainable_async.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Async
99
let(:chainable) {subject.new(parent: parent)}
1010

1111
it "should chain async to parent" do
12-
expect(parent).to receive(:async).and_return {|*arguments, **options, &block|
12+
expect(parent).to receive(:async).and_return{|*arguments, **options, &block|
1313
Async(*arguments, **options, &block)
1414
}
1515

lib/async/barrier.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Barrier
1818
def initialize(parent: nil)
1919
@tasks = List.new
2020
@finished = Queue.new
21-
@cond = Condition.new
21+
@condition = Condition.new
2222

2323
@parent = parent
2424
end
@@ -43,26 +43,32 @@ def size
4343

4444
# Execute a child task and add it to the barrier.
4545
# @asynchronous Executes the given block concurrently.
46+
# @returns [Task] The task which was created to execute the block.
4647
def async(*arguments, parent: (@parent or Task.current), **options, &block)
4748
raise "Barrier is stopped!" if @finished.closed?
4849

4950
waiting = nil
5051

5152
task = parent.async(*arguments, **options) do |task, *arguments|
53+
# Create a new list node for the task and add it to the list of waiting tasks:
5254
node = TaskNode.new(task)
5355
@tasks.append(node)
54-
56+
57+
# Signal the outer async block that we have added the task to the list of waiting tasks, and that it can now wait for it to finish:
5558
waiting = node
56-
@cond.signal
57-
59+
@condition.signal
60+
61+
# Invoke the block, which may raise an error. If it does, we will still signal that the task has finished:
5862
block.call(task, *arguments)
5963
ensure
64+
# Signal that the task has finished, which will unblock the waiting task:
6065
@finished.signal(node) unless @finished.closed?
6166
end
62-
63-
@cond.wait while waiting.nil?
64-
65-
task
67+
68+
# `parent.async` may yield before the child block executes, so we wait here until the child has appended itself to `@tasks`, ensuring `wait` cannot return early and miss tracking it:
69+
@condition.wait while waiting.nil?
70+
71+
return task
6672
end
6773

6874
# Whether there are any tasks being held by the barrier.

test/async/barrier.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
barrier.stop
134134
end
135135
end
136-
136+
137137
it "waits even if the child task yields immediately" do
138138
class Yielder
139139
def async(*arguments, **options, &block)
@@ -143,15 +143,15 @@ def async(*arguments, **options, &block)
143143
end
144144
end
145145
end
146-
146+
147147
parent = Yielder.new
148-
148+
149149
3.times do |i|
150-
barrier.async(parent:) {i}
150+
barrier.async(parent:){i}
151151
end
152-
152+
153153
expect(barrier.size).to be == 3
154-
154+
155155
results = []
156156
barrier.wait do |task|
157157
results << task.wait

0 commit comments

Comments
 (0)