1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/*
* Copyright © 2008-2010 dragchan <zgchan317@gmail.com>
* This file is part of FbTerm.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <unistd.h>
#include <fcntl.h>
#include "config.h"
#include "fbio.h"
#define NR_FDS 32
#ifdef HAVE_EPOLL
#include <sys/epoll.h>
#define NR_EPOLL_FDS 10
s32 epollFd;
#else
#include <sys/select.h>
static fd_set fds;
static u32 maxfd = 0;
#endif
static IoPipe *ioPipeMap[NR_FDS];
IoDispatcher *IoDispatcher::createInstance()
{
return new FbIoDispatcher();
}
FbIoDispatcher::FbIoDispatcher()
{
#ifdef HAVE_EPOLL
epollFd = epoll_create(NR_EPOLL_FDS);
fcntl(epollFd, F_SETFD, fcntl(epollFd, F_GETFD) | FD_CLOEXEC);
#else
FD_ZERO(&fds);
#endif
}
FbIoDispatcher::~FbIoDispatcher()
{
for (u32 i = NR_FDS; i--;) {
if (ioPipeMap[i]) delete ioPipeMap[i];
}
#ifdef HAVE_EPOLL
close(epollFd);
#endif
}
void FbIoDispatcher::addIoSource(IoPipe *src, bool isread)
{
if (src->fd() >= NR_FDS) return;
ioPipeMap[src->fd()] = src;
#ifdef HAVE_EPOLL
epoll_event ev;
ev.data.fd = src->fd();
ev.events = (isread ? EPOLLIN : EPOLLOUT);
epoll_ctl(epollFd, EPOLL_CTL_ADD, src->fd(), &ev);
#else
FD_SET(src->fd(), &fds);
if (src->fd() > maxfd) maxfd = src->fd();
#endif
}
void FbIoDispatcher::removeIoSource(IoPipe *src, bool isread)
{
if (src->fd() >= NR_FDS) return;
ioPipeMap[src->fd()] = 0;
#ifdef HAVE_EPOLL
epoll_event ev;
ev.data.fd = src->fd();
ev.events = (isread ? EPOLLIN : EPOLLOUT);
epoll_ctl(epollFd, EPOLL_CTL_DEL, src->fd(), &ev);
#else
FD_CLR(src->fd(), &fds);
#endif
}
void FbIoDispatcher::poll()
{
#ifdef HAVE_EPOLL
epoll_event evs[NR_EPOLL_FDS];
s32 nfds = epoll_wait(epollFd, evs, NR_EPOLL_FDS, -1);
for (s32 i = 0; i < nfds; i++) {
IoPipe *src = ioPipeMap[evs[i].data.fd];
if (!src) continue;
if (evs[i].events & EPOLLIN) {
src->ready(true);
}
if (evs[i].events & EPOLLOUT) {
src->ready(false);
}
if (evs[i].events & EPOLLHUP) {
delete src;
}
}
#else
fd_set rfds = fds;
s32 num = select(maxfd + 1, &rfds, 0, 0, 0);
if (num <= 0) return;
for (u32 i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &rfds)) {
if (ioPipeMap[i]) {
ioPipeMap[i]->ready(true);
}
if (!--num) break;
}
}
#endif
}
|