PHP 8.2.31
Preview: promise.rb Size: 2.33 KB
//proc/thread-self/root/opt/alt/ruby20/lib64/ruby/2.0.0/rake/promise.rb

module Rake

  # 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.
  #
  class Promise               # :nodoc: all
    NOT_SET = Object.new.freeze # :nodoc:

    attr_accessor :recorder

    # Create a promise to do the chore specified by the block.
    def initialize(args, &block)
      @mutex = Mutex.new
      @result = NOT_SET
      @error = NOT_SET
      @args = args.collect { |a| begin; a.dup; rescue; a; end }
      @block = block
    end

    # Return the value of this promise.
    #
    # If the promised chore is not yet complete, then do the work
    # synchronously. We will wait.
    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

    # If no one else is working this promise, go ahead and do the chore.
    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

    private

    # Perform the chore promised
    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

    # Do we have a result for the promise
    def result?
      ! @result.equal?(NOT_SET)
    end

    # Did the promise throw an error
    def error?
      ! @error.equal?(NOT_SET)
    end

    # Are we done with the promise
    def complete?
      result? || error?
    end

    # free up these items for the GC
    def discard
      @args = nil
      @block = nil
    end

    # Record execution statistics if there is a recorder
    def stat(*args)
      @recorder.call(*args) if @recorder
    end

  end

end

Directory Contents

Dirs: 4 × Files: 42

Name Size Perms Modified Actions
contrib DIR
- drwxr-xr-x 2024-03-03 22:53:08
Edit Download
ext DIR
- drwxr-xr-x 2024-03-03 22:53:08
Edit Download
lib DIR
- drwxr-xr-x 2024-03-03 22:53:08
Edit Download
loaders DIR
- drwxr-xr-x 2024-03-03 22:53:08
Edit Download
3.14 KB lrw-r--r-- 2011-06-23 22:11:55
Edit Download
21.27 KB lrw-r--r-- 2012-11-29 19:16:46
Edit Download
622 B lrw-r--r-- 2012-11-30 03:21:06
Edit Download
408 B lrw-r--r-- 2011-06-28 02:45:29
Edit Download
1.00 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
482 B lrw-r--r-- 2012-11-15 21:59:37
Edit Download
164 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
5.02 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
273 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
670 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
11.50 KB lrw-r--r-- 2012-11-29 19:16:46
Edit Download
1.28 KB lrw-r--r-- 2011-06-23 22:11:55
Edit Download
3.01 KB lrw-r--r-- 2012-12-06 07:35:45
Edit Download
4.10 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
283 B lrw-r--r-- 2011-06-28 02:45:29
Edit Download
962 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
431 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
315 B lrw-r--r-- 2012-11-15 21:59:37
Edit Download
618 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
5.07 KB lrw-r--r-- 2011-06-23 22:11:55
Edit Download
26 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
351 B lrw-r--r-- 2012-11-15 22:32:34
Edit Download
364 B lrw-r--r-- 2012-11-15 22:01:58
Edit Download
2.33 KB lrw-r--r-- 2012-11-15 22:01:58
Edit Download
422 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
727 B lrw-r--r-- 2012-11-15 22:32:34
Edit Download
341 B lrw-r--r-- 2011-06-28 02:45:29
Edit Download
6.55 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
843 B lrw-r--r-- 2012-11-22 05:32:31
Edit Download
353 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
468 B lrw-r--r-- 2012-11-15 22:32:34
Edit Download
10.17 KB lrw-r--r-- 2012-11-29 19:16:46
Edit Download
580 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
1.63 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
119 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
8.78 KB lrw-r--r-- 2012-11-15 21:59:37
Edit Download
5.18 KB lrw-r--r-- 2012-11-15 22:32:34
Edit Download
1.10 KB lrw-r--r-- 2012-11-15 22:01:58
Edit Download
4.67 KB lrw-r--r-- 2012-11-15 22:01:58
Edit Download
488 B lrw-r--r-- 2012-11-29 19:16:46
Edit Download
187 B lrw-r--r-- 2012-12-21 02:34:37
Edit Download
1.53 KB lrw-r--r-- 2011-06-23 22:11:55
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).