Sha256: cfaf3cae31f79a671e532130adb7956f425a0693d7e4208a4cd91b6ff0d8c5ae

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'thread'
require 'timeout'

require 'functional/behavior'

behavior_info(:future,
              state: 0,
              value: -1,
              reason: 0,
              pending?: 0,
              fulfilled?: 0,
              rejected?: 0)

behavior_info(:promise,
              state: 0,
              value: -1,
              reason: 0,
              pending?: 0,
              fulfilled?: 0,
              rejected?: 0,
              then: 0,
              rescue: -1)

module Concurrent

  module Obligation

    attr_reader :state
    attr_reader :reason

    # Has the obligation been fulfilled?
    # @return [Boolean]
    def fulfilled?() return(@state == :fulfilled); end
    alias_method :realized?, :fulfilled?

    # Has the promise been rejected?
    # @return [Boolean]
    def rejected?() return(@state == :rejected); end

    # Is obligation completion still pending?
    # @return [Boolean]
    def pending?() return(@state == :pending); end

    def value(timeout = nil)
      if timeout == 0 || ! pending?
        return @value
      elsif timeout.nil?
        return mutex.synchronize { v = @value }
      else
        begin
          return Timeout::timeout(timeout.to_f) {
            mutex.synchronize { v = @value }
          }
        rescue Timeout::Error => ex
          return nil
        end
      end
    end
    alias_method :deref, :value

    protected

    def mutex
      @mutex ||= Mutex.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
concurrent-ruby-0.2.1 lib/concurrent/obligation.rb