Skip to content

Commit

Permalink
closes #91 Add mutex around creation of System.instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmelt committed Mar 19, 2015
1 parent f3db0cf commit 3f2fb14
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/cosmos/system/system.rb
Expand Up @@ -62,6 +62,7 @@ class System
KNOWN_PATHS = ['LOGS', 'TMP', 'SAVED_CONFIG', 'TABLES', 'HANDBOOKS', 'PROCEDURES']

@@instance = nil
@@instance_mutex = Mutex.new

# Create a new System object. Note, this should not be called directly but
# you should instead use System.instance and treat this class as a
Expand All @@ -71,7 +72,6 @@ class System
# read. Be default this is <Cosmos::USERPATH>/config/system/system.txt
def initialize(filename = nil)
raise "Cosmos::System created twice" unless @@instance.nil?
@@instance = self
@targets = {}
@targets['UNKNOWN'] = Target.new('UNKNOWN')
@config = nil
Expand Down Expand Up @@ -115,6 +115,7 @@ def initialize(filename = nil)

@initial_filename = filename
@initial_config = nil
@@instance = self
end

# @return [String] Configuration name
Expand Down Expand Up @@ -194,8 +195,11 @@ def self.limits_set=(limits_set)

# @return [System] The System singleton
def self.instance(filename = nil)
@@instance ||= self.new(filename)
return @@instance
return @@instance if @@instance
@@instance_mutex.synchronize do
@@instance ||= self.new(filename)
return @@instance
end
end

# Process the system.txt configuration file
Expand Down

0 comments on commit 3f2fb14

Please sign in to comment.