Class: ZeevexConcurrency::Future

Inherits:
Delayed
  • Object
show all
Includes:
Observable, Delayed::Bindable, Delayed::Cancellable, Delayed::LatchBased
Defined in:
lib/zeevex_concurrency/future.rb

Constant Summary collapse

@@worker_pool =

@@worker_pool = ZeevexConcurrency::EventLoop.new

ZeevexConcurrency::ThreadPool::FixedPool.new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delayed::Cancellable

#cancel, #cancelled?, #ready?

Methods included from Delayed::LatchBased

#ready?, #wait

Methods included from Delayed::Bindable

#bind, #binding, #bound?, #call, #execute

Methods inherited from Delayed

#exception, #exception?, #executed?, #set_result, #value, #wait

Constructor Details

#initialize(computation = nil, options = {}, &block) ⇒ Future

Returns a new instance of Future.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zeevex_concurrency/future.rb', line 17

def initialize(computation = nil, options = {}, &block)
  raise ArgumentError, "Must provide computation or block for a future" unless (computation || block)

  @mutex       = Mutex.new
  @exec_mutex  = Mutex.new
  @exception   = nil
  @done        = false
  @result      = false
  @executed    = false

  _initialize_latch

  # has to happen after exec_mutex initialized
  bind(computation, &block) if (computation || block)

  Array(options.delete(:observer) || options.delete(:observers)).each do |observer|
    add_observer observer
  end
end

Class Method Details

.create(callable = nil, options = {}, &block) ⇒ Object Also known as: future



41
42
43
44
45
46
# File 'lib/zeevex_concurrency/future.rb', line 41

def self.create(callable=nil, options = {}, &block)
  nfuture = ZeevexConcurrency::Future.new(callable, options, &block)
  (options.delete(:event_loop) || worker_pool).enqueue nfuture

  nfuture
end

.shutdownObject



37
38
39
# File 'lib/zeevex_concurrency/future.rb', line 37

def self.shutdown
  self.worker_pool.stop
end

.worker_poolObject



48
49
50
# File 'lib/zeevex_concurrency/future.rb', line 48

def self.worker_pool
  @@worker_pool
end

.worker_pool=(pool) ⇒ Object



52
53
54
# File 'lib/zeevex_concurrency/future.rb', line 52

def self.worker_pool=(pool)
  @@worker_pool = pool
end