Skip to content

Commit

Permalink
singleton lite instantiation is thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
kianzarrin committed Oct 19, 2022
1 parent 2efa5e8 commit d77d1cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
12 changes: 7 additions & 5 deletions LoadOrder/CO/SingletonLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

namespace CO {
public abstract class SingletonLite<T> where T : new() {
private static T sInstance;

protected static T sInstance;
private static object lockObject = new object();
public static T instance {
get {
if (sInstance == null) {
sInstance = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
Log.Debug("Creating singleton of type " + typeof(T).Name);
lock (lockObject) {
sInstance = new T();
Log.Debug("Creating singleton of type " + typeof(T).Name);
}
}
return sInstance;
}
}

public static bool exists => sInstance != null;

public static void Ensure() =>_ = instance;
public static void Ensure() => _ = instance;
}
}
17 changes: 11 additions & 6 deletions LoadOrder/CO/settings/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class GameSettings : SingletonLite<GameSettings> {

private static void MonitorSave() {
try {
Log.Info("GameSettings Monitor Started...");
while (GameSettings.m_Run) {
GameSettings.SaveAll();
lock (GameSettings.m_LockObject) {
Expand All @@ -96,12 +97,16 @@ public class GameSettings : SingletonLite<GameSettings> {
}

public GameSettings() {
Log.Info("GameSettings Monitor Started...");
GameSettings.m_SaveThread = new Thread(new ThreadStart(GameSettings.MonitorSave));
GameSettings.m_SaveThread.Name = "SaveSettingsThread";
GameSettings.m_SaveThread.IsBackground = true;
GameSettings.m_Run = true;
GameSettings.m_SaveThread.Start();
try {
sInstance = this;
Log.Info("Creating GameSettings Monitor ...");
Log.Debug(Environment.StackTrace);
GameSettings.m_SaveThread = new Thread(new ThreadStart(GameSettings.MonitorSave));
GameSettings.m_SaveThread.Name = "SaveSettingsThread";
GameSettings.m_SaveThread.IsBackground = true;
GameSettings.m_Run = true;
GameSettings.m_SaveThread.Start();
} catch (Exception ex) { ex.Log(); }
}

~GameSettings() => Terminate();
Expand Down
1 change: 1 addition & 0 deletions LoadOrder/Data/ConfigWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ConfigWrapper : SingletonLite<ConfigWrapper> {
object m_LockObject = new object();

public ConfigWrapper() {
sInstance = this;
var sw = System.Diagnostics.Stopwatch.StartNew();
Config = LoadOrderConfig.Deserialize(DataLocation.LocalLOMData)
?? new LoadOrderConfig();
Expand Down
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<AssemblyVersion>1.12.7.*</AssemblyVersion>
<AssemblyVersion>1.12.8.*</AssemblyVersion>
</PropertyGroup>
</Project>

0 comments on commit d77d1cb

Please sign in to comment.