Class: Rake::Promise

Inherits:
Object
  • Object
show all
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 collapse

NOT_SET =

:nodoc: all

Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, &block) ⇒ Promise

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



16
17
18
19
20
21
22
# File 'lib/rake/promise.rb', line 16

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

Instance Attribute Details

#recorderObject

:nodoc:



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

def recorder
  @recorder
end

Instance Method Details

#valueObject

Return the value of this promise.

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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rake/promise.rb', line 28

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

#workObject

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rake/promise.rb', line 41

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