Sha256: b16af06af60195d1781e1dc0187e0270dc65555af11eb571b627b2f52def354e

Contents?: true

Size: 793 Bytes

Versions: 12

Compression:

Stored size: 793 Bytes

Contents

require "thread"

class FutureResource
  
  def initialize
    @resource_lock          = Monitor.new
    @resource_value_blocker = @resource_lock.new_cond
  end
  
  def set_yet?
    @resource_lock.synchronize { defined? @resource }
  end
  
  def resource
    @resource_lock.synchronize do
      @resource_value_blocker.wait unless defined? @resource
      @resource
    end
  end
  
  def resource=(resource)
    @resource_lock.synchronize do
      raise ResourceAlreadySetException if defined? @resource
      @resource = resource
      @resource_value_blocker.broadcast
      @resource_value_blocker = nil # Don't really need it anymore.
    end
  end
  
  class ResourceAlreadySetException < Exception
    def initialize
      super "Cannot set this resource twice!"
    end
  end
  
end

Version data entries

12 entries across 12 versions & 6 rubygems

Version Path
eric-adhearsion-0.8.0 lib/adhearsion/foundation/future_resource.rb
jicksta-adhearsion-0.7.999 lib/adhearsion/foundation/future_resource.rb
jicksta-adhearsion-0.8.0 lib/adhearsion/foundation/future_resource.rb
jicksta-adhearsion-0.8.2 lib/adhearsion/foundation/future_resource.rb
jicksta-adhearsion-0.8.3 lib/adhearsion/foundation/future_resource.rb
kenwiesner-adhearsioncw-0.8.3 lib/adhearsion/foundation/future_resource.rb
mtrudel-adhearsion-0.8.3 lib/adhearsion/foundation/future_resource.rb
sevenscale-adhearsion-0.8.0 lib/adhearsion/foundation/future_resource.rb
adhearsion-0.8.3 lib/adhearsion/foundation/future_resource.rb
adhearsion-0.8.0 lib/adhearsion/foundation/future_resource.rb
adhearsion-0.8.2 lib/adhearsion/foundation/future_resource.rb
adhearsion-0.8.1 lib/adhearsion/foundation/future_resource.rb