forked from ruby-concurrency/concurrent-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_executor_service.rb
76 lines (62 loc) · 1.41 KB
/
ruby_executor_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'concurrent/executor/abstract_executor_service'
require 'concurrent/atomic/event'
module Concurrent
# @!macro abstract_executor_service_public_api
# @!visibility private
class RubyExecutorService < AbstractExecutorService
safe_initialization!
def initialize(*args, &block)
super
@StopEvent = Event.new
@StoppedEvent = Event.new
end
def post(*args, &task)
raise ArgumentError.new('no block given') unless block_given?
synchronize do
# If the executor is shut down, reject this task
return handle_fallback(*args, &task) unless running?
ns_execute(*args, &task)
true
end
end
def shutdown
synchronize do
break unless running?
stop_event.set
ns_shutdown_execution
end
true
end
def kill
synchronize do
break if shutdown?
stop_event.set
ns_kill_execution
stopped_event.set
end
true
end
def wait_for_termination(timeout = nil)
stopped_event.wait(timeout)
end
private
def stop_event
@StopEvent
end
def stopped_event
@StoppedEvent
end
def ns_shutdown_execution
stopped_event.set
end
def ns_running?
!stop_event.set?
end
def ns_shuttingdown?
!(ns_running? || ns_shutdown?)
end
def ns_shutdown?
stopped_event.set?
end
end
end