-
Notifications
You must be signed in to change notification settings - Fork 174
Custom lock/rlock classes owning the protected data #5102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
happz
wants to merge
2
commits into
main
Choose a base branch
from
threading-locks-own-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from tmt.utils._threading import Lock | ||
|
|
||
|
|
||
| def test_sanity() -> None: | ||
| some_data: list[str] = [] | ||
|
|
||
| lock: Lock[list[str]] = Lock(some_data) | ||
|
|
||
| assert lock._Lock__lock.locked() is False # type: ignore[attr-defined] | ||
| assert lock._Lock__value is some_data # type: ignore[attr-defined] | ||
|
|
||
| with lock as data: | ||
| assert lock._Lock__lock.locked() is True # type: ignore[attr-defined] | ||
| assert data is some_data | ||
|
|
||
| assert lock._Lock__lock.locked() is False # type: ignore[attr-defined] | ||
| assert lock._Lock__value is some_data # type: ignore[attr-defined] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """ | ||
| Threading and synchronization helpers. | ||
| """ | ||
|
|
||
| import threading | ||
| from typing import Generic, TypeVar | ||
|
|
||
| T = TypeVar('T') | ||
| LockT = TypeVar('LockT', threading.Lock, threading.RLock) | ||
|
|
||
|
|
||
| class _Lock(Generic[LockT, T]): | ||
| __lock: LockT | ||
| __value: T | ||
|
LecrisUT marked this conversation as resolved.
|
||
|
|
||
| def __init__(self, lock: LockT, value: T) -> None: | ||
| self.__lock = lock | ||
| self.__value = value | ||
|
|
||
| def __enter__(self) -> T: | ||
| self.__lock.acquire() | ||
|
|
||
| return self.__value | ||
|
|
||
| def __exit__(self, *args: object) -> None: | ||
| self.__lock.release() | ||
|
|
||
|
|
||
| class Lock(_Lock[threading.Lock, T]): | ||
| """ | ||
| A lock protecting its payload from unsynchronized access. | ||
|
|
||
| In functionality it is similar to :py:class:`threading.Lock`, but | ||
| bundles together the protected value and lock protecting it. | ||
| To get the value, code is forced to acquire the lock: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # A list, representing data shared between multiple threads, | ||
| # is not assigned to any global name. Instead, it is wrapped by | ||
| # the lock, and "borrowed" to caller using the context manager | ||
| # approach: | ||
| SHARED_DATA: Lock[list[str]] = Lock([]) | ||
|
|
||
| ... | ||
|
|
||
| # `data` below is the list given to `Lock()` above: | ||
| with SHARED_DATA as data: | ||
| data += [...] | ||
|
|
||
| Compared to :py:class:`RLock`, ``Lock`` is not reentrant, i.e. it | ||
| can be acquired by the same thread only once, another attempt to | ||
| acquire the lock while already holding it will end up with a | ||
| deadlock. See :py:class:`threading.RLock` for more details on its | ||
| reentrancy. | ||
| """ | ||
|
|
||
| def __init__(self, value: T) -> None: | ||
| super().__init__(threading.Lock(), value) | ||
|
|
||
|
|
||
| class RLock(_Lock[threading.RLock, T]): | ||
| """ | ||
| A reentrant variant of :py:class:`Lock`. | ||
|
|
||
| In functionality it is similar to :py:class:`threading.RLock`, but | ||
| bundles together the protected value and lock protecting it. | ||
| To get the value, code is forced to acquire the lock: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # A list, representing data shared between multiple threads, | ||
| # is not assigned to any global name. Instead, it is wrapped by | ||
| # the lock, and "borrowed" to caller using the context manager | ||
| # approach: | ||
| SHARED_DATA: RLock[list[str]] = RLock([]) | ||
|
|
||
| ... | ||
|
|
||
| # `data` below is the list given to `RLock()` above: | ||
| with SHARED_DATA as data: | ||
| data += [...] | ||
|
|
||
| Compared to :py:class:`Lock`, ``RLock`` is reentrant, i.e. it can | ||
| be reacquired by the same thread. See :py:class:`threading.RLock` | ||
| for more details on its reentrancy. | ||
| """ | ||
|
|
||
| def __init__(self, value: T) -> None: | ||
| super().__init__(threading.RLock(), value) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.