123456789_123456789_123456789_123456789_123456789_

Class: Rake::Promise

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/rake/promise.rb

Overview

A Promise object represents a promise to do work (a chore) in the future. The promise is created with a block and a list of arguments for the block. Calling value will return the value of the promised chore.

Used by ThreadPool.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #value

    Return the value of this promise.

  • #work

    If no one else is working this promise, go ahead and do the chore.

  • #chore private

    Perform the chore promised.

  • #discard private

    free up these items for the GC.

  • #stat(*args) private

    Record execution statistics if there is a recorder.

Constructor Details

.new(args, &block) ⇒ Promise

Create a promise to do the chore specified by the block.

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 17

def initialize(args, &block)
  @mutex = Mutex.new
  @result = NOT_SET
  @error = NOT_SET
  @args = args
  @block = block
end

Instance Attribute Details

#complete?Boolean (readonly, private)

Are we done with the promise

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 83

def complete?
  result? || error?
end

#error?Boolean (readonly, private)

Did the promise throw an error

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 78

def error?
  !@error.equal?(NOT_SET)
end

#recorder (rw)

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 14

attr_accessor :recorder

#result?Boolean (readonly, private)

Do we have a result for the promise

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 73

def result?
  !@result.equal?(NOT_SET)
end

Instance Method Details

#chore (private)

Perform the chore promised

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 57

def chore
  if complete?
    stat :found_completed, item_id: object_id
    return
  end
  stat :will_execute, item_id: object_id
  begin
    @result = @block.call(*@args)
  rescue Exception => e
    @error = e
  end
  stat :did_execute, item_id: object_id
  discard
end

#discard (private)

free up these items for the GC

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 88

def discard
  @args = nil
  @block = nil
end

#stat(*args) (private)

Record execution statistics if there is a recorder

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 94

def stat(*args)
  @recorder.call(*args) if @recorder
end

#value

Return the value of this promise.

If the promised chore is not yet complete, then do the work synchronously. We will wait.

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 29

def value
  unless complete?
    stat :sleeping_on, item_id: object_id
    @mutex.synchronize do
      stat :has_lock_on, item_id: object_id
      chore
      stat :releasing_lock_on, item_id: object_id
    end
  end
  error? ? raise(@error) : @result
end

#work

If no one else is working this promise, go ahead and do the chore.

[ GitHub ]

  
# File 'lib/rake/promise.rb', line 42

def work
  stat :attempting_lock_on, item_id: object_id
  if @mutex.try_lock
    stat :has_lock_on, item_id: object_id
    chore
    stat :releasing_lock_on, item_id: object_id
    @mutex.unlock
  else
    stat :bailed_on, item_id: object_id
  end
end