PHP 8.2.31
Preview: promise.rb Size: 2.28 KB
/proc/thread-self/root/opt/alt/ruby22/lib64/ruby/2.2.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
      @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: 45

Name Size Perms Modified Actions
contrib DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
ext DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
lib DIR
- drwxr-xr-x 2023-07-26 14:31:55
Edit Download
loaders DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
3.17 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
23.14 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
868 B lrw-r--r-- 2014-08-16 03:56:17
Edit Download
1.93 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
418 B lrw-r--r-- 2014-09-06 09:31:37
Edit Download
2.83 KB lrw-r--r-- 2014-11-05 05:19:15
Edit Download
235 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
5.64 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
340 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
670 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
12.13 KB lrw-r--r-- 2014-09-06 09:31:37
Edit Download
1.26 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
3.66 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
4.05 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
144 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.16 KB lrw-r--r-- 2014-08-27 12:21:41
Edit Download
431 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
265 B lrw-r--r-- 2014-11-25 07:03:58
Edit Download
2.27 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
315 B lrw-r--r-- 2012-11-15 21:59:37
Edit Download
673 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
5.28 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
53 B lrw-r--r-- 2014-07-15 03:07:37
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.28 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
375 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
770 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
341 B lrw-r--r-- 2011-06-28 02:45:29
Edit Download
153 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
897 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
353 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
594 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
882 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
11.02 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
618 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
2.15 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
119 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
8.84 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
5.55 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.11 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
4.78 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
543 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
156 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.55 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download

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