Class: Pione::Util::WaiterTable

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/pione/util/waiter-table.rb

Instance Method Summary collapse

Constructor Details

#initializeWaiterTable

Returns a new instance of WaiterTable.



6
7
8
9
10
# File 'lib/pione/util/waiter-table.rb', line 6

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

Instance Method Details

#convert_string(obj) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pione/util/waiter-table.rb', line 37

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

#push(req_id, val) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/pione/util/waiter-table.rb', line 12

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

#take(req_id, msg_id, args) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/pione/util/waiter-table.rb', line 20

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

#to_sObject



29
30
31
32
33
34
35
# File 'lib/pione/util/waiter-table.rb', line 29

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