Class: Eventbox

Inherits:
Object
  • Object
show all
Extended by:
Boxable
Defined in:
lib/eventbox.rb,
lib/eventbox/timer.rb,
lib/eventbox/boxable.rb,
lib/eventbox/version.rb,
lib/eventbox/sanitizer.rb,
lib/eventbox/event_loop.rb,
lib/eventbox/thread_pool.rb,
lib/eventbox/object_registry.rb,
lib/eventbox/argument_wrapper.rb

Direct Known Subclasses

ThreadPool, ThreadPool::PoolThread

Defined Under Namespace

Modules: ArgumentWrapper, Boxable, Sanitizer, Timer Classes: AbortAction, Action, AsyncProc, CompletionProc, ExternalObject, ExternalProc, InternalProc, InvalidAccess, MultipleResults, ObjectRegistry, SyncProc, Thread, ThreadPool, WrappedException, WrappedObject, WrappedProc, YieldProc

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Boxable

action, async_call, attr_accessor, attr_reader, attr_writer, sync_call, yield_call

Class Method Details

.eventbox_optionsHash

Retrieves the Eventbox options of this class.

See Also:


46
47
48
49
50
51
52
# File 'lib/eventbox.rb', line 46

def self.eventbox_options
  {
    threadpool: Thread,
    guard_time: 0.5,
    gc_actions: false,
  }
end

.with_options(**options) ⇒ Object

Create a new derived class with the given options.

The options are merged with the options of the base class. The following options are available:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/eventbox.rb', line 70

def self.with_options(**options)
  Class.new(self) do
    define_singleton_method(:eventbox_options) do
      super().merge(options)
    end

    def self.inspect
      klazz = self
      until name=klazz.name
        klazz = klazz.superclass
      end
      "#{name}#{eventbox_options}"
    end
  end
end

Instance Method Details

#async_proc(name = nil, &block) ⇒ Object (private)

Create a proc object for asynchronous (fire-and-forget) calls similar to Eventbox::Boxable#async_call.

It can be passed to external scope and called from there like so:

class MyBox < Eventbox
  sync_call def print(p1)
    async_proc do |p2|
      puts "#{p1} #{p2}"
    end
  end
end
MyBox.new.print("Hello").call("world")   # Prints "Hello world"

The created object can be safely called from any thread. All block arguments are passed through the Sanitizer. The block itself might not do any blocking calls or expensive computations - this would impair responsiveness of the Eventbox instance. Instead use Eventbox::Boxable#action in these cases.

The block always returns self to the caller.


181
182
183
# File 'lib/eventbox.rb', line 181

def async_proc(name=nil, &block)
  @__event_loop__.new_async_proc(name=nil, &block)
end

#init(*args) ⇒ Object (private)

Initialize a new Eventbox instance.

This method is executed for initialization of a Eventbox instance. This method receives all arguments given to Eventbox.new after they have passed the Sanitizer. It can be used like initialize in ordinary ruby classes including super to initialize included modules or base classes.

#init can be defined as either Eventbox::Boxable#sync_call or Eventbox::Boxable#async_call with no difference. #init can also be defined as Eventbox::Boxable#yield_call, so that the new call is blocked until the result is yielded. #init can even be defined as Eventbox::Boxable#action, so that each instance of the class immediately starts a new thread.


159
160
# File 'lib/eventbox.rb', line 159

def init(*args)
end

#shared_object(object) ⇒ Object

Mark an object as to be shared instead of copied.

A marked object is never passed as copy, but passed as reference. The object is therefore wrapped as WrappedObject or ExternalObject when used in an unsafe scope. Objects marked within the event scope are wrapped as WrappedObject, which denies access from external/action scope or the event scope of a different Eventbox instance. Objects marked in external/action scope are wrapped as ExternalObject which allows asynchronous calls from event scope. In all cases the object can be passed as reference and is automatically unwrapped when passed back to the original scope. It can therefore be used to modify the original object even after traversing the boundaries.

The mark is stored for the lifetime of the object, so that it's enough to mark only once at object creation.

Due to Sanitizer dissection of non-marshalable objects, wrapping and unwrapping works even if the shared object is stored within another object as instance variable or within a collection class. This is in contrast to €-variables which can only wrap the argument object as a whole when entering the event scope. See the difference here:

A = Struct.new(:a)
class Bo < Eventbox
  sync_call def go(struct, €struct)
    p struct            # prints #<struct A a=#<Eventbox::ExternalObject @object="abc" @name=:a>>
    p €struct           # prints #<Eventbox::ExternalObject @object=#<struct A a="abc"> @name=:€struct>
    [struct, €struct]
  end
end
e = Bo.new
o = A.new(e.shared_object("abc"))
e.go(o, o)              # => [#<struct A a="abc">, #<struct A a="abc">]

266
267
268
# File 'lib/eventbox.rb', line 266

public def shared_object(object)
  @__event_loop__.shared_object(object)
end

#shutdown!(&completion_block) ⇒ Object

Force stop of all action threads spawned by this Eventbox instance.

It is possible to enable automatic cleanup of action threads by the garbage collector through with_options. However in some cases automatic garbage collection doesn't remove all instances due to running action threads. Calling shutdown! when the work of the instance is done, ensures that it is GC'ed in all cases.

If #shutdown! is called externally, it blocks until all actions threads have terminated.

If #shutdown! is called in the event scope, it just triggers the termination of all action threads and returns afterwards. Optionally #shutdown! can be called with a block. It is called when all actions threads terminated.


281
282
283
# File 'lib/eventbox.rb', line 281

public def shutdown!(&completion_block)
  @__event_loop__.shutdown(&completion_block)
end

#sync_proc(name = nil, &block) ⇒ Object (private)

Create a Proc object for synchronous calls similar to Eventbox::Boxable#sync_call.

It can be passed to external scope and called from there like so:

class MyBox < Eventbox
  sync_call def print(p1)
    sync_proc do |p2|
      "#{p1} #{p2}"
    end
  end
end
puts MyBox.new.print("Hello").call("world")   # Prints "Hello world"

The created object can be safely called from any thread. All block arguments as well as the result value are passed through the Sanitizer. The block itself might not do any blocking calls or expensive computations - this would impair responsiveness of the Eventbox instance. Instead use Eventbox::Boxable#action in these cases.

This Proc is simular to #async_proc, but when the block is invoked, it is executed and it's return value is returned to the caller. Since all processing within the event scope of an Eventbox instance must not execute blocking operations, sync procs can only return immediate values. For deferred results use #yield_proc instead.


206
207
208
# File 'lib/eventbox.rb', line 206

def sync_proc(name=nil, &block)
  @__event_loop__.new_sync_proc(name=nil, &block)
end

#yield_proc(name = nil, &block) ⇒ Object (private)

Create a Proc object for calls with deferred result similar to Eventbox::Boxable#yield_call.

It can be passed to external scope and called from there like so:

class MyBox < Eventbox
  sync_call def print(p1)
    yield_proc do |p2, result|
      result.yield "#{p1} #{p2}"
    end
  end
end
puts MyBox.new.print("Hello").call("world")   # Prints "Hello world"

This proc type is simular to #sync_proc, however it's not the result of the block that is returned. Instead the block is called with one additional argument in the event scope, which is used to yield a result value. The result value can be yielded within the called block, but it can also be called by any other event scope or external method, leading to a deferred proc return. The external thread calling this proc is suspended until a result is yielded. However the Eventbox object keeps responsive to calls from other threads.

The created object can be safely called from any thread. If yield procs are called in the event scope, they must get a Proc object as the last argument. It is called when a result was yielded.

All block arguments as well as the result value are passed through the Sanitizer. The block itself might not do any blocking calls or expensive computations - this would impair responsiveness of the Eventbox instance. Instead use Eventbox::Boxable#action in these cases.


236
237
238
# File 'lib/eventbox.rb', line 236

def yield_proc(name=nil, &block)
  @__event_loop__.new_yield_proc(name=nil, &block)
end