[yast-commit] <rest-service> master : use a Mutex for accessing the shared variables
ref: refs/heads/master commit de437369ca8af7f724b91d0ae4a653260c288ed8 Author: Ladislav Slezak <lslezak@novell.com> Date: Wed Nov 11 19:03:46 2009 +0100 use a Mutex for accessing the shared variables make the code thread safe using a Mutex object for accesing the shared variables --- plugins/patches/app/models/patch.rb | 34 ++++++++++++++++++++-------------- 1 files changed, 20 insertions(+), 14 deletions(-) diff --git a/plugins/patches/app/models/patch.rb b/plugins/patches/app/models/patch.rb index 37c1bfc..eb868d5 100644 --- a/plugins/patches/app/models/patch.rb +++ b/plugins/patches/app/models/patch.rb @@ -5,6 +5,7 @@ class Patch < Resolvable @@running = Hash.new @@done = Hash.new + @@mutex = Mutex.new def to_xml( options = {} ) super :patch_update, options @@ -38,31 +39,36 @@ class Patch < Resolvable # background reading doesn't work correctly if class reloading is active # (static class members are lost between requests) if background && !Rails.configuration.cache_classes - Rails.logger.info "Class reloading is active, cannot run background thread (set config.cache_classes = true)" + Rails.logger.info "Class reloading is active, cannot use background thread (set config.cache_classes = true)" background = false end if background - if @@done.has_key?(what) - Rails.logger.debug "Request #{what} is done" - @@running.delete(what) - return @@done.delete(what) - end + bs = nil + @@mutex.synchronize do + if @@done.has_key?(what) + Rails.logger.debug "Request #{what} is done" + return @@done.delete(what) + end - running = @@running[what] - if running - Rails.logger.debug "Request #{what} is already running: #{running.inspect}" - return [running] - end + running = @@running[what] + if running + Rails.logger.debug "Request #{what} is already running: #{running.inspect}" + return [running] + end - bs = BackgroundStatus.new - @@running[what] = bs + bs = BackgroundStatus.new + @@running[what] = bs + end Rails.logger.info "Starting background thread for reading patches..." Thread.new do res = do_find(what, bs) Rails.logger.info "*** Patches thread: Found #{res.size} applicable patches" - @@done[what] = res + @@mutex.synchronize do + @@running.delete(what) + @@done[what] = res + end end return [bs] -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
Ladislav Slezak