Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c5905f39
Commit
c5905f39
authored
Jun 17, 2019
by
Joost Lek
Committed by
Victor Stinner
Jun 17, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36688: Adding an implementation of RLock in _dummy_thread (GH-12943)
parent
28fca0c4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
1 deletion
+52
-1
Lib/_dummy_thread.py
Lib/_dummy_thread.py
+31
-1
Lib/test/test_dummy_thread.py
Lib/test/test_dummy_thread.py
+21
-0
No files found.
Lib/_dummy_thread.py
View file @
c5905f39
...
...
@@ -14,7 +14,7 @@ Suggested usage is::
# Exports only things specified by thread documentation;
# skipping obsolete synonyms allocate(), start_new(), exit_thread().
__all__
=
[
'error'
,
'start_new_thread'
,
'exit'
,
'get_ident'
,
'allocate_lock'
,
'interrupt_main'
,
'LockType'
]
'interrupt_main'
,
'LockType'
,
'RLock'
]
# A dummy value
TIMEOUT_MAX
=
2
**
31
...
...
@@ -148,6 +148,36 @@ class LockType(object):
hex
(
id
(
self
))
)
class
RLock
(
LockType
):
"""Dummy implementation of threading._RLock.
Re-entrant lock can be aquired multiple times and needs to be released
just as many times. This dummy implemention does not check wheter the
current thread actually owns the lock, but does accounting on the call
counts.
"""
def
__init__
(
self
):
super
().
__init__
()
self
.
_levels
=
0
def
acquire
(
self
,
waitflag
=
None
,
timeout
=-
1
):
"""Aquire the lock, can be called multiple times in succession.
"""
locked
=
super
().
acquire
(
waitflag
,
timeout
)
if
locked
:
self
.
_levels
+=
1
return
locked
def
release
(
self
):
"""Release needs to be called once for every call to acquire().
"""
if
self
.
_levels
==
0
:
raise
error
if
self
.
_levels
==
1
:
super
().
release
()
self
.
_levels
-=
1
# Used to signal that interrupt_main was called in a "thread"
_interrupt
=
False
# True when not executing in a "thread"
...
...
Lib/test/test_dummy_thread.py
View file @
c5905f39
...
...
@@ -102,6 +102,24 @@ class LockTests(unittest.TestCase):
self
.
assertIn
(
"unlocked"
,
repr
(
self
.
lock
))
class
RLockTests
(
unittest
.
TestCase
):
"""Test dummy RLock objects."""
def
setUp
(
self
):
self
.
rlock
=
_thread
.
RLock
()
def
test_multiple_acquire
(
self
):
self
.
assertIn
(
"unlocked"
,
repr
(
self
.
rlock
))
self
.
rlock
.
acquire
()
self
.
rlock
.
acquire
()
self
.
assertIn
(
"locked"
,
repr
(
self
.
rlock
))
self
.
rlock
.
release
()
self
.
assertIn
(
"locked"
,
repr
(
self
.
rlock
))
self
.
rlock
.
release
()
self
.
assertIn
(
"unlocked"
,
repr
(
self
.
rlock
))
self
.
assertRaises
(
RuntimeError
,
self
.
rlock
.
release
)
class
MiscTests
(
unittest
.
TestCase
):
"""Miscellaneous tests."""
...
...
@@ -253,3 +271,6 @@ class ThreadTests(unittest.TestCase):
func
=
mock
.
Mock
(
side_effect
=
Exception
)
_thread
.
start_new_thread
(
func
,
tuple
())
self
.
assertTrue
(
mock_print_exc
.
called
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment