Class: Libuv::Reactor
- Extended by:
- Accessors, ClassMethods
- Includes:
- Assertions, Resource
- Defined in:
- lib/libuv/reactor.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CRITICAL =
::Mutex.new
Constants included from Accessors
Constants included from Assertions
Instance Attribute Summary collapse
-
#run_count ⇒ Object
readonly
Returns the value of attribute run_count.
Instance Method Summary collapse
-
#all(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
-
#any(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when any of the input promises are resolved.
-
#async(callback = nil, &block) ⇒ ::Libuv::Async
Get a new Async handle.
-
#check(callback = nil, &blk) ⇒ ::Libuv::Check
Get a new Check handle.
-
#defer ⇒ ::Libuv::Q::Deferred
Creates a deferred result object for where the result of an operation may only be returned at some point in the future or is being processed on a different thread (thread safe).
-
#file(path, flags = 0, mode: 0, **opts, &blk) ⇒ ::Libuv::File
Opens a file and returns an object that can be used to manipulate it.
-
#filesystem ⇒ ::Libuv::Filesystem
Returns an object for manipulating the filesystem.
-
#finally(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved or rejected.
-
#fs_event(path) ⇒ ::Libuv::FSEvent
Get a new FSEvent instance.
- #handle ⇒ Object
-
#idle(callback = nil, &block) ⇒ ::Libuv::Idle
Get a new Idle handle.
-
#initialize(pointer) ⇒ Reactor
constructor
Initialize a reactor using an FFI::Pointer to a libuv reactor.
-
#inspect ⇒ Object
Overwrite as errors in jRuby can literally hang VM when inspecting as many many classes will reference this class.
-
#log(error, msg = nil, trace = nil) ⇒ Object
Notifies the reactor there was an event that should be logged.
-
#lookup(hostname, hint = :IPv4, port = 9, wait: true, &block) ⇒ ::Libuv::Dns
Lookup a hostname.
-
#lookup_error(err) ⇒ ::Libuv::Error
Lookup an error code and return is as an error object.
-
#next_tick(callback = nil, &block) ⇒ Object
Queue some work to be processed in the next iteration of the event reactor (thread safe).
-
#notifier(callback = nil, &blk) ⇒ ::Libuv::Q::Promise
Provides a promise notifier for receiving un-handled exceptions.
-
#now ⇒ Fixnum
Get current time in milliseconds.
-
#on_program_interrupt(callback = nil, &block) ⇒ Object
Allows user defined behaviour when sig int is received.
-
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance.
-
#prepare(callback = nil, &blk) ⇒ ::Libuv::Prepare
Get a new Prepare handle.
-
#reactor_running? ⇒ Boolean
(also: #running?)
Tells you whether the Libuv reactor reactor is currently running.
-
#reactor_thread? ⇒ Boolean
True if the calling thread is the same thread as the reactor.
-
#ref ⇒ Object
Prevents the reactor loop from stopping.
-
#reject(reason) ⇒ Object
Creates a promise that is resolved as rejected with the specified reason.
-
#run(run_type = :UV_RUN_DEFAULT) {|promise| ... } ⇒ Object
Run the actual event reactor.
-
#schedule(callback = nil, &block) ⇒ Object
Schedule some work to be processed on the event reactor as soon as possible (thread safe).
-
#signal(signum = nil, callback = nil, &block) ⇒ ::Libuv::Signal
Get a new signal handler.
- #sleep(msecs) ⇒ Object
-
#stop ⇒ Object
Closes handles opened by the reactor class and completes the current reactor iteration (thread safe).
-
#tcp(callback = nil, &blk) ⇒ ::Libuv::TCP
Get a new TCP instance.
-
#timer(callback = nil, &blk) ⇒ ::Libuv::Timer
Get a new timer instance.
-
#tty(fileno, readable = false) ⇒ ::Libuv::TTY
Get a new TTY instance.
-
#udp(callback = nil, &blk) ⇒ ::Libuv::UDP
Get a new UDP instance.
-
#unref ⇒ Object
Allows the reactor loop to stop.
-
#update_time ⇒ Object
forces reactor time update, useful for getting more granular times.
-
#work(callback = nil, &block) ⇒ ::Libuv::Work
Queue some work for processing in the libuv thread pool.
Methods included from Accessors
Methods included from ClassMethods
Methods included from Resource
#check_result, #check_result!, #resolve, #to_ptr
Methods included from Assertions
#assert_block, #assert_boolean, #assert_type
Constructor Details
#initialize(pointer) ⇒ Reactor
Initialize a reactor using an FFI::Pointer to a libuv reactor
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/libuv/reactor.rb', line 56 def initialize(pointer) # :notnew: @pointer = pointer @reactor = self @run_count = 0 @ref_count = 0 # Create an async call for scheduling work from other threads @run_queue = Queue.new @process_queue = @reactor.async method(:process_queue_cb) @process_queue.unref # Create a next tick timer @next_tick = @reactor.timer method(:next_tick_cb) @next_tick.unref # Create an async call for ending the reactor @stop_reactor = @reactor.async method(:stop_cb) @stop_reactor.unref # Libuv can prevent the application shutting down once the main thread has ended # The addition of a prepare function prevents this from happening. @reactor_prep = Libuv::Prepare.new(@reactor, method(:noop)) @reactor_prep.unref @reactor_prep.start # LibUV ingnores program interrupt by default. # We provide normal behaviour and allow this to be overriden @on_signal = [] sig_callback = method(:signal_cb) self.signal(:INT, sig_callback).unref self.signal(:HUP, sig_callback).unref self.signal(:TERM, sig_callback).unref # Notify of errors @throw_on_exit = nil @reactor_notify_default = @reactor_notify = proc { |error| @throw_on_exit = error } end |
Instance Attribute Details
#run_count ⇒ Object (readonly)
Returns the value of attribute run_count.
96 97 98 |
# File 'lib/libuv/reactor.rb', line 96 def run_count @run_count end |
Instance Method Details
#all(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved. (thread safe)
245 246 247 |
# File 'lib/libuv/reactor.rb', line 245 def all(*promises) Q.all(@reactor, *promises) end |
#any(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when any of the input promises are resolved.
255 256 257 |
# File 'lib/libuv/reactor.rb', line 255 def any(*promises) Q.any(@reactor, *promises) end |
#async(callback = nil, &block) ⇒ ::Libuv::Async
Get a new Async handle
388 389 390 391 392 393 |
# File 'lib/libuv/reactor.rb', line 388 def async(callback = nil, &block) callback ||= block handle = Async.new(@reactor) handle.progress callback if callback handle end |
#check(callback = nil, &blk) ⇒ ::Libuv::Check
Get a new Check handle
373 374 375 |
# File 'lib/libuv/reactor.rb', line 373 def check(callback = nil, &blk) Check.new(@reactor, callback || blk) end |
#defer ⇒ ::Libuv::Q::Deferred
Creates a deferred result object for where the result of an operation may only be returned at some point in the future or is being processed on a different thread (thread safe)
233 234 235 |
# File 'lib/libuv/reactor.rb', line 233 def defer Q.defer(@reactor) end |
#file(path, flags = 0, mode: 0, **opts, &blk) ⇒ ::Libuv::File
Opens a file and returns an object that can be used to manipulate it
455 456 457 458 459 460 |
# File 'lib/libuv/reactor.rb', line 455 def file(path, flags = 0, mode: 0, **opts, &blk) assert_type(String, path, "path must be a String") assert_type(Integer, flags, "flags must be an Integer") assert_type(Integer, mode, "mode must be an Integer") File.new(@reactor, path, flags, mode: mode, **opts, &blk) end |
#filesystem ⇒ ::Libuv::Filesystem
Returns an object for manipulating the filesystem
465 466 467 |
# File 'lib/libuv/reactor.rb', line 465 def filesystem Filesystem.new(@reactor) end |
#finally(*promises) ⇒ ::Libuv::Q::Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved or rejected.
266 267 268 |
# File 'lib/libuv/reactor.rb', line 266 def finally(*promises) Q.finally(@reactor, *promises) end |
#fs_event(path) ⇒ ::Libuv::FSEvent
Get a new FSEvent instance
444 445 446 447 |
# File 'lib/libuv/reactor.rb', line 444 def fs_event(path) assert_type(String, path) FSEvent.new(@reactor, path) end |
#handle ⇒ Object
155 |
# File 'lib/libuv/reactor.rb', line 155 def handle; @pointer; end |
#idle(callback = nil, &block) ⇒ ::Libuv::Idle
Get a new Idle handle
381 382 383 |
# File 'lib/libuv/reactor.rb', line 381 def idle(callback = nil, &block) Idle.new(@reactor, callback || block) end |
#inspect ⇒ Object
Overwrite as errors in jRuby can literally hang VM when inspecting as many many classes will reference this class
150 151 152 |
# File 'lib/libuv/reactor.rb', line 150 def inspect "#<#{self.class}:0x#{self.__id__.to_s(16)} NT=#{@run_queue.length}>" end |
#log(error, msg = nil, trace = nil) ⇒ Object
Notifies the reactor there was an event that should be logged
515 516 517 |
# File 'lib/libuv/reactor.rb', line 515 def log(error, msg = nil, trace = nil) @reactor_notify.call(error, msg, trace) end |
#lookup(hostname, hint = :IPv4, port = 9, wait: true, &block) ⇒ ::Libuv::Dns
Lookup a hostname
429 430 431 432 433 434 435 436 437 |
# File 'lib/libuv/reactor.rb', line 429 def lookup(hostname, hint = :IPv4, port = 9, wait: true, &block) dns = Dns.new(@reactor, hostname, port, hint, wait: wait) # Work is a promise object if block_given? || !wait dns.then block dns else dns.results end end |
#lookup_error(err) ⇒ ::Libuv::Error
Lookup an error code and return is as an error object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/libuv/reactor.rb', line 296 def lookup_error(err) name = ::Libuv::Ext.err_name(err) if name msg = ::Libuv::Ext.strerror(err) ::Libuv::Error.const_get(name.to_sym).new("#{msg}, #{name}:#{err}") else # We want a back-trace in this case raise "error lookup failed for code #{err}" end rescue Exception => e @reactor.log e, 'performing error lookup' e end |
#next_tick(callback = nil, &block) ⇒ Object
Queue some work to be processed in the next iteration of the event reactor (thread safe)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/libuv/reactor.rb', line 491 def next_tick(callback = nil, &block) callback ||= block assert_block(callback) @run_queue << callback if reactor_thread? # Create a next tick timer if not @next_tick_scheduled @next_tick.start(0) @next_tick_scheduled = true @next_tick.ref end else @process_queue.call end self end |
#notifier(callback = nil, &blk) ⇒ ::Libuv::Q::Promise
Provides a promise notifier for receiving un-handled exceptions
224 225 226 227 |
# File 'lib/libuv/reactor.rb', line 224 def notifier(callback = nil, &blk) @reactor_notify = callback || blk || @reactor_notify_default self end |
#now ⇒ Fixnum
Get current time in milliseconds
288 289 290 |
# File 'lib/libuv/reactor.rb', line 288 def now ::Libuv::Ext.now(@pointer) end |
#on_program_interrupt(callback = nil, &block) ⇒ Object
Allows user defined behaviour when sig int is received
407 408 409 410 |
# File 'lib/libuv/reactor.rb', line 407 def on_program_interrupt(callback = nil, &block) @on_signal << (callback || block) self end |
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance
351 352 353 |
# File 'lib/libuv/reactor.rb', line 351 def pipe(ipc = false) Pipe.new(@reactor, ipc) end |
#prepare(callback = nil, &blk) ⇒ ::Libuv::Prepare
Get a new Prepare handle
366 367 368 |
# File 'lib/libuv/reactor.rb', line 366 def prepare(callback = nil, &blk) Prepare.new(@reactor, callback || blk) end |
#reactor_running? ⇒ Boolean Also known as: running?
Tells you whether the Libuv reactor reactor is currently running.
534 535 536 |
# File 'lib/libuv/reactor.rb', line 534 def reactor_running? @reactor_running end |
#reactor_thread? ⇒ Boolean
True if the calling thread is the same thread as the reactor.
527 528 529 |
# File 'lib/libuv/reactor.rb', line 527 def reactor_thread? self == Thread.current.thread_variable_get(:reactor) end |
#ref ⇒ Object
Prevents the reactor loop from stopping
205 206 207 208 209 210 |
# File 'lib/libuv/reactor.rb', line 205 def ref if reactor_thread? && reactor_running? @process_queue.ref if @ref_count == 0 @ref_count += 1 end end |
#reject(reason) ⇒ Object
Creates a promise that is resolved as rejected with the specified reason. This api should be used to forward rejection in a chain of promises. If you are dealing with the last promise in a promise chain, you don’t need to worry about it.
273 274 275 |
# File 'lib/libuv/reactor.rb', line 273 def reject(reason) Q.reject(@reactor, reason) end |
#run(run_type = :UV_RUN_DEFAULT) {|promise| ... } ⇒ Object
Run the actual event reactor. This method will block until the reactor is stopped.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/libuv/reactor.rb', line 162 def run(run_type = :UV_RUN_DEFAULT) if not @reactor_running begin @reactor_running = true raise 'only one reactor allowed per-thread' if Thread.current.thread_variable_get(:reactor) Thread.current.thread_variable_set(:reactor, @reactor) @throw_on_exit = nil if block_given? update_time ::Fiber.new { begin yield @reactor rescue Exception => e log(e, 'in reactor run block') end }.resume end @run_count += 1 ::Libuv::Ext.run(@pointer, run_type) # This is blocking ensure Thread.current.thread_variable_set(:reactor, nil) @reactor_running = false @run_queue.clear end # Raise the last unhandled error to occur on the reactor thread raise @throw_on_exit if @throw_on_exit elsif block_given? if reactor_thread? update_time yield @reactor else raise 'reactor already running on another thread' end end @reactor end |
#schedule(callback = nil, &block) ⇒ Object
Schedule some work to be processed on the event reactor as soon as possible (thread safe)
473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/libuv/reactor.rb', line 473 def schedule(callback = nil, &block) callback ||= block assert_block(callback) if reactor_thread? callback.call else @run_queue << callback @process_queue.call end self end |
#signal(signum = nil, callback = nil, &block) ⇒ ::Libuv::Signal
Get a new signal handler
398 399 400 401 402 403 404 |
# File 'lib/libuv/reactor.rb', line 398 def signal(signum = nil, callback = nil, &block) callback ||= block handle = Signal.new(@reactor) handle.progress callback if callback handle.start(signum) if signum handle end |
#sleep(msecs) ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/libuv/reactor.rb', line 311 def sleep(msecs) fiber = Fiber.current time = timer { time.close fiber.resume }.start(msecs) Fiber.yield end |
#stop ⇒ Object
Closes handles opened by the reactor class and completes the current reactor iteration (thread safe)
520 521 522 |
# File 'lib/libuv/reactor.rb', line 520 def stop @stop_reactor.call end |
#tcp(callback = nil, &blk) ⇒ ::Libuv::TCP
Get a new TCP instance
323 324 325 326 |
# File 'lib/libuv/reactor.rb', line 323 def tcp(callback = nil, &blk) callback ||= blk TCP.new(@reactor, progress: callback) end |
#timer(callback = nil, &blk) ⇒ ::Libuv::Timer
Get a new timer instance
359 360 361 |
# File 'lib/libuv/reactor.rb', line 359 def timer(callback = nil, &blk) Timer.new(@reactor, callback || blk) end |
#tty(fileno, readable = false) ⇒ ::Libuv::TTY
Get a new TTY instance
341 342 343 344 345 |
# File 'lib/libuv/reactor.rb', line 341 def tty(fileno, readable = false) assert_type(Integer, fileno, "io#fileno must return an integer file descriptor, #{fileno.inspect} given") TTY.new(@reactor, fileno, readable) end |
#udp(callback = nil, &blk) ⇒ ::Libuv::UDP
Get a new UDP instance
331 332 333 334 |
# File 'lib/libuv/reactor.rb', line 331 def udp(callback = nil, &blk) callback ||= blk UDP.new(@reactor, progress: callback) end |
#unref ⇒ Object
Allows the reactor loop to stop
213 214 215 216 217 218 |
# File 'lib/libuv/reactor.rb', line 213 def unref if reactor_thread? && reactor_running? && @ref_count > 0 @ref_count -= 1 @process_queue.unref if @ref_count == 0 end end |
#update_time ⇒ Object
forces reactor time update, useful for getting more granular times
280 281 282 283 |
# File 'lib/libuv/reactor.rb', line 280 def update_time ::Libuv::Ext.update_time(@pointer) self end |
#work(callback = nil, &block) ⇒ ::Libuv::Work
Queue some work for processing in the libuv thread pool
417 418 419 420 421 |
# File 'lib/libuv/reactor.rb', line 417 def work(callback = nil, &block) callback ||= block assert_block(callback) Work.new(@reactor, callback) # Work is a promise object end |