Sha256: 48131bafb9d6d33b5b6d3a604113eed213af0eb13ea86bb58e75ac1791593733

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

require 'thread'

require 'concurrent/global_thread_pool'
require 'concurrent/obligation'
require 'concurrent/utilities'

module Concurrent

  class Future
    include Obligation
    behavior(:future)

    def initialize(*args, &block)

      unless block_given?
        @state = :fulfilled
      else
        @value = nil
        @state = :pending
        $GLOBAL_THREAD_POOL.post do
          Thread.pass
          work(*args, &block)
        end
      end
    end

    private

    # @private
    def work(*args) # :nodoc:
      semaphore.synchronize do
        begin
          atomic {
            @value = yield(*args)
            @state = :fulfilled
          }
        rescue Exception => ex
          atomic {
            @state = :rejected
            @reason = ex
          }
        end
      end
    end
  end
end

module Kernel

  def future(*args, &block)
    return Concurrent::Future.new(*args, &block)
  end
  module_function :future
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
concurrent-ruby-0.1.1 lib/concurrent/future.rb