Change getting of singleton dependency container to be thread-safe

This commit is contained in:
Mike Bierlee 2015-03-28 15:04:34 +01:00
parent f2eabe9797
commit 8d268846ed

View file

@ -16,6 +16,7 @@ module poodinis.container;
import std.string; import std.string;
import std.array; import std.array;
import std.algorithm; import std.algorithm;
import std.concurrency;
debug { debug {
import std.stdio; import std.stdio;
@ -44,9 +45,6 @@ class ResolveException : Exception {
* You can still create new instances of this class for exceptional situations. * You can still create new instances of this class for exceptional situations.
*/ */
class DependencyContainer { class DependencyContainer {
private static DependencyContainer instance;
private Registration[][TypeInfo] registrations; private Registration[][TypeInfo] registrations;
private Registration[] autowireStack; private Registration[] autowireStack;
@ -273,10 +271,8 @@ class DependencyContainer {
/** /**
* Returns a global singleton instance of a dependency container. * Returns a global singleton instance of a dependency container.
*/ */
public static DependencyContainer getInstance() { public static shared(DependencyContainer) getInstance() {
if (instance is null) { static shared DependencyContainer instance;
instance = new DependencyContainer(); return initOnce!instance(new DependencyContainer());
}
return instance;
} }
} }