rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 999f003779005a51009c6ab8c6cde55f86463ffe 598 bytes (raw)
$ git show HEAD:lib/rainbows/sync_close.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
# -*- encoding: binary -*-
# :enddoc:
require 'thread'
class Rainbows::SyncClose
  def initialize(body)
    @body = body
    @mutex = Mutex.new
    @cv = ConditionVariable.new
    @mutex.synchronize do
      yield self
      @cv.wait(@mutex)
    end
  end

  def respond_to?(m)
    @body.respond_to?(m)
  end

  def to_path
    @body.to_path
  end

  def each
    @body.each { |x| yield x }
  end

  def to_io
    @body.to_io
  end

  # called by the writer thread to wake up the original thread (in #initialize)
  def close
    @body.close
  ensure
    @mutex.synchronize { @cv.signal }
  end
end

git clone https://yhbt.net/rainbows.git