Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

Concurrent::Edge::Event

Represents an event which will happen in future (will be completed). It has to always happen.

Constants

COMPLETED

@!visibility private

PENDING

@!visibility private

Public Class Methods

new(promise, default_executor) click to toggle source
# File lib/concurrent/edge/future.rb, line 158
def initialize(promise, default_executor)
  @Promise         = promise
  @DefaultExecutor = default_executor
  @Touched         = AtomicBoolean.new(false)
  @Callbacks       = LockFreeStack.new
  @Waiters         = LockFreeStack.new # TODO replace with AtomicFixnum, avoid aba problem
  @State           = AtomicReference.new PENDING
  super()
  ensure_ivar_visibility!
end

Public Instance Methods

&(other) click to toggle source
Alias for: zip
add_callback(method, *args) click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 327
def add_callback(method, *args)
  if completed?
    call_callback method, *args
  else
    @Callbacks.push [method, *args]
    call_callbacks if completed?
  end
  self
end
blocks() click to toggle source

@!visibility private just for inspection @return [Array<AbstractPromise>]

# File lib/concurrent/edge/future.rb, line 314
def blocks
  @Callbacks.each_with_object([]) do |callback, promises|
    promises.push *callback.select { |v| v.is_a? AbstractPromise }
  end
end
callbacks() click to toggle source

@!visibility private just for inspection

# File lib/concurrent/edge/future.rb, line 322
def callbacks
  @Callbacks.each.to_a
end
chain(executor = nil, &callback) click to toggle source

@yield [success, value, reason] of the parent

# File lib/concurrent/edge/future.rb, line 220
def chain(executor = nil, &callback)
  ChainPromise.new(self, @DefaultExecutor, executor || @DefaultExecutor, &callback).future
end
Also aliased as: then
chain_completable(completable_event) click to toggle source
# File lib/concurrent/edge/future.rb, line 226
def chain_completable(completable_event)
  on_completion! { completable_event.complete_with COMPLETED }
end
Also aliased as: tangle
complete?(state = @State.get) click to toggle source
Alias for: completed?
complete_with(state, raise_on_reassign = true) click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 298
def complete_with(state, raise_on_reassign = true)
  if @State.compare_and_set(PENDING, state)
    (state)
    # go to synchronized block only if there were waiting threads
    synchronize { ns_broadcast } if @Waiters.clear
    call_callbacks
  else
    Concurrent::MultipleAssignmentError.new('Event can be completed only once') if raise_on_reassign
    return false
  end
  self
end
completed?(state = @State.get) click to toggle source

Has the Event been completed? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 188
def completed?(state = @State.get)
  state.completed?
end
Also aliased as: complete?
default_executor() click to toggle source

@return [Executor] current default executor @see with_default_executor

# File lib/concurrent/edge/future.rb, line 215
def default_executor
  @DefaultExecutor
end
delay() click to toggle source

Inserts delay into the chain of Futures making rest of it lazy evaluated. @return [Event]

# File lib/concurrent/edge/future.rb, line 246
def delay
  ZipEventEventPromise.new(self, Delay.new(@DefaultExecutor).event, @DefaultExecutor).event
end
incomplete?(state = @State.get) click to toggle source
Alias for: pending?
inspect() click to toggle source
# File lib/concurrent/edge/future.rb, line 288
def inspect
  "#{to_s[0..-2]} blocks:[#{blocks.map(&:to_s).join(', ')}]>"
end
internal_state() click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 356
def internal_state
  @State.get
end
on_completion(executor = nil, &callback) click to toggle source

@yield [success, value, reason] executed async on `executor` when completed @return self

# File lib/concurrent/edge/future.rb, line 268
def on_completion(executor = nil, &callback)
  add_callback :pr_async_callback_on_completion, executor || @DefaultExecutor, callback
end
on_completion!(&callback) click to toggle source

@yield [success, value, reason] executed sync when completed @return self

# File lib/concurrent/edge/future.rb, line 274
def on_completion!(&callback)
  add_callback :pr_callback_on_completion, callback
end
pending?(state = @State.get) click to toggle source

Is Event/Future pending? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 176
def pending?(state = @State.get)
  !state.completed?
end
Also aliased as: incomplete?
promise() click to toggle source

@!visibility private only for inspection

# File lib/concurrent/edge/future.rb, line 339
def promise
  @Promise
end
set(*args, &block) click to toggle source
# File lib/concurrent/edge/future.rb, line 292
def set(*args, &block)
  raise 'Use CompletableEvent#complete or CompletableFuture#complete instead, ' +
            'constructed by Concurrent.event or Concurrent.future respectively.'
end
state() click to toggle source

@return [:pending, :completed]

# File lib/concurrent/edge/future.rb, line 170
def state
  @State.get.to_sym
end
tangle(completable_event) click to toggle source
Alias for: chain_completable
then(executor = nil, &callback) click to toggle source
Alias for: chain
then_select(*channels) click to toggle source

Zips with selected value form the suplied channels @return [Future]

# File lib/concurrent/edge/future.rb, line 262
def then_select(*channels)
  ZipFutureEventPromise(Concurrent.select(*channels), self, @DefaultExecutor).future
end
to_s() click to toggle source
# File lib/concurrent/edge/future.rb, line 284
def to_s
  "<##{self.class}:0x#{'%x' % (object_id << 1)} #{state.to_sym}>"
end
touch() click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 207
def touch
  # distribute touch to promise only once
  @Promise.touch if @Touched.make_true
  self
end
touched() click to toggle source

@!visibility private only for inspection

# File lib/concurrent/edge/future.rb, line 345
def touched
  @Touched.value
end
unscheduled?() click to toggle source
# File lib/concurrent/edge/future.rb, line 180
def unscheduled?
  raise 'unsupported'
end
wait(timeout = nil) click to toggle source

Wait until Event is complete? @param [Numeric] timeout the maximum time in second to wait. @return [Event, true, false] self or true/false if timeout is used @!macro [attach] edge.periodical_wait

@note a thread should wait only once! For repeated checking use faster `completed?` check.
  If thread waits periodically it will dangerously grow the waiters stack.
# File lib/concurrent/edge/future.rb, line 200
def wait(timeout = nil)
  touch
  result = wait_until_complete(timeout)
  timeout ? result : self
end
waiting_threads() click to toggle source

@!visibility private only for debugging inspection

# File lib/concurrent/edge/future.rb, line 351
def waiting_threads
  @Waiters.each.to_a
end
with_default_executor(executor) click to toggle source

Changes default executor for rest of the chain @return [Event]

# File lib/concurrent/edge/future.rb, line 280
def with_default_executor(executor)
  EventWrapperPromise.new(self, executor).future
end
zip(other) click to toggle source

Zip with future producing new Future @return [Event]

# File lib/concurrent/edge/future.rb, line 234
def zip(other)
  if other.is?(Future)
    ZipFutureEventPromise.new(other, self, @DefaultExecutor).future
  else
    ZipEventEventPromise.new(self, other, @DefaultExecutor).future
  end
end
Also aliased as: &

[Validate]

Generated with the Darkfish Rdoc Generator 2.