thread-int.rb

Joel VanderWerf, 03/20/2013 02:21 AM

Download (1.1 KB)

 
1
$LOAD_PATH.unshift "lib"
2

    
3
# Expected output:
4
#
5
#   $ ruby thread-int.rb lose
6
#   read_all returned: []
7
#
8
#   $ ruby thread-int.rb
9
#   read_all returned: [[42]]
10

    
11
require 'rinda/rinda'
12

    
13
mode = ARGV.shift
14

    
15
rd, wr = IO.pipe
16

    
17
server = fork do
18
  rd.close
19
  require 'rinda/tuplespace'
20
  ts = Rinda::TupleSpace.new
21
  DRb.start_service(nil, ts)
22
  wr.puts DRb.uri
23
  DRb.thread.join
24
end
25

    
26
wr.close
27
uri = rd.gets.chomp
28

    
29
c1 = fork do
30
  DRb.start_service
31
  ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri(uri))
32
  th = Thread.new do
33
    case mode
34
    when "lose"
35
      result = ts.take([nil])
36
    else
37
      result = ts.take_safely([nil]) # Fixes problem!
38
    end
39
    $stderr.puts "take returned: #{result.inspect}"
40
  end
41
  sleep 0.1
42
  th.raise Interrupt # causes bug
43
  #DRb.stop_service # avoids bug, but not correct solution
44
  sleep
45
end
46

    
47
sleep 0.2
48

    
49
c2 = fork do
50
  DRb.start_service
51
  ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri(uri))
52
  ts.write([42])
53
  result = ts.read_all([nil])
54
  $stderr.puts "read_all returned: #{result.inspect}"
55
end
56

    
57
sleep 0.2
58

    
59
Process.wait c2
60
Process.kill "TERM", c1
61
Process.kill "TERM", server