several response body#close fixes
[rainbows.git] / lib / rainbows / sync_close.rb
bloba336262dad232ada35c8e478fcae7dc3cbddb2df
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'thread'
4 class Rainbows::SyncClose
5   def initialize(body)
6     @body = body
7     @mutex = Mutex.new
8     @cv = ConditionVariable.new
9     @mutex.synchronize do
10       yield self
11       @cv.wait(@mutex)
12     end
13   end
15   def respond_to?(m)
16     @body.respond_to?(m)
17   end
19   def to_path
20     @body.to_path
21   end
23   def each(&block)
24     @body.each(&block)
25   end
27   def to_io
28     @body.to_io
29   end
31   # called by the writer thread to wake up the original thread (in #initialize)
32   def close
33     @body.close
34     ensure
35       @mutex.synchronize { @cv.signal }
36   end
37 end