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
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 ⇒ ::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.
-
#log(level, id, *args) ⇒ Object
Notifies the loop there was an event that should be logged.
-
#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).
-
#now ⇒ Fixnum
Get current time in microseconds.
-
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance.
-
#prepare ⇒ ::Libuv::Prepare
Get a new Prepare handle.
-
#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
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 |
# File 'lib/libuv/loop.rb', line 34 def initialize(pointer) # :notnew: @pointer = pointer @loop = self # Create an async call for scheduling work from other threads @run_queue = Queue.new @queue_proc = proc do # ensure we only execute what was required for this tick length = @run_queue.length length.times do begin run = @run_queue.pop true # pop non-block run.call rescue Exception => e @loop.log :error, :next_tick_cb, e end end end @process_queue = Async.new(@loop, @queue_proc) # Create a next tick timer @next_tick = @loop.timer do @next_tick_scheduled = false @queue_proc.call end # Create an async call for ending the loop @stop_loop = Async.new @loop do @process_queue.close @stop_loop.close @next_tick.close ::Libuv::Ext.stop(@pointer) end 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)
109 110 111 |
# File 'lib/libuv/loop.rb', line 109 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.
119 120 121 |
# File 'lib/libuv/loop.rb', line 119 def any(*promises) Q.any(@loop, *promises) end |
#async(callback = nil, &block) ⇒ ::Libuv::Async
Get a new Async handle
229 230 231 232 233 234 |
# File 'lib/libuv/loop.rb', line 229 def async(callback = nil, &block) callback ||= block handle = Async.new(@loop) handle.progress callback if callback handle end |
#check ⇒ ::Libuv::Check
Get a new Check handle
214 215 216 |
# File 'lib/libuv/loop.rb', line 214 def check Check.new(@loop) 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)
97 98 99 |
# File 'lib/libuv/loop.rb', line 97 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
274 275 276 277 278 279 |
# File 'lib/libuv/loop.rb', line 274 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
284 285 286 |
# File 'lib/libuv/loop.rb', line 284 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.
130 131 132 |
# File 'lib/libuv/loop.rb', line 130 def finally(*promises) Q.finally(@loop, *promises) end |
#fs_event(path) ⇒ ::Libuv::FSEvent
Get a new FSEvent instance
263 264 265 266 |
# File 'lib/libuv/loop.rb', line 263 def fs_event(path) assert_type(String, path) FSEvent.new(@loop, path) end |
#handle ⇒ Object
70 |
# File 'lib/libuv/loop.rb', line 70 def handle; @pointer; end |
#idle(callback = nil, &block) ⇒ ::Libuv::Idle
Get a new Idle handle
222 223 224 |
# File 'lib/libuv/loop.rb', line 222 def idle(callback = nil, &block) Idle.new(@loop, callback || block) end |
#log(level, id, *args) ⇒ Object
Notifies the loop there was an event that should be logged
329 330 331 |
# File 'lib/libuv/loop.rb', line 329 def log(level, id, *args) @loop_notify.notify(level, id, *args) end |
#lookup_error(err) ⇒ ::Libuv::Error
Lookup an error code and return is as an error object
153 154 155 156 157 158 159 160 161 |
# File 'lib/libuv/loop.rb', line 153 def lookup_error(err) name = ::Libuv::Ext.err_name(err) msg = ::Libuv::Ext.strerror(err) ::Libuv::Error.const_get(name.to_sym).new(msg) rescue Exception => e @loop.log :warn, :error_lookup_failed, e ::Libuv::Error::UNKNOWN.new("error lookup failed for code #{err} #{name} #{msg}") end |
#next_tick(callback = nil, &block) ⇒ Object
Queue some work to be processed in the next iteration of the event loop (thread safe)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/libuv/loop.rb', line 308 def next_tick(callback = nil, &block) callback ||= block assert_block(callback) @run_queue << callback if @reactor_thread == Thread.current # Create a next tick timer if not @next_tick_scheduled @next_tick.start(0) @next_tick_scheduled = true end else @process_queue.call end end |
#now ⇒ Fixnum
Get current time in microseconds
145 146 147 |
# File 'lib/libuv/loop.rb', line 145 def now ::Libuv::Ext.now(@pointer) end |
#pipe(ipc = false) ⇒ ::Libuv::Pipe
Get a new Pipe instance
192 193 194 |
# File 'lib/libuv/loop.rb', line 192 def pipe(ipc = false) Pipe.new(@loop, ipc) end |
#prepare ⇒ ::Libuv::Prepare
Get a new Prepare handle
207 208 209 |
# File 'lib/libuv/loop.rb', line 207 def prepare Prepare.new(@loop) end |
#run(run_type = :UV_RUN_DEFAULT) {|promise| ... } ⇒ Object
Run the actual event loop. This method will block until the loop is stopped.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/libuv/loop.rb', line 77 def run(run_type = :UV_RUN_DEFAULT) @loop_notify = @loop.defer begin @reactor_thread = Thread.current yield @loop_notify.promise if block_given? ::Libuv::Ext.run(@pointer, run_type) # This is blocking ensure @reactor_thread = nil @run_queue.clear end @loop end |
#schedule(callback = nil, &block) ⇒ Object
Schedule some work to be processed on the event loop as soon as possible (thread safe)
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/libuv/loop.rb', line 292 def schedule(callback = nil, &block) callback ||= block assert_block(callback) if @reactor_thread == Thread.current block.call else @run_queue << callback @process_queue.call end end |
#signal(signum = nil, callback = nil, &block) ⇒ ::Libuv::Signal
Get a new signal handler
239 240 241 242 243 244 245 |
# File 'lib/libuv/loop.rb', line 239 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)
334 335 336 |
# File 'lib/libuv/loop.rb', line 334 def stop @stop_loop.call end |
#tcp ⇒ ::Libuv::TCP
Get a new TCP instance
166 167 168 |
# File 'lib/libuv/loop.rb', line 166 def tcp TCP.new(@loop) end |
#timer(callback = nil, &blk) ⇒ ::Libuv::Timer
Get a new timer instance
200 201 202 |
# File 'lib/libuv/loop.rb', line 200 def timer(callback = nil, &blk) Timer.new(@loop, callback || blk) end |
#tty(fileno, readable = false) ⇒ ::Libuv::TTY
Get a new TTY instance
182 183 184 185 186 |
# File 'lib/libuv/loop.rb', line 182 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
173 174 175 |
# File 'lib/libuv/loop.rb', line 173 def udp UDP.new(@loop) end |
#update_time ⇒ Object
forces loop time update, useful for getting more granular times
138 139 140 |
# File 'lib/libuv/loop.rb', line 138 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
252 253 254 255 256 |
# File 'lib/libuv/loop.rb', line 252 def work(callback = nil, &block) callback ||= block assert_block(callback) Work.new(@loop, callback) # Work is a promise object end |