Sha256: 9cbd66cb7e89b84a2bf5b38a776e56c601d434f6a5239677d6180fa0905d362b

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

module Pione
  module Util
    # FreeThreadGenerator provides the function that creates new threads under
    # main thread group. This is useful for escaping threadgroup encloser.
    module FreeThreadGenerator
      @queue = Queue.new                        # task queue
      @method_lock = Mutex.new                  # method lock
      @mutex = Mutex.new                        # generator lock
      @cv_response = ConditionVariable.new      # response cv
      @cv_bye = ConditionVariable.new           # bye client cv
      @__generated__ = nil                      # generated thread(temporary variable)

      # Generate a thread with the block under default thread group.
      def self.generate(&b)
        @method_lock.synchronize do
          @mutex.synchronize do
            @queue.push(b)
            @cv_response.wait(@mutex)
            thread = @__generated__
            @__generated__ = nil
            @cv_bye.signal
            return thread
          end
        end
      end

      private

      # Start to run a loop for creating threadgroup free thread.
      def self.create_free_thread
        while true
          b = @queue.pop
          @mutex.synchronize do
            @__generated__ = Thread.new(&b)
            @cv_response.signal
            @cv_bye.wait(@mutex)
          end
        end
      end

      # we should wait interpret +create_free_thread+ method...

      @thread = Thread.new {create_free_thread} # a thread under default thread group
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pione-0.5.0 lib/pione/util/free-thread-generator.rb
pione-0.5.0.alpha.2 lib/pione/util/free-thread-generator.rb
pione-0.5.0.alpha.1 lib/pione/util/free-thread-generator.rb
pione-0.4.2 lib/pione/util/free-thread-generator.rb
pione-0.4.1 lib/pione/util/free-thread-generator.rb
pione-0.4.0 lib/pione/util/free-thread-generator.rb
pione-0.3.2 lib/pione/util/free-thread-generator.rb
pione-0.3.1 lib/pione/util/free-thread-generator.rb
pione-0.3.0 lib/pione/util/free-thread-generator.rb