Refactor storage locking of ThreadSafeSingleton

This commit is contained in:
Roman Mogilatov 2017-04-18 23:01:20 +03:00
parent 870809c836
commit 9ed806b0ca
5 changed files with 1597 additions and 1592 deletions

View File

@ -9,7 +9,8 @@ follows `Semantic versioning`_
Development version
-------------------
- No features.
.. - No features.
- Refactor storage locking of ``ThreadSafeSingleton`` provider.
3.4.1
-----

View File

@ -876,12 +876,12 @@ struct __pyx_obj_19dependency_injector_9providers_DelegatedSingleton {
*
* cdef class ThreadSafeSingleton(BaseSingleton): # <<<<<<<<<<<<<<
* cdef object __storage
* cdef object __lock
* cdef object __storage_lock
*/
struct __pyx_obj_19dependency_injector_9providers_ThreadSafeSingleton {
struct __pyx_obj_19dependency_injector_9providers_BaseSingleton __pyx_base;
PyObject *__pyx___storage;
PyObject *__pyx___lock;
PyObject *__pyx___storage_lock;
};
@ -1259,7 +1259,7 @@ static struct __pyx_vtabstruct_19dependency_injector_9providers_DelegatedSinglet
*
* cdef class ThreadSafeSingleton(BaseSingleton): # <<<<<<<<<<<<<<
* cdef object __storage
* cdef object __lock
* cdef object __storage_lock
*/
struct __pyx_vtabstruct_19dependency_injector_9providers_ThreadSafeSingleton {

File diff suppressed because it is too large Load Diff

View File

@ -101,7 +101,7 @@ cdef class DelegatedSingleton(Singleton):
cdef class ThreadSafeSingleton(BaseSingleton):
cdef object __storage
cdef object __lock
cdef object __storage_lock
cpdef object _provide(self, tuple args, dict kwargs)

View File

@ -24,12 +24,6 @@ else: # pragma: no cover
copy.deepcopy(obj.im_self, memo),
obj.im_class)
GLOBAL_LOCK = threading.RLock()
"""Global reentrant lock.
:type: :py:class:`threading.RLock`
"""
cdef class Provider(object):
"""Base provider class.
@ -1307,6 +1301,12 @@ cdef class DelegatedSingleton(Singleton):
cdef class ThreadSafeSingleton(BaseSingleton):
"""Thread-safe singleton provider."""
storage_lock = threading.RLock()
"""Storage reentrant lock.
:type: :py:class:`threading.RLock`
"""
def __init__(self, provides, *args, **kwargs):
"""Initializer.
@ -1320,7 +1320,7 @@ cdef class ThreadSafeSingleton(BaseSingleton):
:type kwargs: dict[str, object]
"""
self.__storage = None
self.__lock = GLOBAL_LOCK
self.__storage_lock = self.__class__.storage_lock
super(ThreadSafeSingleton, self).__init__(provides, *args, **kwargs)
def reset(self):
@ -1332,7 +1332,7 @@ cdef class ThreadSafeSingleton(BaseSingleton):
cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance."""
with self.__lock:
with self.__storage_lock:
if self.__storage is None:
self.__storage = __factory_call(self.__instantiator,
args, kwargs)