minor: switched from Monitor to Mutex; passing tests in 1.9.1

This commit is contained in:
Kyle Banker 2009-12-28 13:05:45 -05:00
parent 5ccfcf95ab
commit 23c8b9d45f

View File

@ -16,7 +16,7 @@
require 'set' require 'set'
require 'socket' require 'socket'
require 'monitor' require 'thread'
module Mongo module Mongo
@ -107,14 +107,11 @@ module Mongo
@size = options[:pool_size] || 1 @size = options[:pool_size] || 1
@timeout = options[:timeout] || 5.0 @timeout = options[:timeout] || 5.0
# Number of seconds to wait for threads to signal availability.
@thread_timeout = @timeout >= 5.0 ? (@timeout / 4.0) : 1.0
# Mutex for synchronizing pool access # Mutex for synchronizing pool access
@connection_mutex = Monitor.new @connection_mutex = Mutex.new
# Condition variable for signal and wait # Condition variable for signal and wait
@queue = @connection_mutex.new_cond @queue = ConditionVariable.new
@sockets = [] @sockets = []
@checked_out = [] @checked_out = []
@ -300,8 +297,8 @@ module Mongo
def checkin(socket) def checkin(socket)
@connection_mutex.synchronize do @connection_mutex.synchronize do
@checked_out.delete(socket) @checked_out.delete(socket)
@queue.signal
end end
@queue.signal
true true
end end
@ -352,23 +349,11 @@ module Mongo
end end
return socket if socket return socket if socket
wait
end
end
end
if RUBY_VERSION >= '1.9' # Otherwise, wait
# Ruby 1.9's Condition Variables don't support timeouts yet; @queue.wait(@connection_mutex)
# until they do, we'll make do with this hack.
def wait
Timeout.timeout(@thread_timeout) do
@queue.wait
end end
end end
else
def wait
@queue.wait(@thread_timeout)
end
end end
def receive(sock) def receive(sock)