From 28d571b7cca709641d964e00e6004facb6bfcc7e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 4 Feb 2009 14:30:40 -0800 Subject: s/Mongrel/Unicorn/g Avoid conflicting with existing Mongrel libraries since we'll be incompatible and break things w/o disrupting Mongrel installations. --- lib/unicorn/semaphore.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/unicorn/semaphore.rb (limited to 'lib/unicorn/semaphore.rb') diff --git a/lib/unicorn/semaphore.rb b/lib/unicorn/semaphore.rb new file mode 100644 index 0000000..1c0b87c --- /dev/null +++ b/lib/unicorn/semaphore.rb @@ -0,0 +1,46 @@ +class Semaphore + def initialize(resource_count = 0) + @available_resource_count = resource_count + @mutex = Mutex.new + @waiting_threads = [] + end + + def wait + make_thread_wait unless resource_is_available + end + + def signal + schedule_waiting_thread if thread_is_waiting + end + + def synchronize + self.wait + yield + ensure + self.signal + end + + private + + def resource_is_available + @mutex.synchronize do + return (@available_resource_count -= 1) >= 0 + end + end + + def make_thread_wait + @waiting_threads << Thread.current + Thread.stop + end + + def thread_is_waiting + @mutex.synchronize do + return (@available_resource_count += 1) <= 0 + end + end + + def schedule_waiting_thread + thread = @waiting_threads.shift + thread.wakeup if thread + end +end -- cgit v1.2.3-24-ge0c7