Please sign in to comment.
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
- Loading branch information...
Showing with 1,139 additions and 207 deletions.
- +2 −2 core/src/com/google/inject/internal/BindingProcessor.java
- +2 −3 core/src/com/google/inject/internal/BoundProviderFactory.java
- +12 −5 core/src/com/google/inject/internal/ConstructionContext.java
- +1 −3 core/src/com/google/inject/internal/ConstructorBindingImpl.java
- +5 −8 core/src/com/google/inject/internal/ConstructorInjector.java
- +292 −0 core/src/com/google/inject/internal/CycleDetectingLock.java
- +18 −11 core/src/com/google/inject/internal/DelegatingInvocationHandler.java
- +3 −0 core/src/com/google/inject/internal/Errors.java
- +0 −7 core/src/com/google/inject/internal/InheritingState.java
- +44 −7 core/src/com/google/inject/internal/InjectorImpl.java
- +14 −3 core/src/com/google/inject/internal/InternalContext.java
- +3 −4 core/src/com/google/inject/internal/InternalFactoryToInitializableAdapter.java
- +3 −4 core/src/com/google/inject/internal/ProvidedByInternalFactory.java
- +5 −10 core/src/com/google/inject/internal/ProviderInternalFactory.java
- +3 −8 core/src/com/google/inject/internal/Scoping.java
- +293 −47 core/src/com/google/inject/internal/SingletonScope.java
- +0 −6 core/src/com/google/inject/internal/State.java
- +338 −79 core/test/com/google/inject/ScopesTest.java
- +101 −0 core/test/com/google/inject/internal/CycleDetectingLockTest.java
Oops, something went wrong.