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
30a5dd26
Commit
30a5dd26
authored
Aug 29, 2017
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event/TimeoutMonitor: move code to new class TimerEvent
parent
2f0d6833
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
115 additions
and
40 deletions
+115
-40
Makefile.am
Makefile.am
+2
-1
Loop.cxx
src/event/Loop.cxx
+5
-5
Loop.hxx
src/event/Loop.hxx
+10
-10
TimeoutMonitor.hxx
src/event/TimeoutMonitor.hxx
+12
-21
TimerEvent.cxx
src/event/TimerEvent.cxx
+3
-3
TimerEvent.hxx
src/event/TimerEvent.hxx
+83
-0
No files found.
Makefile.am
View file @
30a5dd26
...
@@ -514,7 +514,8 @@ libevent_a_SOURCES = \
...
@@ -514,7 +514,8 @@ libevent_a_SOURCES = \
src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx
\
src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx
\
src/event/PollResultGeneric.hxx
\
src/event/PollResultGeneric.hxx
\
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx
\
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx
\
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx
\
src/event/TimerEvent.hxx src/event/TimerEvent.cxx
\
src/event/TimeoutMonitor.hxx
\
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx
\
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx
\
src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx
\
src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx
\
src/event/DeferredCall.hxx
\
src/event/DeferredCall.hxx
\
...
...
src/event/Loop.cxx
View file @
30a5dd26
...
@@ -82,7 +82,7 @@ EventLoop::RemoveIdle(IdleMonitor &i)
...
@@ -82,7 +82,7 @@ EventLoop::RemoveIdle(IdleMonitor &i)
}
}
void
void
EventLoop
::
AddTimer
(
Time
outMonitor
&
t
,
std
::
chrono
::
steady_clock
::
duration
d
)
EventLoop
::
AddTimer
(
Time
rEvent
&
t
,
std
::
chrono
::
steady_clock
::
duration
d
)
{
{
assert
(
IsInside
());
assert
(
IsInside
());
...
@@ -92,7 +92,7 @@ EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d)
...
@@ -92,7 +92,7 @@ EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d)
}
}
void
void
EventLoop
::
CancelTimer
(
Time
outMonitor
&
t
)
EventLoop
::
CancelTimer
(
Time
rEvent
&
t
)
{
{
assert
(
IsInside
());
assert
(
IsInside
());
...
@@ -139,14 +139,14 @@ EventLoop::Run()
...
@@ -139,14 +139,14 @@ EventLoop::Run()
break
;
break
;
}
}
Time
outMonitor
&
m
=
*
i
;
Time
rEvent
&
t
=
*
i
;
timeout
=
m
.
due
-
now
;
timeout
=
t
.
due
-
now
;
if
(
timeout
>
timeout
.
zero
())
if
(
timeout
>
timeout
.
zero
())
break
;
break
;
timers
.
erase
(
i
);
timers
.
erase
(
i
);
m
.
Run
();
t
.
Run
();
if
(
quit
)
if
(
quit
)
return
;
return
;
...
...
src/event/Loop.hxx
View file @
30a5dd26
...
@@ -28,7 +28,7 @@
...
@@ -28,7 +28,7 @@
#include "thread/Mutex.hxx"
#include "thread/Mutex.hxx"
#include "WakeFD.hxx"
#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
#include "SocketMonitor.hxx"
#include "Time
outMonitor
.hxx"
#include "Time
rEvent
.hxx"
#include "IdleMonitor.hxx"
#include "IdleMonitor.hxx"
#include "DeferredMonitor.hxx"
#include "DeferredMonitor.hxx"
...
@@ -46,23 +46,23 @@
...
@@ -46,23 +46,23 @@
* thread that runs it, except where explicitly documented as
* thread that runs it, except where explicitly documented as
* thread-safe.
* thread-safe.
*
*
* @see SocketMonitor, MultiSocketMonitor, Time
outMonitor
, IdleMonitor
* @see SocketMonitor, MultiSocketMonitor, Time
rEvent
, IdleMonitor
*/
*/
class
EventLoop
final
:
SocketMonitor
class
EventLoop
final
:
SocketMonitor
{
{
WakeFD
wake_fd
;
WakeFD
wake_fd
;
struct
TimerCompare
{
struct
TimerCompare
{
constexpr
bool
operator
()(
const
Time
outMonitor
&
a
,
constexpr
bool
operator
()(
const
Time
rEvent
&
a
,
const
Time
outMonitor
&
b
)
const
{
const
Time
rEvent
&
b
)
const
{
return
a
.
due
<
b
.
due
;
return
a
.
due
<
b
.
due
;
}
}
};
};
typedef
boost
::
intrusive
::
multiset
<
Time
outMonitor
,
typedef
boost
::
intrusive
::
multiset
<
Time
rEvent
,
boost
::
intrusive
::
member_hook
<
Time
outMonitor
,
boost
::
intrusive
::
member_hook
<
Time
rEvent
,
Time
outMonitor
::
TimerSetHook
,
Time
rEvent
::
TimerSetHook
,
&
Time
outMonitor
::
timer_set_hook
>
,
&
Time
rEvent
::
timer_set_hook
>
,
boost
::
intrusive
::
compare
<
TimerCompare
>
,
boost
::
intrusive
::
compare
<
TimerCompare
>
,
boost
::
intrusive
::
constant_time_size
<
false
>>
TimerSet
;
boost
::
intrusive
::
constant_time_size
<
false
>>
TimerSet
;
TimerSet
timers
;
TimerSet
timers
;
...
@@ -155,9 +155,9 @@ public:
...
@@ -155,9 +155,9 @@ public:
void
AddIdle
(
IdleMonitor
&
i
);
void
AddIdle
(
IdleMonitor
&
i
);
void
RemoveIdle
(
IdleMonitor
&
i
);
void
RemoveIdle
(
IdleMonitor
&
i
);
void
AddTimer
(
Time
outMonitor
&
t
,
void
AddTimer
(
Time
rEvent
&
t
,
std
::
chrono
::
steady_clock
::
duration
d
);
std
::
chrono
::
steady_clock
::
duration
d
);
void
CancelTimer
(
Time
outMonitor
&
t
);
void
CancelTimer
(
Time
rEvent
&
t
);
/**
/**
* Schedule a call to DeferredMonitor::RunDeferred().
* Schedule a call to DeferredMonitor::RunDeferred().
...
...
src/event/TimeoutMonitor.hxx
View file @
30a5dd26
...
@@ -21,6 +21,7 @@
...
@@ -21,6 +21,7 @@
#define MPD_SOCKET_TIMEOUT_MONITOR_HXX
#define MPD_SOCKET_TIMEOUT_MONITOR_HXX
#include "check.h"
#include "check.h"
#include "TimerEvent.hxx"
#include <boost/intrusive/set_hook.hpp>
#include <boost/intrusive/set_hook.hpp>
...
@@ -37,38 +38,28 @@ class EventLoop;
...
@@ -37,38 +38,28 @@ class EventLoop;
* as thread-safe.
* as thread-safe.
*/
*/
class
TimeoutMonitor
{
class
TimeoutMonitor
{
friend
class
EventLoop
;
TimerEvent
timer
;
typedef
boost
::
intrusive
::
set_member_hook
<>
TimerSetHook
;
TimerSetHook
timer_set_hook
;
EventLoop
&
loop
;
/**
* When is this timer due? This is only valid if #active is
* true.
*/
std
::
chrono
::
steady_clock
::
time_point
due
;
public
:
public
:
TimeoutMonitor
(
EventLoop
&
_loop
)
TimeoutMonitor
(
EventLoop
&
_loop
)
:
loop
(
_loop
)
{
:
timer
(
_loop
,
BIND_THIS_METHOD
(
Run
))
{
}
~
TimeoutMonitor
()
{
Cancel
();
}
}
EventLoop
&
GetEventLoop
()
{
EventLoop
&
GetEventLoop
()
{
return
loop
;
return
timer
.
GetEventLoop
()
;
}
}
bool
IsActive
()
const
{
bool
IsActive
()
const
{
return
timer_set_hook
.
is_linked
();
return
timer
.
IsActive
();
}
void
Schedule
(
std
::
chrono
::
steady_clock
::
duration
d
)
{
timer
.
Schedule
(
d
);
}
}
void
Schedule
(
std
::
chrono
::
steady_clock
::
duration
d
);
void
Cancel
()
{
void
Cancel
();
timer
.
Cancel
();
}
protected
:
protected
:
virtual
void
OnTimeout
()
=
0
;
virtual
void
OnTimeout
()
=
0
;
...
...
src/event/Time
outMonitor
.cxx
→
src/event/Time
rEvent
.cxx
View file @
30a5dd26
...
@@ -18,18 +18,18 @@
...
@@ -18,18 +18,18 @@
*/
*/
#include "config.h"
#include "config.h"
#include "Time
outMonitor
.hxx"
#include "Time
rEvent
.hxx"
#include "Loop.hxx"
#include "Loop.hxx"
void
void
Time
outMonitor
::
Cancel
()
Time
rEvent
::
Cancel
()
{
{
if
(
IsActive
())
if
(
IsActive
())
loop
.
CancelTimer
(
*
this
);
loop
.
CancelTimer
(
*
this
);
}
}
void
void
Time
outMonitor
::
Schedule
(
std
::
chrono
::
steady_clock
::
duration
d
)
Time
rEvent
::
Schedule
(
std
::
chrono
::
steady_clock
::
duration
d
)
{
{
Cancel
();
Cancel
();
...
...
src/event/TimerEvent.hxx
0 → 100644
View file @
30a5dd26
/*
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_TIMER_EVENT_HXX
#define MPD_TIMER_EVENT_HXX
#include "check.h"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
#include <chrono>
class
EventLoop
;
/**
* This class invokes a callback function after a certain amount of
* time. Use Schedule() to start the timer or Cancel() to cancel it.
*
* This class is not thread-safe, all methods must be called from the
* thread that runs the #EventLoop, except where explicitly documented
* as thread-safe.
*/
class
TimerEvent
final
{
friend
class
EventLoop
;
typedef
boost
::
intrusive
::
set_member_hook
<>
TimerSetHook
;
TimerSetHook
timer_set_hook
;
EventLoop
&
loop
;
typedef
BoundMethod
<
void
()
>
Callback
;
const
Callback
callback
;
/**
* When is this timer due? This is only valid if IsActive()
* returns true.
*/
std
::
chrono
::
steady_clock
::
time_point
due
;
public
:
TimerEvent
(
EventLoop
&
_loop
,
Callback
_callback
)
:
loop
(
_loop
),
callback
(
_callback
)
{
}
~
TimerEvent
()
{
Cancel
();
}
EventLoop
&
GetEventLoop
()
{
return
loop
;
}
bool
IsActive
()
const
{
return
timer_set_hook
.
is_linked
();
}
void
Schedule
(
std
::
chrono
::
steady_clock
::
duration
d
);
void
Cancel
();
private
:
void
Run
()
{
callback
();
}
};
#endif
/* MAIN_NOTIFY_H */
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