Class: Dispatch::Source

Inherits:
Object show all

Overview

Dispatch::Source monitors a variety of system objects and events including file descriptors, processes, virtual filesystem nodes, signals and timers.

When a state change occurs, the dispatch source will submit its event handler block to its target queue, with the source as a parameter.

gcdq = Dispatch::Queue.new('doc')
src = Dispatch::Source.new(Dispatch::Source::DATA_ADD, 0, 0, gcdq) do |s|
  puts "Fired with #{s.data}"
end

Unlike GCD's C API, Dispatch::Source objects start off resumed (since the event handler -et al- have already been set).

src.suspended? #=? false
src << 0
gcdq.sync { } #=> Fired!

Constant Summary

DATA_ADD
DATA_OR
PROC
READ
SIGNAL
VNODE
WRITE
PROC_EXIT
PROC_FORK
PROC_EXEC
PROC_SIGNAL
VNODE_DELETE
VNODE_WRITE
VNODE_EXTEND
VNODE_ATTRIB
VNODE_RENAME
VNODE_REVOKE

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Object

#dispatch_object, #resume!, #suspend!, #suspended?

Methods inherited from NSObject

#!, #!=, #!~, #<=>, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

- (Dispatch::Source) Dispatch(type, handle, mask, queue) {|src| ... }

Returns a Source used to monitor a variety of system objects and events, using dispatch_source_create(3)

If an IO object (e.g., File) is passed as the handle, it will automatically create a cancel handler that closes that file (see cancel! for details). The type must be one of:

- Dispatch::Source::READ (calls +handle.close_read+)
- Dispatch::Source::WRITE (calls +handle.close_write+)
- Dispatch::Source::VNODE (calls +handle.close+)

This is the only way to set the cancel_handler, since in MacRuby sources start off resumed. This is safer than closing the file yourself, as the cancel handler is guaranteed to only run once, and only after all pending events are processed. If you do not want the file closed on cancel, simply use file.to_i to instead pass a descriptor as the handle.

Yields:

  • (src)

Returns:

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Object) alloc

+ (Dispatch::Source) Dispatch(delay, interval, leeway, queue)

Returns a Source that will submit the event handler block to the target queue after delay, repeated at interval, within leeway, via a call to dispatch_source_set_timer(3). A best effort attempt is made to submit the event handler block to the target queue at the specified time; however, actual invocation may occur at a later time even if the leeway is zero.

gcdq = Dispatch::Queue.new('doc')
timer = Dispatch::Source.timer(0, 5, 0.1, gcdq) do |s|
   puts s.data
end

Returns:

Instance Method Details

- (Object) <<(number)

Intended only for use with the Dispatch::Source::DATA_ADD and Dispatch::Source::DATA_OR source types, calling this function will atomically ADD or logical OR the count into the source's data, and trigger delivery of the source's event handler.

gcdq = Dispatch::Queue.new('doc')
@sum = 0
src = Dispatch::Source.new(Dispatch::Source::DATA_ADD, 0, 0, gcdq) do |s|
  @sum += s.data # safe since always serialized
end
src << 1
src << 3
gcdq.sync { }
puts @sum #=> 4

- (Object) cancel!

Asynchronously cancels the dispatch source, preventing any further invocation of its event handler block. Cancellation does not interrupt a currently executing handler block (non-preemptive).

When a dispatch source is canceled its cancellation handler will be submitted to its target queue. This is only used by Dispatch::Source; when initialized a File (or IO) object, it will automatically set a cancellation handler that closes it.

- (Boolean) cancelled?

Used to determine whether the specified source has been cancelled. True will be returned if the source is cancelled.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Numeric) data

Returns a Number containing currently pending data for the dispatch source. This function should only be called from within the source's event handler. The result of calling this function from any other context is undefined.

gcdq = Dispatch::Queue.new('doc')
src = Dispatch::Source.new(Dispatch::Source::DATA_ADD, 0, 0, gcdq) do |s|
  puts s.data
end
src << 1
gcdq.sync { } #=> 1

Returns:

- (Numeric) handle

Returns the underlying Ruby handle for the dispatch source (i.e. file, file descriptor, process identifer, etc.).

gcdq = Dispatch::Queue.new('doc')
name = "/var/tmp/gcd_spec_source-#{$$}-#{Time.now}"
file = File.open(name, "w")
src = Dispatch::Source.new(Dispatch::Source::WRITE, file, 0, gcdq) { }
puts src.handle #=> file

Returns:

- (Numeric) mask

Returns a Number representing the mask argument, corresponding to the flags set when the source was created.

gcdq = Dispatch::Queue.new('doc')
src = Dispatch::Source.new(Dispatch::Source::DATA_ADD, 0, 0, gcdq) { }
puts src.mask #=> 0

Returns: