Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
536e20e8
Commit
536e20e8
authored
Jul 01, 2019
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Windows lock->wait() not waking other waiters.
parent
0028d85c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
6 deletions
+43
-6
mutex-test.c++
c++/src/kj/mutex-test.c++
+22
-0
mutex.c++
c++/src/kj/mutex.c++
+18
-6
mutex.h
c++/src/kj/mutex.h
+3
-0
No files found.
c++/src/kj/mutex-test.c++
View file @
536e20e8
...
...
@@ -408,6 +408,28 @@ TEST(Mutex, WhenWithTimeoutPreciseTimingAfterInterrupt) {
KJ_FAIL_ASSERT
(
"time not within expected bounds even after retries"
);
}
KJ_TEST
(
"wait()s wake each other"
)
{
MutexGuarded
<
uint
>
value
(
0
);
{
kj
::
Thread
thread
([
&
]()
{
auto
lock
=
value
.
lockExclusive
();
++*
lock
;
lock
.
wait
([](
uint
value
)
{
return
value
==
2
;
});
++*
lock
;
lock
.
wait
([](
uint
value
)
{
return
value
==
4
;
});
});
{
auto
lock
=
value
.
lockExclusive
();
lock
.
wait
([](
uint
value
)
{
return
value
==
1
;
});
++*
lock
;
lock
.
wait
([](
uint
value
)
{
return
value
==
3
;
});
++*
lock
;
}
}
}
TEST
(
Mutex
,
Lazy
)
{
Lazy
<
uint
>
lazy
;
volatile
bool
initStarted
=
false
;
...
...
c++/src/kj/mutex.c++
View file @
536e20e8
...
...
@@ -447,13 +447,11 @@ void Mutex::lock(Exclusivity exclusivity) {
}
}
void
Mutex
::
unlock
(
Exclusivity
exclusivity
)
{
switch
(
exclusivity
)
{
case
EXCLUSIVE
:
{
KJ_DEFER
(
ReleaseSRWLockExclusive
(
&
coercedSrwLock
));
void
Mutex
::
wakeReadyWaiter
(
)
{
// Look for a waiter whose predicate is now evaluating true, and wake it. We wake no more than
// one waiter because only one waiter could get the lock anyway, and once it releases that lock
// it will awake the next waiter if necessary.
// Check if there are any conditional waiters. Note we only do this when unlocking an
// exclusive lock since under a shared lock the state couldn't have changed.
auto
nextWaiter
=
waitersHead
;
for
(;;)
{
KJ_IF_MAYBE
(
waiter
,
nextWaiter
)
{
...
...
@@ -477,6 +475,16 @@ void Mutex::unlock(Exclusivity exclusivity) {
break
;
}
}
}
void
Mutex
::
unlock
(
Exclusivity
exclusivity
)
{
switch
(
exclusivity
)
{
case
EXCLUSIVE
:
{
KJ_DEFER
(
ReleaseSRWLockExclusive
(
&
coercedSrwLock
));
// Check if there are any conditional waiters. Note we only do this when unlocking an
// exclusive lock since under a shared lock the state couldn't have changed.
wakeReadyWaiter
();
break
;
}
...
...
@@ -530,6 +538,10 @@ void Mutex::wait(Predicate& predicate, Maybe<Duration> timeout) {
}
while
(
!
predicate
.
check
())
{
// SleepConditionVariableSRW() will temporarily release the lock, so we need to signal other
// waiters that are now ready.
wakeReadyWaiter
();
if
(
SleepConditionVariableSRW
(
&
coercedCondvar
(
waiter
.
condvar
),
&
coercedSrwLock
,
sleepMs
,
0
))
{
// Normal result. Continue loop to check predicate.
}
else
{
...
...
c++/src/kj/mutex.h
View file @
536e20e8
...
...
@@ -133,6 +133,9 @@ private:
inline
void
addWaiter
(
Waiter
&
waiter
);
inline
void
removeWaiter
(
Waiter
&
waiter
);
bool
checkPredicate
(
Waiter
&
waiter
);
#if _WIN32
void
wakeReadyWaiter
();
#endif
};
class
Once
{
...
...
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