Class: Rake::Promise

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/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.



17
18
19
20
21
22
23
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/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

#recorderObject

:nodoc:



14
15
16
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/promise.rb', line 14

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.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/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

#workObject

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/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