tests: "wc -c" portability for *BSDs
[rainbows.git] / lib / rainbows / sync_close.rb
blob75f119eae36c00ee47db0a31be8e7f5463243056
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
24     @body.each { |x| yield x }
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