History log of /external/guice/core/src/com/google/inject/internal/SingletonScope.java
Revision Date Author Comments (<<< Hide modified files) (Show modified files >>>)
5e6c93348c4250012801b6e41753789d760f06e4 20-Apr-2015 timofeyb <timofeyb@google.com> Implement more granular locks for a Singleton scope in Guice.

Now when you can create two independent singletons using
the same injector in different threads.
This make it easy to create scopes creating singletons using
thread pools with all the concurrency being done by Guice.
As a nice side effect Singleton scope is no longer treated
specially in Guice codebase.

The obvious problem to solve is potential deadlocks:
A requires B, B requires C, C requires A where all are
singletons and all are created simultaneously.
It's impossible to detect this deadlock using information
within one thread, so we have to have a shared storage.

An idea is to have a map of creators' locks and a map
of which threads are waiting for other singletons to be created.
Using this information circular dependencies are trivially
discovered within O(N) where N is a number of concurrent threads.

Important to not that no other deadlock scenarios within
Guice code is introduced as Guice does not expose any
other scopes that can span several threads.

Now it would be possible for
client code to deadlock on itself with two lazy singletons
calling each other's providers during creation.
This is deemed as a non-issue as it is up to the client
to write a thread-safe code.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=91610630
/external/guice/core/src/com/google/inject/internal/SingletonScope.java
d7aa953d088f4955789051414bcd6134437afa17 25-Oct-2014 timofeyb <timofeyb@google.com> Making Singleton's creation lock less coarse.

Singleton is defined as a scope which creates no more than one
object per injector. It's highly confusing when you catch a deadlock
between two unrelated injectors due to the same class injection.

Problem is demonstrated using a test that recreates scenario when
one thread injecting class can block other thread to use its own
injector.

Proposed solution is to use Injector-wide locks in a singleton.
ThreadLocal as a way to store current state.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=78469951
/external/guice/core/src/com/google/inject/internal/SingletonScope.java