|
| 1 | +require 'concurrent/atomic/event' |
| 2 | + |
| 3 | +module Concurrent |
| 4 | + |
| 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) |
| 8 | + |
| 9 | + module Executor |
| 10 | + |
| 11 | + # Submit a task to the executor for asynchronous processing. |
| 12 | + # |
| 13 | + # @param [Array] args zero or more arguments to be passed to the task |
| 14 | + # |
| 15 | + # @yield the asynchronous task to perform |
| 16 | + # |
| 17 | + # @return [Boolean] `true` if the task is queued, `false` if the executor |
| 18 | + # is not running |
| 19 | + # |
| 20 | + # @raise [ArgumentError] if no task is given |
| 21 | + def post(*args, &task) |
| 22 | + raise ArgumentError.new('no block given') unless block_given? |
| 23 | + mutex.synchronize do |
| 24 | + return false unless running? |
| 25 | + execute(*args, &task) |
| 26 | + true |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + # Submit a task to the executor for asynchronous processing. |
| 31 | + # |
| 32 | + # @param [Proc] task the asynchronous task to perform |
| 33 | + # |
| 34 | + # @return [self] returns itself |
| 35 | + def <<(task) |
| 36 | + post(&task) |
| 37 | + self |
| 38 | + end |
| 39 | + |
| 40 | + # Is the executor running? |
| 41 | + # |
| 42 | + # @return [Boolean] `true` when running, `false` when shutting down or shutdown |
| 43 | + def running? |
| 44 | + ! stop_event.set? |
| 45 | + end |
| 46 | + |
| 47 | + # Is the executor shuttingdown? |
| 48 | + # |
| 49 | + # @return [Boolean] `true` when not running and not shutdown, else `false` |
| 50 | + def shuttingdown? |
| 51 | + ! (running? || shutdown?) |
| 52 | + end |
| 53 | + |
| 54 | + # Is the executor shutdown? |
| 55 | + # |
| 56 | + # @return [Boolean] `true` when shutdown, `false` when shutting down or running |
| 57 | + def shutdown? |
| 58 | + stopped_event.set? |
| 59 | + end |
| 60 | + |
| 61 | + # Begin an orderly shutdown. Tasks already in the queue will be executed, |
| 62 | + # but no new tasks will be accepted. Has no additional effect if the |
| 63 | + # thread pool is not running. |
| 64 | + def shutdown |
| 65 | + mutex.synchronize do |
| 66 | + return unless running? |
| 67 | + stop_event.set |
| 68 | + shutdown_execution |
| 69 | + end |
| 70 | + true |
| 71 | + end |
| 72 | + |
| 73 | + # Begin an immediate shutdown. In-progress tasks will be allowed to |
| 74 | + # complete but enqueued tasks will be dismissed and no new tasks |
| 75 | + # will be accepted. Has no additional effect if the thread pool is |
| 76 | + # not running. |
| 77 | + def kill |
| 78 | + mutex.synchronize do |
| 79 | + return if shutdown? |
| 80 | + stop_event.set |
| 81 | + kill_execution |
| 82 | + stopped_event.set |
| 83 | + end |
| 84 | + true |
| 85 | + end |
| 86 | + |
| 87 | + # Block until executor shutdown is complete or until `timeout` seconds have |
| 88 | + # passed. |
| 89 | + # |
| 90 | + # @note Does not initiate shutdown or termination. Either `shutdown` or `kill` |
| 91 | + # must be called before this method (or on another thread). |
| 92 | + # |
| 93 | + # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete |
| 94 | + # |
| 95 | + # @return [Boolean] `true` if shutdown complete or false on `timeout` |
| 96 | + def wait_for_termination(timeout) |
| 97 | + stopped_event.wait(timeout.to_f) |
| 98 | + end |
| 99 | + |
| 100 | + protected |
| 101 | + |
| 102 | + attr_reader :mutex, :stop_event, :stopped_event |
| 103 | + |
| 104 | + def init_executor |
| 105 | + @mutex = Mutex.new |
| 106 | + @stop_event = Event.new |
| 107 | + @stopped_event = Event.new |
| 108 | + end |
| 109 | + |
| 110 | + def execute(*args, &task) |
| 111 | + raise NotImplementedError |
| 112 | + end |
| 113 | + |
| 114 | + def shutdown_execution |
| 115 | + stopped_event.set |
| 116 | + end |
| 117 | + |
| 118 | + def kill_execution |
| 119 | + # do nothing |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + if RUBY_PLATFORM == 'java' |
| 124 | + |
| 125 | + module JavaExecutor |
| 126 | + |
| 127 | + # Submit a task to the executor for asynchronous processing. |
| 128 | + # |
| 129 | + # @param [Array] args zero or more arguments to be passed to the task |
| 130 | + # |
| 131 | + # @yield the asynchronous task to perform |
| 132 | + # |
| 133 | + # @return [Boolean] `true` if the task is queued, `false` if the executor |
| 134 | + # is not running |
| 135 | + # |
| 136 | + # @raise [ArgumentError] if no task is given |
| 137 | + def post(*args) |
| 138 | + raise ArgumentError.new('no block given') unless block_given? |
| 139 | + if running? |
| 140 | + @executor.submit{ yield(*args) } |
| 141 | + true |
| 142 | + else |
| 143 | + false |
| 144 | + end |
| 145 | + rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex |
| 146 | + raise RejectedExecutionError |
| 147 | + end |
| 148 | + |
| 149 | + # Submit a task to the executor for asynchronous processing. |
| 150 | + # |
| 151 | + # @param [Proc] task the asynchronous task to perform |
| 152 | + # |
| 153 | + # @return [self] returns itself |
| 154 | + def <<(task) |
| 155 | + post(&task) |
| 156 | + self |
| 157 | + end |
| 158 | + |
| 159 | + # Is the executor running? |
| 160 | + # |
| 161 | + # @return [Boolean] `true` when running, `false` when shutting down or shutdown |
| 162 | + def running? |
| 163 | + ! (shuttingdown? || shutdown?) |
| 164 | + end |
| 165 | + |
| 166 | + # Is the executor shuttingdown? |
| 167 | + # |
| 168 | + # @return [Boolean] `true` when not running and not shutdown, else `false` |
| 169 | + def shuttingdown? |
| 170 | + if @executor.respond_to? :isTerminating |
| 171 | + @executor.isTerminating |
| 172 | + else |
| 173 | + false |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + # Is the executor shutdown? |
| 178 | + # |
| 179 | + # @return [Boolean] `true` when shutdown, `false` when shutting down or running |
| 180 | + def shutdown? |
| 181 | + @executor.isShutdown || @executor.isTerminated |
| 182 | + end |
| 183 | + |
| 184 | + # Block until executor shutdown is complete or until `timeout` seconds have |
| 185 | + # passed. |
| 186 | + # |
| 187 | + # @note Does not initiate shutdown or termination. Either `shutdown` or `kill` |
| 188 | + # must be called before this method (or on another thread). |
| 189 | + # |
| 190 | + # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete |
| 191 | + # |
| 192 | + # @return [Boolean] `true` if shutdown complete or false on `timeout` |
| 193 | + def wait_for_termination(timeout) |
| 194 | + @executor.awaitTermination(timeout.to_i, java.util.concurrent.TimeUnit::SECONDS) |
| 195 | + end |
| 196 | + |
| 197 | + # Begin an orderly shutdown. Tasks already in the queue will be executed, |
| 198 | + # but no new tasks will be accepted. Has no additional effect if the |
| 199 | + # executor is not running. |
| 200 | + def shutdown |
| 201 | + @executor.shutdown |
| 202 | + nil |
| 203 | + end |
| 204 | + |
| 205 | + # Begin an immediate shutdown. In-progress tasks will be allowed to |
| 206 | + # complete but enqueued tasks will be dismissed and no new tasks |
| 207 | + # will be accepted. Has no additional effect if the executor is |
| 208 | + # not running. |
| 209 | + def kill |
| 210 | + @executor.shutdownNow |
| 211 | + nil |
| 212 | + end |
| 213 | + |
| 214 | + protected |
| 215 | + |
| 216 | + def set_shutdown_hook |
| 217 | + # without this the process may fail to exit |
| 218 | + at_exit { self.kill } |
| 219 | + end |
| 220 | + end |
| 221 | + end |
| 222 | +end |
0 commit comments