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
bb288f02
Commit
bb288f02
authored
Nov 28, 2013
by
Denis Krjuchkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event: introduce generic API for internal event loop
parent
f90abe95
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
149 additions
and
55 deletions
+149
-55
Makefile.am
Makefile.am
+2
-0
Loop.cxx
src/event/Loop.cxx
+14
-27
Loop.hxx
src/event/Loop.hxx
+6
-18
PollGroup.hxx
src/event/PollGroup.hxx
+29
-0
PollGroupEPoll.hxx
src/event/PollGroupEPoll.hxx
+91
-0
SocketMonitor.cxx
src/event/SocketMonitor.cxx
+2
-1
SocketMonitor.hxx
src/event/SocketMonitor.hxx
+5
-9
No files found.
Makefile.am
View file @
bb288f02
...
@@ -306,6 +306,8 @@ libsystem_a_SOURCES = \
...
@@ -306,6 +306,8 @@ libsystem_a_SOURCES = \
libevent_a_SOURCES
=
\
libevent_a_SOURCES
=
\
src/event/WakeFD.hxx
\
src/event/WakeFD.hxx
\
src/event/PollGroup.hxx
\
src/event/PollGroupEPoll.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/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx
\
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx
\
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx
\
...
...
src/event/Loop.cxx
View file @
bb288f02
...
@@ -33,9 +33,6 @@ EventLoop::EventLoop(Default)
...
@@ -33,9 +33,6 @@ EventLoop::EventLoop(Default)
:
SocketMonitor
(
*
this
),
:
SocketMonitor
(
*
this
),
now_ms
(
::
MonotonicClockMS
()),
now_ms
(
::
MonotonicClockMS
()),
quit
(
false
),
quit
(
false
),
#ifdef USE_EPOLL
n_events
(
0
),
#endif
thread
(
ThreadId
::
Null
())
thread
(
ThreadId
::
Null
())
{
{
SocketMonitor
::
Open
(
wake_fd
.
Get
());
SocketMonitor
::
Open
(
wake_fd
.
Get
());
...
@@ -60,23 +57,18 @@ EventLoop::Break()
...
@@ -60,23 +57,18 @@ EventLoop::Break()
AddCall
([
this
]()
{
Break
();
});
AddCall
([
this
]()
{
Break
();
});
}
}
void
bool
EventLoop
::
Abandon
(
SocketMonitor
&
m
)
EventLoop
::
Abandon
(
int
_fd
,
SocketMonitor
&
m
)
{
{
#ifdef USE_EPOLL
poll_result
.
Clear
(
&
m
);
for
(
unsigned
i
=
0
,
n
=
n_events
;
i
<
n
;
++
i
)
return
poll_group
.
Abandon
(
_fd
);
if
(
events
[
i
].
data
.
ptr
==
&
m
)
events
[
i
].
events
=
0
;
#endif
}
}
bool
bool
EventLoop
::
RemoveFD
(
int
_fd
,
SocketMonitor
&
m
)
EventLoop
::
RemoveFD
(
int
_fd
,
SocketMonitor
&
m
)
{
{
#ifdef USE_EPOLL
poll_result
.
Clear
(
&
m
);
Abandon
(
m
);
return
poll_group
.
Remove
(
_fd
);
return
epoll
.
Remove
(
_fd
);
#endif
}
}
void
void
...
@@ -168,34 +160,29 @@ EventLoop::Run()
...
@@ -168,34 +160,29 @@ EventLoop::Run()
timeout */
timeout */
continue
;
continue
;
#ifdef USE_EPOLL
/* wait for new event */
/* wait for new event */
const
int
n
=
epoll
.
Wait
(
events
,
MAX_EVENTS
,
timeout_ms
);
poll_group
.
ReadEvents
(
poll_result
,
timeout_ms
);
n_events
=
std
::
max
(
n
,
0
);
now_ms
=
::
MonotonicClockMS
();
now_ms
=
::
MonotonicClockMS
();
assert
(
!
quit
);
assert
(
!
quit
);
/* invoke sockets */
/* invoke sockets */
for
(
int
i
=
0
;
i
<
poll_result
.
GetSize
();
++
i
)
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
auto
events
=
poll_result
.
GetEvents
(
i
);
const
auto
&
e
=
events
[
i
];
if
(
events
!=
0
)
{
auto
m
=
(
SocketMonitor
*
)
poll_result
.
GetObject
(
i
);
if
(
e
.
events
!=
0
)
{
m
->
Dispatch
(
events
);
SocketMonitor
&
m
=
*
(
SocketMonitor
*
)
e
.
data
.
ptr
;
m
.
Dispatch
(
e
.
events
);
if
(
quit
)
if
(
quit
)
break
;
break
;
}
}
}
}
n_events
=
0
;
poll_result
.
Reset
();
#endif
}
while
(
!
quit
);
}
while
(
!
quit
);
#endif
#endif
#ifdef USE_GLIB_EVENTLOOP
#ifdef USE_GLIB_EVENTLOOP
...
...
src/event/Loop.hxx
View file @
bb288f02
...
@@ -25,10 +25,7 @@
...
@@ -25,10 +25,7 @@
#include "Compiler.h"
#include "Compiler.h"
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_EPOLL
#include "PollGroup.hxx"
#include "system/EPollFD.hxx"
#endif
#include "thread/Mutex.hxx"
#include "thread/Mutex.hxx"
#include "WakeFD.hxx"
#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
#include "SocketMonitor.hxx"
...
@@ -99,13 +96,8 @@ class EventLoop final
...
@@ -99,13 +96,8 @@ class EventLoop final
bool
quit
;
bool
quit
;
#ifdef USE_EPOLL
PollGroup
poll_group
;
EPollFD
epoll
;
PollResult
poll_result
;
static
constexpr
unsigned
MAX_EVENTS
=
16
;
unsigned
n_events
;
epoll_event
events
[
MAX_EVENTS
];
#endif
#endif
#endif
#ifdef USE_GLIB_EVENTLOOP
#ifdef USE_GLIB_EVENTLOOP
...
@@ -140,15 +132,11 @@ public:
...
@@ -140,15 +132,11 @@ public:
void
Break
();
void
Break
();
bool
AddFD
(
int
_fd
,
unsigned
flags
,
SocketMonitor
&
m
)
{
bool
AddFD
(
int
_fd
,
unsigned
flags
,
SocketMonitor
&
m
)
{
#ifdef USE_EPOLL
return
poll_group
.
Add
(
_fd
,
flags
,
&
m
);
return
epoll
.
Add
(
_fd
,
flags
,
&
m
);
#endif
}
}
bool
ModifyFD
(
int
_fd
,
unsigned
flags
,
SocketMonitor
&
m
)
{
bool
ModifyFD
(
int
_fd
,
unsigned
flags
,
SocketMonitor
&
m
)
{
#ifdef USE_EPOLL
return
poll_group
.
Modify
(
_fd
,
flags
,
&
m
);
return
epoll
.
Modify
(
_fd
,
flags
,
&
m
);
#endif
}
}
/**
/**
...
@@ -156,7 +144,7 @@ public:
...
@@ -156,7 +144,7 @@ public:
* has been closed. This is like RemoveFD(), but does not
* has been closed. This is like RemoveFD(), but does not
* attempt to use #EPOLL_CTL_DEL.
* attempt to use #EPOLL_CTL_DEL.
*/
*/
void
Abandon
(
SocketMonitor
&
m
);
bool
Abandon
(
int
fd
,
SocketMonitor
&
m
);
bool
RemoveFD
(
int
fd
,
SocketMonitor
&
m
);
bool
RemoveFD
(
int
fd
,
SocketMonitor
&
m
);
...
...
src/event/PollGroup.hxx
0 → 100644
View file @
bb288f02
/*
* Copyright (C) 2003-2013 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_EVENT_POLLGROUP_HXX
#define MPD_EVENT_POLLGROUP_HXX
#ifdef USE_EPOLL
#include "PollGroupEPoll.hxx"
typedef
PollResultEPoll
PollResult
;
typedef
PollGroupEPoll
PollGroup
;
#endif
#endif
src/event/PollGroupEPoll.hxx
0 → 100644
View file @
bb288f02
/*
* Copyright (C) 2003-2013 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_EVENT_POLLGROUP_EPOLL_HXX
#define MPD_EVENT_POLLGROUP_EPOLL_HXX
#include "check.h"
#include "Compiler.h"
#include "system/EPollFD.hxx"
#include <array>
#include <algorithm>
class
PollResultEPoll
{
friend
class
PollGroupEPoll
;
std
::
array
<
epoll_event
,
16
>
events
;
int
n_events
;
public
:
PollResultEPoll
()
:
n_events
(
0
)
{
}
int
GetSize
()
const
{
return
n_events
;
}
unsigned
GetEvents
(
int
i
)
{
return
events
[
i
].
events
;
}
void
*
GetObject
(
int
i
)
{
return
events
[
i
].
data
.
ptr
;
}
void
Reset
()
{
n_events
=
0
;
}
void
Clear
(
void
*
obj
)
{
for
(
int
i
=
0
;
i
<
n_events
;
++
i
)
if
(
events
[
i
].
data
.
ptr
==
obj
)
events
[
i
].
events
=
0
;
}
};
class
PollGroupEPoll
{
EPollFD
epoll
;
PollGroupEPoll
(
PollGroupEPoll
&
)
=
delete
;
PollGroupEPoll
&
operator
=
(
PollGroupEPoll
&
)
=
delete
;
public
:
static
constexpr
unsigned
READ
=
EPOLLIN
;
static
constexpr
unsigned
WRITE
=
EPOLLOUT
;
static
constexpr
unsigned
ERROR
=
EPOLLERR
;
static
constexpr
unsigned
HANGUP
=
EPOLLHUP
;
PollGroupEPoll
()
=
default
;
void
ReadEvents
(
PollResultEPoll
&
result
,
int
timeout_ms
)
{
int
ret
=
epoll
.
Wait
(
result
.
events
.
data
(),
result
.
events
.
size
(),
timeout_ms
);
result
.
n_events
=
std
::
max
(
0
,
ret
);
}
bool
Add
(
int
fd
,
unsigned
events
,
void
*
obj
)
{
return
epoll
.
Add
(
fd
,
events
,
obj
);
}
bool
Modify
(
int
fd
,
unsigned
events
,
void
*
obj
)
{
return
epoll
.
Modify
(
fd
,
events
,
obj
);
}
bool
Remove
(
int
fd
)
{
return
epoll
.
Remove
(
fd
);
}
bool
Abandon
(
gcc_unused
int
fd
)
{
// Nothing to do in this implementation.
// Closed descriptors are automatically unregistered.
return
true
;
}
};
#endif
src/event/SocketMonitor.cxx
View file @
bb288f02
...
@@ -158,8 +158,9 @@ SocketMonitor::Abandon()
...
@@ -158,8 +158,9 @@ SocketMonitor::Abandon()
assert
(
IsDefined
());
assert
(
IsDefined
());
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_INTERNAL_EVENTLOOP
int
old_fd
=
fd
;
fd
=
-
1
;
fd
=
-
1
;
loop
.
Abandon
(
*
this
);
loop
.
Abandon
(
old_fd
,
*
this
);
#else
#else
Steal
();
Steal
();
#endif
#endif
...
...
src/event/SocketMonitor.hxx
View file @
bb288f02
...
@@ -23,9 +23,7 @@
...
@@ -23,9 +23,7 @@
#include "check.h"
#include "check.h"
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_EPOLL
#include "PollGroup.hxx"
#include <sys/epoll.h>
#endif
#endif
#endif
#ifdef USE_GLIB_EVENTLOOP
#ifdef USE_GLIB_EVENTLOOP
...
@@ -79,12 +77,10 @@ class SocketMonitor {
...
@@ -79,12 +77,10 @@ class SocketMonitor {
public
:
public
:
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_INTERNAL_EVENTLOOP
#ifdef USE_EPOLL
static
constexpr
unsigned
READ
=
PollGroup
::
READ
;
static
constexpr
unsigned
READ
=
EPOLLIN
;
static
constexpr
unsigned
WRITE
=
PollGroup
::
WRITE
;
static
constexpr
unsigned
WRITE
=
EPOLLOUT
;
static
constexpr
unsigned
ERROR
=
PollGroup
::
ERROR
;
static
constexpr
unsigned
ERROR
=
EPOLLERR
;
static
constexpr
unsigned
HANGUP
=
PollGroup
::
HANGUP
;
static
constexpr
unsigned
HANGUP
=
EPOLLHUP
;
#endif
#endif
#endif
#ifdef USE_GLIB_EVENTLOOP
#ifdef USE_GLIB_EVENTLOOP
...
...
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