Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
6c674089
Commit
6c674089
authored
Feb 05, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event/Loop: add flag `alive`
This replaces the old `dead` flag which was unreliable; it was `false` if the EventThread was not yet started, which could cause deadlocks in BlockingCall().
parent
261a816b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
12 deletions
+28
-12
Call.cxx
src/event/Call.cxx
+1
-1
Loop.cxx
src/event/Loop.cxx
+8
-4
Loop.hxx
src/event/Loop.hxx
+13
-7
Thread.cxx
src/event/Thread.cxx
+6
-0
No files found.
src/event/Call.cxx
View file @
6c674089
...
...
@@ -80,7 +80,7 @@ private:
void
BlockingCall
(
EventLoop
&
loop
,
std
::
function
<
void
()
>
&&
f
)
{
if
(
loop
.
IsDead
()
||
loop
.
IsInside
())
{
if
(
!
loop
.
IsAlive
()
||
loop
.
IsInside
())
{
/* we're already inside the loop - we can simply call
the function */
f
();
...
...
src/event/Loop.cxx
View file @
6c674089
...
...
@@ -25,7 +25,13 @@
EventLoop
::
EventLoop
(
ThreadId
_thread
)
:
SocketMonitor
(
*
this
),
quit
(
false
),
dead
(
false
),
/* if this instance is hosted by an EventThread (no ThreadId
known yet) then we're not yet alive until the thread is
started; for the main EventLoop instance, we assume it's
already alive, because nobody but EventThread will call
SetAlive() */
alive
(
!
_thread
.
IsNull
()),
quit
(
false
),
thread
(
_thread
)
{
SocketMonitor
::
Open
(
SocketDescriptor
(
wake_fd
.
Get
()));
...
...
@@ -143,12 +149,11 @@ EventLoop::Run() noexcept
assert
(
IsInside
());
assert
(
!
quit
);
assert
(
!
dead
);
assert
(
alive
);
assert
(
busy
);
SocketMonitor
::
Schedule
(
SocketMonitor
::
READ
);
AtScopeExit
(
this
)
{
dead
=
true
;
SocketMonitor
::
Cancel
();
};
...
...
@@ -215,7 +220,6 @@ EventLoop::Run() noexcept
}
while
(
!
quit
);
#ifndef NDEBUG
assert
(
!
dead
);
assert
(
busy
);
assert
(
thread
.
IsInside
());
#endif
...
...
src/event/Loop.hxx
View file @
6c674089
...
...
@@ -85,12 +85,15 @@ class EventLoop final : SocketMonitor
std
::
chrono
::
steady_clock
::
time_point
now
=
std
::
chrono
::
steady_clock
::
now
();
std
::
atomic_bool
quit
;
/**
* If this is true, then Run() has returned.
* Is this #EventLoop alive, i.e. can events be scheduled?
* This is used by BlockingCall() to determine whether
* schedule in the #EventThread or to call directly (if
* there's no #EventThread yet/anymore).
*/
std
::
atomic_bool
dead
;
bool
alive
;
std
::
atomic_bool
quit
;
/**
* True when the object has been modified and another check is
...
...
@@ -207,9 +210,12 @@ private:
bool
OnSocketReady
(
unsigned
flags
)
noexcept
override
;
public
:
gcc_pure
bool
IsDead
()
const
noexcept
{
return
dead
;
void
SetAlive
(
bool
_alive
)
noexcept
{
alive
=
_alive
;
}
bool
IsAlive
()
const
noexcept
{
return
alive
;
}
/**
...
...
src/event/Thread.cxx
View file @
6c674089
...
...
@@ -26,8 +26,11 @@
void
EventThread
::
Start
()
{
assert
(
!
event_loop
.
IsAlive
());
assert
(
!
thread
.
IsDefined
());
event_loop
.
SetAlive
(
true
);
thread
.
Start
();
}
...
...
@@ -35,6 +38,9 @@ void
EventThread
::
Stop
()
noexcept
{
if
(
thread
.
IsDefined
())
{
assert
(
event_loop
.
IsAlive
());
event_loop
.
SetAlive
(
false
);
event_loop
.
Break
();
thread
.
Join
();
}
...
...
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