Class: Libuv::Loop
- Inherits:
-
Object
- Object
- Libuv::Loop
- Extended by:
- ClassMethods
- Includes:
- Assertions, Resource
- Defined in:
- lib/libuv/loop.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- LOOPS =
ThreadSafe::Cache.new
- CRITICAL =
Mutex.new
Constants included from Assertions
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) ⇒ ::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) ⇒ Loop
constructor
Initialize a loop using an FFI::Pointer to a libuv loop.
-
#inspect ⇒ Object
Overwrite as errors in jRuby can literally hang VM when inspecting as many many classes will reference this class.
-
#log(level, id, *args) ⇒ Object
Notifies the loop there was an event that should be logged.
-
#lookup(hostname, hint = :IPv4, port = 9, &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 loop (thread safe).
-
#notifier ⇒ ::Libuv::Q::Promise
Provides a promise notifier for receiving un-handled exceptions.
-
#now ⇒ Fixnum
Get current time in milliseconds.
-
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance.
-
#prepare(callback = nil, &blk) ⇒ ::Libuv::Prepare
Get a new Prepare handle.
-
#reactor_running? ⇒ Boolean
Tells you whether the Libuv reactor loop is currently running.
-
#reactor_thread ⇒ Thread
Exposed to allow joining on the thread, when run in a multithreaded environment.
-
#reactor_thread? ⇒ Boolean
True if the calling thread is the same thread as the reactor.
-
#run(run_type = :UV_RUN_DEFAULT) {|promise| ... } ⇒ Object
Run the actual event loop.
-
#schedule(callback = nil, &block) ⇒ Object
Schedule some work to be processed on the event loop as soon as possible (thread safe).
-
#signal(signum = nil, callback = nil, &block) ⇒ ::Libuv::Signal
Get a new signal handler.
-
#stop ⇒ Object
Closes handles opened by the loop class and completes the current loop iteration (thread safe).
-
#tcp ⇒ ::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 ⇒ ::Libuv::UDP
Get a new UDP instance.
-
#update_time ⇒ Object
forces loop 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 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) ⇒ Loop
Initialize a loop using an FFI::Pointer to a libuv loop
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/libuv/loop.rb', line 48 def initialize(pointer) # :notnew: @pointer = pointer @loop = self # Create an async call for scheduling work from other threads @run_queue = Queue.new @process_queue = @loop.async method(:process_queue_cb) @process_queue.unref # Create a next tick timer @next_tick = @loop.timer method(:next_tick_cb) @next_tick.unref # Create an async call for ending the loop @stop_loop = @loop.async method(:stop_cb) @stop_loop.unref 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)
161 162 163 |
# File 'lib/libuv/loop.rb', line 161 def all(*promises) Q.all(@loop, *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.
171 172 173 |
# File 'lib/libuv/loop.rb', line 171 def any(*promises) Q.any(@loop, *promises) end |
#async(callback = nil, &block) ⇒ ::Libuv::Async
Get a new Async handle
286 287 288 289 290 291 |
# File 'lib/libuv/loop.rb', line 286 def async(callback = nil, &block) callback ||= block handle = Async.new(@loop) handle.progress callback if callback handle end |
#check(callback = nil, &blk) ⇒ ::Libuv::Check
Get a new Check handle
271 272 273 |
# File 'lib/libuv/loop.rb', line 271 def check(callback = nil, &blk) Check.new(@loop, 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)
149 150 151 |
# File 'lib/libuv/loop.rb', line 149 def defer Q.defer(@loop) end |
#file(path, flags = 0, mode = 0) ⇒ ::Libuv::File
Opens a file and returns an object that can be used to manipulate it
343 344 345 346 347 348 |
# File 'lib/libuv/loop.rb', line 343 def file(path, flags = 0, mode = 0) 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(@loop, path, flags, mode) end |
#filesystem ⇒ ::Libuv::Filesystem
Returns an object for manipulating the filesystem
353 354 355 |
# File 'lib/libuv/loop.rb', line 353 def filesystem Filesystem.new(@loop) 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.
182 183 184 |
# File 'lib/libuv/loop.rb', line 182 def finally(*promises) Q.finally(@loop, *promises) end |
#fs_event(path) ⇒ ::Libuv::FSEvent
Get a new FSEvent instance
332 333 334 335 |
# File 'lib/libuv/loop.rb', line 332 def fs_event(path) assert_type(String, path) FSEvent.new(@loop, path) end |
#handle ⇒ Object
111 |
# File 'lib/libuv/loop.rb', line 111 def handle; @pointer; end |
#idle(callback = nil, &block) ⇒ ::Libuv::Idle
Get a new Idle handle
279 280 281 |
# File 'lib/libuv/loop.rb', line 279 def idle(callback = nil, &block) Idle.new(@loop, callback || block) end |
#inspect ⇒ Object
Overwrite as errors in jRuby can literally hang VM when inspecting as many many classes will reference this class
106 107 108 |
# File 'lib/libuv/loop.rb', line 106 def inspect "#<#{self.class}:0x#{self.__id__.to_s(16)} NT=#{@run_queue.length}>" end |
#log(level, id, *args) ⇒ Object
Notifies the loop there was an event that should be logged
399 400 401 |
# File 'lib/libuv/loop.rb', line 399 def log(level, id, *args) @loop_notify.notify(level, id, *args) end |
#lookup(hostname, hint = :IPv4, port = 9, &block) ⇒ ::Libuv::Dns
Lookup a hostname
321 322 323 324 325 |
# File 'lib/libuv/loop.rb', line 321 def lookup(hostname, hint = :IPv4, port = 9, &block) dns = Dns.new(@loop, hostname, port, hint) # Work is a promise object dns.then block if block_given? dns end |
#lookup_error(err) ⇒ ::Libuv::Error
Lookup an error code and return is as an error object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/libuv/loop.rb', line 205 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) else # We want a back-trace in this case raise "error lookup failed for code #{err}" end rescue Exception => e @loop.log :warn, :error_lookup_failed, e e end |
#next_tick(callback = nil, &block) ⇒ Object
Queue some work to be processed in the next iteration of the event loop (thread safe)
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/libuv/loop.rb', line 377 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 end |
#notifier ⇒ ::Libuv::Q::Promise
Provides a promise notifier for receiving un-handled exceptions
141 142 143 |
# File 'lib/libuv/loop.rb', line 141 def notifier @loop_notify.promise end |
#now ⇒ Fixnum
Get current time in milliseconds
197 198 199 |
# File 'lib/libuv/loop.rb', line 197 def now ::Libuv::Ext.now(@pointer) end |
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance
249 250 251 |
# File 'lib/libuv/loop.rb', line 249 def pipe(ipc = false) Pipe.new(@loop, ipc) end |
#prepare(callback = nil, &blk) ⇒ ::Libuv::Prepare
Get a new Prepare handle
264 265 266 |
# File 'lib/libuv/loop.rb', line 264 def prepare(callback = nil, &blk) Prepare.new(@loop, callback || blk) end |
#reactor_running? ⇒ Boolean
Tells you whether the Libuv reactor loop is currently running.
425 426 427 |
# File 'lib/libuv/loop.rb', line 425 def reactor_running? !@reactor_thread.nil? end |
#reactor_thread ⇒ Thread
Exposed to allow joining on the thread, when run in a multithreaded environment. Performing other actions on the thread has undefined semantics (read: a dangerous endevor).
418 419 420 |
# File 'lib/libuv/loop.rb', line 418 def reactor_thread @reactor_thread end |
#reactor_thread? ⇒ Boolean
True if the calling thread is the same thread as the reactor.
411 412 413 |
# File 'lib/libuv/loop.rb', line 411 def reactor_thread? @reactor_thread == Thread.current end |
#run(run_type = :UV_RUN_DEFAULT) {|promise| ... } ⇒ Object
Run the actual event loop. This method will block until the loop is stopped.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/libuv/loop.rb', line 118 def run(run_type = :UV_RUN_DEFAULT) if @reactor_thread.nil? @loop_notify = @loop.defer begin @reactor_thread = Thread.current LOOPS[@reactor_thread] = @loop yield @loop_notify.promise if block_given? ::Libuv::Ext.run(@pointer, run_type) # This is blocking ensure @reactor_thread = nil @run_queue.clear end elsif block_given? schedule { yield @loop_notify.promise } end @loop end |
#schedule(callback = nil, &block) ⇒ Object
Schedule some work to be processed on the event loop as soon as possible (thread safe)
361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/libuv/loop.rb', line 361 def schedule(callback = nil, &block) callback ||= block assert_block(callback) if reactor_thread? callback.call else @run_queue << callback @process_queue.call end end |
#signal(signum = nil, callback = nil, &block) ⇒ ::Libuv::Signal
Get a new signal handler
296 297 298 299 300 301 302 |
# File 'lib/libuv/loop.rb', line 296 def signal(signum = nil, callback = nil, &block) callback ||= block handle = Signal.new(@loop) handle.progress callback if callback handle.start(signum) if signum handle end |
#stop ⇒ Object
Closes handles opened by the loop class and completes the current loop iteration (thread safe)
404 405 406 |
# File 'lib/libuv/loop.rb', line 404 def stop @stop_loop.call end |
#tcp ⇒ ::Libuv::TCP
Get a new TCP instance
223 224 225 |
# File 'lib/libuv/loop.rb', line 223 def tcp TCP.new(@loop) end |
#timer(callback = nil, &blk) ⇒ ::Libuv::Timer
Get a new timer instance
257 258 259 |
# File 'lib/libuv/loop.rb', line 257 def timer(callback = nil, &blk) Timer.new(@loop, callback || blk) end |
#tty(fileno, readable = false) ⇒ ::Libuv::TTY
Get a new TTY instance
239 240 241 242 243 |
# File 'lib/libuv/loop.rb', line 239 def tty(fileno, readable = false) assert_type(Integer, fileno, "io#fileno must return an integer file descriptor, #{fileno.inspect} given") TTY.new(@loop, fileno, readable) end |
#udp ⇒ ::Libuv::UDP
Get a new UDP instance
230 231 232 |
# File 'lib/libuv/loop.rb', line 230 def udp UDP.new(@loop) end |
#update_time ⇒ Object
forces loop time update, useful for getting more granular times
190 191 192 |
# File 'lib/libuv/loop.rb', line 190 def update_time ::Libuv::Ext.update_time(@pointer) end |
#work(callback = nil, &block) ⇒ ::Libuv::Work
Queue some work for processing in the libuv thread pool
309 310 311 312 313 |
# File 'lib/libuv/loop.rb', line 309 def work(callback = nil, &block) callback ||= block assert_block(callback) Work.new(@loop, callback) # Work is a promise object end |