Sha256: 80e3784cdaec5ffa306d7bd9e658ca9dea4c9f28d13638d38b2c55d3817bdb5a

Contents?: true

Size: 1.39 KB

Versions: 17

Compression:

Stored size: 1.39 KB

Contents

module Pione
  module Util
    class WaiterTable
      include DRbUndumped

      def initialize
        @mutex = Mutex.new
        @table = {}
        @waiting_thread = {}
      end

      def push(req_id, val)
        @mutex.synchronize {@table[req_id] = val}
        thread = @mutex.synchronize {@waiting_thread[req_id]}
        if thread && thread.status == "sleep"
          thread.run
        end
      end

      def take(req_id, msg_id, args)
        unless @mutex.synchronize {@table.has_key?(req_id)}
          @mutex.synchronize {@waiting_thread[req_id] = Thread.current}
          Thread.stop
          @mutex.synchronize {@waiting_thread.delete(req_id)}
        end
        return @mutex.synchronize {@table.delete(req_id)}
      end

      def to_s
        @mutex.synchronize do
          table = convert_string(@table)
          waiting = convert_string(@waiting)
          "#<WaiterTable @table=%s @waiting=%s>" % [table, waiting]
        end
      end

      def convert_string(obj)
        case obj
        when Hash
          "{%s}" % obj.map do |k,v|
            "%s=>%s" % [convert_string(k), convert_string(v)]
          end.join(", ")
        when Array
          "[%s]" % obj.map{|elt| convert_string(elt)}.join(", ")
        when DRbObject
          "#<DRbObject @drburi=%s, @drbref=%s>" % [obj.__drburi, obj.__drbref]
        else
          obj.inspect
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pione-0.5.0 lib/pione/util/waiter-table.rb
pione-0.5.0.alpha.2 lib/pione/util/waiter-table.rb
pione-0.5.0.alpha.1 lib/pione/util/waiter-table.rb
pione-0.4.2 lib/pione/util/waiter-table.rb
pione-0.4.1 lib/pione/util/waiter-table.rb
pione-0.4.0 lib/pione/util/waiter-table.rb
pione-0.3.2 lib/pione/util/waiter-table.rb
pione-0.3.1 lib/pione/util/waiter-table.rb
pione-0.3.0 lib/pione/util/waiter-table.rb
pione-0.2.2 lib/pione/util/waiter-table.rb
pione-0.2.1 lib/pione/util/waiter-table.rb
pione-0.2.0 lib/pione/util/waiter-table.rb
pione-0.1.4 lib/pione/util/waiter-table.rb
pione-0.1.3 lib/pione/util/waiter-table.rb
pione-0.1.2 lib/pione/util/waiter-table.rb
pione-0.1.1 lib/pione/util/waiter-table.rb
pione-0.1.0 lib/pione/util/waiter-table.rb