Class: Celluloid::Future
- Inherits:
-
Object
- Object
- Celluloid::Future
- Defined in:
- lib/celluloid/future.rb
Overview
Celluloid::Future objects allow methods and blocks to run in the background, their values requested later
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
Class Method Summary collapse
Instance Method Summary collapse
- #cancel(error) ⇒ Object
-
#execute(receiver, method, args, block) ⇒ Object
Execute the given method in future context.
-
#initialize(&block) ⇒ Future
constructor
A new instance of Future.
-
#ready? ⇒ Boolean
Check if this future has a value yet.
-
#signal(value) ⇒ Object
(also: #<<)
Signal this future with the given result value.
-
#value(timeout = nil) ⇒ Object
(also: #call)
Obtain the value for this Future.
Constructor Details
#initialize(&block) ⇒ Future
Returns a new instance of Future.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/celluloid/future.rb', line 28 def initialize(&block) @address = Celluloid.uuid @mutex = Mutex.new @ready = false @result = nil @forwards = nil @cancelled = false if block @call = Call::Sync.new(self, :call, args) Celluloid.internal_pool.get do begin @call.dispatch(block) rescue # Exceptions in blocks will get raised when the value is retrieved end end else @call = nil end end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
26 27 28 |
# File 'lib/celluloid/future.rb', line 26 def address @address end |
Class Method Details
.new(*args, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/celluloid/future.rb', line 7 def self.new(*args, &block) return super unless block future = new # task = Thread.current[:celluloid_task] # actor = Thread.current[:celluloid_actor] Internals::ThreadHandle.new(Celluloid.actor_system, :future) do begin # Thread.current[:celluloid_task] = task # Thread.current[:celluloid_actor] = actor call = Call::Sync.new(future, :call, args) call.dispatch(block) rescue # Exceptions in blocks will get raised when the value is retrieved end end future end |
Instance Method Details
#cancel(error) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/celluloid/future.rb', line 121 def cancel(error) response = ErrorResponse.new(@call, error) signal response @mutex.synchronize do @cancelled = true end end |
#execute(receiver, method, args, block) ⇒ Object
Execute the given method in future context
51 52 53 54 55 56 57 58 |
# File 'lib/celluloid/future.rb', line 51 def execute(receiver, method, args, block) @mutex.synchronize do fail "already calling" if @call @call = Call::Sync.new(self, method, args, block) end receiver << @call end |
#ready? ⇒ Boolean
Check if this future has a value yet
61 62 63 |
# File 'lib/celluloid/future.rb', line 61 def ready? @ready end |
#signal(value) ⇒ Object Also known as: <<
Signal this future with the given result value
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/celluloid/future.rb', line 104 def signal(value) return if @cancelled result = Result.new(value, self) @mutex.synchronize do fail "the future has already happened!" if @ready if @forwards @forwards.is_a?(Array) ? @forwards.each { |f| f << result } : @forwards << result end @result = result @ready = true end end |
#value(timeout = nil) ⇒ Object Also known as: call
Obtain the value for this Future
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/celluloid/future.rb', line 66 def value(timeout = nil) ready = result = nil begin @mutex.lock if @ready ready = true result = @result else case @forwards when Array @forwards << Celluloid.mailbox when NilClass @forwards = Celluloid.mailbox else @forwards = [@forwards, Celluloid.mailbox] end end ensure @mutex.unlock end unless ready result = Celluloid.receive(timeout) do |msg| msg.is_a?(Future::Result) && msg.future == self end end if result (result.respond_to?(:value)) ? result.value : result else fail TimedOut, "Timed out" end end |