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
35f6301d
Commit
35f6301d
authored
Aug 29, 2019
by
Raymond Hettinger
Committed by
GitHub
Aug 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-10978: Semaphores can release multiple threads at a time (GH-15588)
parent
0dac68f1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
13 deletions
+56
-13
Doc/library/threading.rst
Doc/library/threading.rst
+7
-4
Lib/test/lock_tests.py
Lib/test/lock_tests.py
+32
-0
Lib/threading.py
Lib/threading.py
+15
-9
Misc/NEWS.d/next/Library/2019-08-29-01-19-13.bpo-10978.J6FQYY.rst
...S.d/next/Library/2019-08-29-01-19-13.bpo-10978.J6FQYY.rst
+2
-0
No files found.
Doc/library/threading.rst
View file @
35f6301d
...
...
@@ -802,11 +802,14 @@ Semaphores also support the :ref:`context management protocol <with-locks>`.
.. versionchanged:: 3.2
The *timeout* parameter is new.
.. method:: release()
.. method:: release(n=1)
Release a semaphore, incrementing the internal counter by *n*. When it
was zero on entry and other threads are waiting for it to become larger
than zero again, wake up *n* of those threads.
Release a semaphore, incrementing the internal counter by one. When it
was zero on entry and another thread is waiting for it to become larger
than zero again, wake up that thread.
.. versionchanged:: 3.9
Added the *n* parameter to release multiple waiting threads at once.
.. class:: BoundedSemaphore(value=1)
...
...
Lib/test/lock_tests.py
View file @
35f6301d
...
...
@@ -663,6 +663,38 @@ class BaseSemaphoreTests(BaseTestCase):
b
.
wait_for_finished
()
self
.
assertEqual
(
sem_results
,
[
True
]
*
(
6
+
7
+
6
+
1
))
def
test_multirelease
(
self
):
sem
=
self
.
semtype
(
7
)
sem
.
acquire
()
results1
=
[]
results2
=
[]
phase_num
=
0
def
f
():
sem
.
acquire
()
results1
.
append
(
phase_num
)
sem
.
acquire
()
results2
.
append
(
phase_num
)
b
=
Bunch
(
f
,
10
)
b
.
wait_for_started
()
while
len
(
results1
)
+
len
(
results2
)
<
6
:
_wait
()
self
.
assertEqual
(
results1
+
results2
,
[
0
]
*
6
)
phase_num
=
1
sem
.
release
(
7
)
while
len
(
results1
)
+
len
(
results2
)
<
13
:
_wait
()
self
.
assertEqual
(
sorted
(
results1
+
results2
),
[
0
]
*
6
+
[
1
]
*
7
)
phase_num
=
2
sem
.
release
(
6
)
while
len
(
results1
)
+
len
(
results2
)
<
19
:
_wait
()
self
.
assertEqual
(
sorted
(
results1
+
results2
),
[
0
]
*
6
+
[
1
]
*
7
+
[
2
]
*
6
)
# The semaphore is still locked
self
.
assertFalse
(
sem
.
acquire
(
False
))
# Final release, to let the last thread finish
sem
.
release
()
b
.
wait_for_finished
()
def
test_try_acquire
(
self
):
sem
=
self
.
semtype
(
2
)
self
.
assertTrue
(
sem
.
acquire
(
False
))
...
...
Lib/threading.py
View file @
35f6301d
...
...
@@ -439,16 +439,19 @@ class Semaphore:
__enter__
=
acquire
def
release
(
self
):
"""Release a semaphore, incrementing the internal counter by one.
def
release
(
self
,
n
=
1
):
"""Release a semaphore, incrementing the internal counter by one
or more
.
When the counter is zero on entry and another thread is waiting for it
to become larger than zero again, wake up that thread.
"""
if
n
<
1
:
raise
ValueError
(
'n must be one or more'
)
with
self
.
_cond
:
self
.
_value
+=
1
self
.
_cond
.
notify
()
self
.
_value
+=
n
for
i
in
range
(
n
):
self
.
_cond
.
notify
()
def
__exit__
(
self
,
t
,
v
,
tb
):
self
.
release
()
...
...
@@ -475,8 +478,8 @@ class BoundedSemaphore(Semaphore):
Semaphore
.
__init__
(
self
,
value
)
self
.
_initial_value
=
value
def
release
(
self
):
"""Release a semaphore, incrementing the internal counter by one.
def
release
(
self
,
n
=
1
):
"""Release a semaphore, incrementing the internal counter by one
or more
.
When the counter is zero on entry and another thread is waiting for it
to become larger than zero again, wake up that thread.
...
...
@@ -485,11 +488,14 @@ class BoundedSemaphore(Semaphore):
raise a ValueError.
"""
if
n
<
1
:
raise
ValueError
(
'n must be one or more'
)
with
self
.
_cond
:
if
self
.
_value
>=
self
.
_initial_value
:
if
self
.
_value
+
n
>
self
.
_initial_value
:
raise
ValueError
(
"Semaphore released too many times"
)
self
.
_value
+=
1
self
.
_cond
.
notify
()
self
.
_value
+=
n
for
i
in
range
(
n
):
self
.
_cond
.
notify
()
class
Event
:
...
...
Misc/NEWS.d/next/Library/2019-08-29-01-19-13.bpo-10978.J6FQYY.rst
0 → 100644
View file @
35f6301d
Semaphores and BoundedSemaphores can now release more than one waiting
thread at a time.
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