diff --git a/src/flow/lib/models/service_pool.rb b/src/flow/lib/models/service_pool.rb index 54ba424a0e..34ada0c02f 100644 --- a/src/flow/lib/models/service_pool.rb +++ b/src/flow/lib/models/service_pool.rb @@ -19,9 +19,6 @@ module OpenNebula DOCUMENT_TYPE = 100 - @@mutex = Mutex.new - @@mutex_hash = Hash.new - # Class constructor # # @param [OpenNebula::Client] client the xml-rpc client @@ -51,42 +48,33 @@ module OpenNebula service_id = service_id.to_i if service_id service = Service.new_with_id(service_id, @client) - rc = service.info + if block_given? + locked = false - if OpenNebula.is_error?(rc) - return rc + while !locked + locked = service.lock() + + if OpenNebula.is_error?(locked) + return locked + end + + sleep 1 + end + + rc = service.info + + if OpenNebula.is_error?(rc) + return rc + end + + block.call(service) + + service.unlock() else - if block_given? - obj_mutex = nil - entry = nil + rc = service.info - @@mutex.synchronize { - # entry is an array of [Mutex, waiting] - # waiting is the number of threads waiting on this mutex - entry = @@mutex_hash[service_id] - - if entry.nil? - entry = [Mutex.new, 0] - @@mutex_hash[service_id] = entry - end - - obj_mutex = entry[0] - entry[1] = entry[1] + 1 - - if @@mutex_hash.size > 10000 - @@mutex_hash.delete_if { |s_id, entry| - entry[1] == 0 - } - end - } - - obj_mutex.synchronize { - block.call(service) - } - - @@mutex.synchronize { - entry[1] = entry[1] - 1 - } + if OpenNebula.is_error?(rc) + return rc end return service