Skip to content

Commit 5b90c4c

Browse files
committed
Create an IndirectImmediateExecutor
1 parent 43c4a2d commit 5b90c4c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'concurrent/executor/executor'
2+
3+
module Concurrent
4+
# An executor service which runs all operations on a new thread, blocking
5+
# until it completes. Operations are performed in the order they are received
6+
# and no two operations can be performed simultaneously.
7+
#
8+
# This executor service exists mainly for testing an debugging. When used it
9+
# immediately runs every `#post` operation on a new thread, blocking the
10+
# current thread until the operation is complete. This is similar to how the
11+
# ImmediateExecutor works, but the operation has the full stack of the new
12+
# thread at its disposal. This can be helpful when the operations will spawn
13+
# more operations on the same executor and so on - such a situation might
14+
# overflow the single stack in case of an ImmediateExecutor, which is
15+
# inconsistent with how it would behave for a threaded executor.
16+
#
17+
# @note Intended for use primarily in testing and debugging.
18+
class IndirectImmediateExecutor < ImmediateExecutor
19+
# @!macro executor_method_post
20+
def post(*args, &task)
21+
return false unless running?
22+
23+
executor = SingleThreadExecutor.new
24+
executor.post(*args, &task)
25+
executor.shutdown
26+
executor.wait_for_termination
27+
true
28+
end
29+
end
30+
end

lib/concurrent/executors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'concurrent/executor/cached_thread_pool'
22
require 'concurrent/executor/fixed_thread_pool'
33
require 'concurrent/executor/immediate_executor'
4+
require 'concurrent/executor/indirect_immediate_executor'
45
require 'concurrent/executor/per_thread_executor'
56
require 'concurrent/executor/safe_task_executor'
67
require 'concurrent/executor/single_thread_executor'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'spec_helper'
2+
require_relative 'executor_service_shared'
3+
4+
module Concurrent
5+
6+
describe IndirectImmediateExecutor do
7+
8+
subject { IndirectImmediateExecutor.new }
9+
10+
it_should_behave_like :executor_service
11+
end
12+
end

0 commit comments

Comments
 (0)