Skip to content

Commit 1fd07c2

Browse files
committed
wip: Created stub of Executor mixin module.
1 parent 045efba commit 1fd07c2

8 files changed

+67
-64
lines changed

lib/concurrent/executor/executor.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Concurrent
2+
3+
# An exception class raised when the maximum queue size is reached and the
4+
# `overflow_policy` is set to `:abort`.
5+
RejectedExecutionError = Class.new(StandardError)
6+
7+
module Executor
8+
9+
def running?
10+
true
11+
end
12+
13+
def shutdown?
14+
false
15+
end
16+
17+
def shutdown
18+
end
19+
20+
def kill
21+
end
22+
23+
def wait_for_termination(timeout)
24+
end
25+
26+
def post(*args, &task)
27+
end
28+
29+
# Submit a task to the thread pool for asynchronous processing.
30+
#
31+
# @param [Proc] task the asynchronous task to perform
32+
#
33+
# @return [self] returns itself
34+
def <<(task)
35+
post(&task)
36+
self
37+
end
38+
39+
protected
40+
41+
def init_executor
42+
end
43+
44+
def execute(*args, &task)
45+
end
46+
end
47+
end
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
require_relative 'executor'
2+
13
module Concurrent
24
class ImmediateExecutor
5+
include Executor
36

4-
def post(*args, &block)
7+
def post(*args, &task)
58
raise ArgumentError.new('no block given') unless block_given?
6-
block.call(*args)
9+
task.call(*args)
710
return true
811
end
9-
10-
def <<(block)
11-
post(&block)
12-
self
13-
end
1412
end
1513
end

lib/concurrent/executor/java_single_thread_executor.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
if RUBY_PLATFORM == 'java'
2+
require_relative 'executor'
23

34
module Concurrent
45

56
# @!macro single_thread_executor
67
class JavaSingleThreadExecutor
8+
include Executor
79

810
# Create a new thread pool.
911
#
@@ -67,17 +69,6 @@ def post(*args)
6769
raise RejectedExecutionError
6870
end
6971

70-
# Submit a task to the thread pool for asynchronous processing.
71-
#
72-
# @param [Proc] task the asynchronous task to perform
73-
#
74-
# @return [self] returns itself
75-
def <<(task)
76-
@executor.submit(&task)
77-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
78-
raise RejectedExecutionError
79-
end
80-
8172
# Begin an orderly shutdown. Tasks already in the queue will be executed,
8273
# but no new tasks will be accepted. Has no additional effect if the
8374
# thread pool is not running.

lib/concurrent/executor/java_thread_pool_executor.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
if RUBY_PLATFORM == 'java'
2+
require_relative 'executor'
23

34
module Concurrent
45

5-
# An exception class raised when the maximum queue size is reached and the
6-
# `overflow_policy` is set to `:abort`.
7-
RejectedExecutionError = Class.new(StandardError) unless defined? RejectedExecutionError
8-
96
# @!macro thread_pool_executor
107
class JavaThreadPoolExecutor
8+
include Executor
119

1210
# Default maximum number of threads that will be created in the pool.
1311
DEFAULT_MAX_POOL_SIZE = java.lang.Integer::MAX_VALUE # 2147483647
@@ -215,17 +213,6 @@ def post(*args)
215213
raise RejectedExecutionError
216214
end
217215

218-
# Submit a task to the thread pool for asynchronous processing.
219-
#
220-
# @param [Proc] task the asynchronous task to perform
221-
#
222-
# @return [self] returns itself
223-
def <<(task)
224-
@executor.submit(&task)
225-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
226-
raise RejectedExecutionError
227-
end
228-
229216
# Begin an orderly shutdown. Tasks already in the queue will be executed,
230217
# but no new tasks will be accepted. Has no additional effect if the
231218
# thread pool is not running.
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
require_relative 'executor'
2+
13
module Concurrent
24

35
class PerThreadExecutor
6+
include Executor
47

58
def self.post(*args)
69
raise ArgumentError.new('no block given') unless block_given?
@@ -11,13 +14,8 @@ def self.post(*args)
1114
return true
1215
end
1316

14-
def post(*args, &block)
15-
return PerThreadExecutor.post(*args, &block)
16-
end
17-
18-
def <<(block)
19-
PerThreadExecutor.post(&block)
20-
return self
17+
def post(*args, &task)
18+
return PerThreadExecutor.post(*args, &task)
2119
end
2220
end
2321
end

lib/concurrent/executor/ruby_single_thread_executor.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
require 'thread'
22

3+
require_relative 'executor'
34
require 'concurrent/atomic/event'
45

56
module Concurrent
67

78
# @!macro single_thread_executor
89
class RubySingleThreadExecutor
10+
include Executor
911

1012
# Create a new thread pool.
1113
#
@@ -67,16 +69,6 @@ def post(*args, &task)
6769
end
6870
end
6971

70-
# Submit a task to the thread pool for asynchronous processing.
71-
#
72-
# @param [Proc] task the asynchronous task to perform
73-
#
74-
# @return [self] returns itself
75-
def <<(task)
76-
self.post(&task)
77-
return self
78-
end
79-
8072
# Begin an orderly shutdown. Tasks already in the queue will be executed,
8173
# but no new tasks will be accepted. Has no additional effect if the
8274
# thread pool is not running.

lib/concurrent/executor/ruby_thread_pool_executor.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
require 'thread'
22

3+
require_relative 'executor'
34
require 'concurrent/atomic/event'
45
require 'concurrent/executor/ruby_thread_pool_worker'
56

67
module Concurrent
78

8-
# An exception class raised when the maximum queue size is reached and the
9-
# `overflow_policy` is set to `:abort`.
10-
RejectedExecutionError = Class.new(StandardError) unless defined? RejectedExecutionError
11-
129
# @!macro thread_pool_executor
1310
class RubyThreadPoolExecutor
11+
include Executor
1412

1513
# Default maximum number of threads that will be created in the pool.
1614
DEFAULT_MAX_POOL_SIZE = 2**15 # 32768
@@ -188,16 +186,6 @@ def post(*args, &task)
188186
end
189187
end
190188

191-
# Submit a task to the thread pool for asynchronous processing.
192-
#
193-
# @param [Proc] task the asynchronous task to perform
194-
#
195-
# @return [self] returns itself
196-
def <<(task)
197-
self.post(&task)
198-
return self
199-
end
200-
201189
# Begin an orderly shutdown. Tasks already in the queue will be executed,
202190
# but no new tasks will be accepted. Has no additional effect if the
203191
# thread pool is not running.

lib/concurrent/executor/timer_set.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'thread'
2+
require_relative 'executor'
23
require 'concurrent/options_parser'
34
require 'concurrent/atomic/event'
45
require 'concurrent/collection/priority_queue'
@@ -9,6 +10,7 @@ module Concurrent
910
# monitors the set and schedules each task for execution at the appropriate
1011
# time. Tasks are run on the global task pool or on the supplied executor.
1112
class TimerSet
13+
include Executor
1214
include OptionsParser
1315

1416
# Create a new set of timed tasks.

0 commit comments

Comments
 (0)