@@ -87,11 +87,11 @@ def initialize(opts = {})
87
87
raise ArgumentError . new ( 'min_threads cannot be less than zero' ) if @min_length < 0
88
88
raise ArgumentError . new ( "#{ overflow_policy } is not a valid overflow policy" ) unless OVERFLOW_POLICIES . include? ( @overflow_policy )
89
89
90
- @state = :running
90
+ init_executor
91
+
91
92
@pool = [ ]
92
- @terminator = Event . new
93
+ @stopped_event = Event . new
93
94
@queue = Queue . new
94
- @mutex = Mutex . new
95
95
@scheduled_task_count = 0
96
96
@completed_task_count = 0
97
97
@largest_length = 0
@@ -104,8 +104,8 @@ def initialize(opts = {})
104
104
#
105
105
# @return [Integer] the length
106
106
def length
107
- @ mutex. synchronize do
108
- @state != :shutdown ? @pool . length : 0
107
+ mutex . synchronize do
108
+ running? ? @pool . length : 0
109
109
end
110
110
end
111
111
alias_method :current_length , :length
@@ -122,29 +122,15 @@ def queue_length
122
122
#
123
123
# @return [Integer] the remaining_capacity
124
124
def remaining_capacity
125
- @mutex . synchronize { @max_queue == 0 ? -1 : @max_queue - @queue . length }
126
- end
127
-
128
- # Is the thread pool running?
129
- #
130
- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
131
- def running?
132
- @mutex . synchronize { @state == :running }
125
+ mutex . synchronize { @max_queue == 0 ? -1 : @max_queue - @queue . length }
133
126
end
134
127
135
128
# Returns an array with the status of each thread in the pool
136
129
#
137
130
# This method is deprecated and will be removed soon.
138
131
def status
139
132
warn '[DEPRECATED] `status` is deprecated and will be removed soon.'
140
- @mutex . synchronize { @pool . collect { |worker | worker . status } }
141
- end
142
-
143
- # Is the thread pool shutdown?
144
- #
145
- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
146
- def shutdown?
147
- @mutex . synchronize { @state != :running }
133
+ mutex . synchronize { @pool . collect { |worker | worker . status } }
148
134
end
149
135
150
136
# Block until thread pool shutdown is complete or until `timeout` seconds have
@@ -157,7 +143,7 @@ def shutdown?
157
143
#
158
144
# @return [Boolean] `true` if shutdown complete or false on `timeout`
159
145
def wait_for_termination ( timeout )
160
- return @terminator . wait ( timeout . to_i )
146
+ return @stopped_event . wait ( timeout . to_i )
161
147
end
162
148
163
149
# Submit a task to the thread pool for asynchronous processing.
@@ -172,8 +158,8 @@ def wait_for_termination(timeout)
172
158
# @raise [ArgumentError] if no task is given
173
159
def post ( *args , &task )
174
160
raise ArgumentError . new ( 'no block given' ) unless block_given?
175
- @ mutex. synchronize do
176
- break false unless @state == : running
161
+ mutex . synchronize do
162
+ break false unless running?
177
163
return handle_overflow ( *args , &task ) if @max_queue != 0 && @queue . length >= @max_queue
178
164
@scheduled_task_count += 1
179
165
@queue << [ args , task ]
@@ -190,14 +176,13 @@ def post(*args, &task)
190
176
# but no new tasks will be accepted. Has no additional effect if the
191
177
# thread pool is not running.
192
178
def shutdown
193
- @ mutex. synchronize do
194
- break unless @state == : running
179
+ mutex . synchronize do
180
+ break unless running?
195
181
@queue . clear
182
+ stop_event . set
196
183
if @pool . empty?
197
- @state = :shutdown
198
- @terminator . set
184
+ @stopped_event . set
199
185
else
200
- @state = :shuttingdown
201
186
@pool . length . times { @queue << :stop }
202
187
end
203
188
end
@@ -208,34 +193,34 @@ def shutdown
208
193
# will be accepted. Has no additional effect if the thread pool is
209
194
# not running.
210
195
def kill
211
- @mutex . synchronize do
212
- break if @state == :shutdown
196
+ mutex . synchronize do
197
+ return if shutdown?
198
+ stop_event . set
213
199
@queue . clear
214
- @state = :shutdown
215
200
drain_pool
216
- @terminator . set
201
+ stopped_event . set
217
202
end
218
203
end
219
204
220
205
# Run on task completion.
221
206
#
222
207
# @!visibility private
223
208
def on_end_task # :nodoc:
224
- @ mutex. synchronize do
209
+ mutex . synchronize do
225
210
@completed_task_count += 1 #if success
226
- break unless @state == : running
211
+ break unless running?
227
212
end
228
213
end
229
214
230
215
# Run when a thread worker exits.
231
216
#
232
217
# @!visibility private
233
218
def on_worker_exit ( worker ) # :nodoc:
234
- @ mutex. synchronize do
219
+ mutex . synchronize do
235
220
@pool . delete ( worker )
236
- if @pool . empty? && @state != : running
237
- @state = :shutdown
238
- @terminator . set
221
+ if @pool . empty? && ! running?
222
+ stop_event . set
223
+ stopped_event . set
239
224
end
240
225
end
241
226
end
0 commit comments