]> git.saurik.com Git - wxWidgets.git/blame - src/unix/epolldispatcher.cpp
Fix wxHtmlHelpData::SetTempDir() to behave correctly without trailing slash.
[wxWidgets.git] / src / unix / epolldispatcher.cpp
CommitLineData
b46b1d59 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/unix/epolldispatcher.cpp
b46b1d59
VZ
3// Purpose: implements dispatcher for epoll_wait() call
4// Author: Lukasz Michalski
5// Created: April 2007
b46b1d59 6// Copyright: (c) 2007 Lukasz Michalski
526954c5 7// Licence: wxWindows licence
b46b1d59
VZ
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18// for compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
9550ee98 21#if wxUSE_EPOLL_DISPATCHER
b46b1d59
VZ
22
23#include "wx/unix/private/epolldispatcher.h"
24#include "wx/unix/private.h"
e169ac81 25#include "wx/stopwatch.h"
b46b1d59 26
b5ef33b2
VS
27#ifndef WX_PRECOMP
28 #include "wx/log.h"
29 #include "wx/intl.h"
30#endif
31
b46b1d59
VZ
32#include <sys/epoll.h>
33#include <errno.h>
98883bca 34#include <unistd.h>
b46b1d59
VZ
35
36#define wxEpollDispatcher_Trace wxT("epolldispatcher")
37
b46b1d59
VZ
38// ============================================================================
39// implementation
40// ============================================================================
41
42// helper: return EPOLLxxx mask corresponding to the given flags (and also log
43// debugging messages about it)
3f65cc75 44static uint32_t GetEpollMask(int flags, int fd)
b46b1d59 45{
3f65cc75
VZ
46 wxUnusedVar(fd); // unused if wxLogTrace() disabled
47
b46b1d59
VZ
48 uint32_t ep = 0;
49
50 if ( flags & wxFDIO_INPUT )
51 {
52 ep |= EPOLLIN;
53 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 54 wxT("Registered fd %d for input events"), fd);
b46b1d59
VZ
55 }
56
57 if ( flags & wxFDIO_OUTPUT )
58 {
59 ep |= EPOLLOUT;
60 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 61 wxT("Registered fd %d for output events"), fd);
b46b1d59
VZ
62 }
63
64 if ( flags & wxFDIO_EXCEPTION )
65 {
66 ep |= EPOLLERR | EPOLLHUP;
67 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 68 wxT("Registered fd %d for exceptional events"), fd);
b46b1d59
VZ
69 }
70
71 return ep;
72}
73
74// ----------------------------------------------------------------------------
75// wxEpollDispatcher
76// ----------------------------------------------------------------------------
77
5e1eac14
VZ
78/* static */
79wxEpollDispatcher *wxEpollDispatcher::Create()
b46b1d59 80{
5e1eac14
VZ
81 int epollDescriptor = epoll_create(1024);
82 if ( epollDescriptor == -1 )
b46b1d59
VZ
83 {
84 wxLogSysError(_("Failed to create epoll descriptor"));
5e1eac14 85 return NULL;
b46b1d59 86 }
2804f77d 87 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 88 wxT("Epoll fd %d created"), epollDescriptor);
5e1eac14
VZ
89 return new wxEpollDispatcher(epollDescriptor);
90}
91
92wxEpollDispatcher::wxEpollDispatcher(int epollDescriptor)
93{
9a83f860 94 wxASSERT_MSG( epollDescriptor != -1, wxT("invalid descriptor") );
5e1eac14
VZ
95
96 m_epollDescriptor = epollDescriptor;
b46b1d59
VZ
97}
98
d31a4a84
VZ
99wxEpollDispatcher::~wxEpollDispatcher()
100{
101 if ( close(m_epollDescriptor) != 0 )
102 {
103 wxLogSysError(_("Error closing epoll descriptor"));
104 }
105}
106
b46b1d59
VZ
107bool wxEpollDispatcher::RegisterFD(int fd, wxFDIOHandler* handler, int flags)
108{
b46b1d59
VZ
109 epoll_event ev;
110 ev.events = GetEpollMask(flags, fd);
111 ev.data.ptr = handler;
112
113 const int ret = epoll_ctl(m_epollDescriptor, EPOLL_CTL_ADD, fd, &ev);
114 if ( ret != 0 )
115 {
116 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
117 fd, m_epollDescriptor);
118
119 return false;
120 }
2804f77d 121 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 122 wxT("Added fd %d (handler %p) to epoll %d"), fd, handler, m_epollDescriptor);
b46b1d59
VZ
123
124 return true;
125}
126
127bool wxEpollDispatcher::ModifyFD(int fd, wxFDIOHandler* handler, int flags)
128{
b46b1d59
VZ
129 epoll_event ev;
130 ev.events = GetEpollMask(flags, fd);
131 ev.data.ptr = handler;
132
133 const int ret = epoll_ctl(m_epollDescriptor, EPOLL_CTL_MOD, fd, &ev);
134 if ( ret != 0 )
135 {
136 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
137 fd, m_epollDescriptor);
138
139 return false;
140 }
141
2804f77d 142 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 143 wxT("Modified fd %d (handler: %p) on epoll %d"), fd, handler, m_epollDescriptor);
b46b1d59
VZ
144 return true;
145}
146
af57c51a 147bool wxEpollDispatcher::UnregisterFD(int fd)
b46b1d59 148{
b46b1d59
VZ
149 epoll_event ev;
150 ev.events = 0;
151 ev.data.ptr = NULL;
152
153 if ( epoll_ctl(m_epollDescriptor, EPOLL_CTL_DEL, fd, &ev) != 0 )
154 {
155 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
156 fd, m_epollDescriptor);
157 }
2804f77d 158 wxLogTrace(wxEpollDispatcher_Trace,
9a83f860 159 wxT("removed fd %d from %d"), fd, m_epollDescriptor);
ad8d42f8 160 return true;
b46b1d59
VZ
161}
162
a12698ab
VZ
163int
164wxEpollDispatcher::DoPoll(epoll_event *events, int numEvents, int timeout) const
b46b1d59 165{
e169ac81
VZ
166 // the code below relies on TIMEOUT_INFINITE being -1 so that we can pass
167 // timeout value directly to epoll_wait() which interprets -1 as meaning to
168 // wait forever and would need to be changed if the value of
169 // TIMEOUT_INFINITE ever changes
170 wxCOMPILE_TIME_ASSERT( TIMEOUT_INFINITE == -1, UpdateThisCode );
b46b1d59 171
e169ac81 172 wxMilliClock_t timeEnd;
a12698ab 173 if ( timeout > 0 )
e169ac81
VZ
174 timeEnd = wxGetLocalTimeMillis();
175
176 int rc;
177 for ( ;; )
b46b1d59 178 {
a12698ab 179 rc = epoll_wait(m_epollDescriptor, events, numEvents, timeout);
e169ac81
VZ
180 if ( rc != -1 || errno != EINTR )
181 break;
182
183 // we got interrupted, update the timeout and restart
a12698ab
VZ
184 if ( timeout > 0 )
185 {
186 timeout = wxMilliClockToLong(timeEnd - wxGetLocalTimeMillis());
187 if ( timeout < 0 )
188 return 0;
189 }
e169ac81
VZ
190 }
191
a12698ab
VZ
192 return rc;
193}
194
195bool wxEpollDispatcher::HasPending() const
196{
197 epoll_event event;
a7132f4b
VZ
198
199 // NB: it's not really clear if epoll_wait() can return a number greater
200 // than the number of events passed to it but just in case it can, use
201 // >= instead of == here, see #10397
fa00c4e3 202 return DoPoll(&event, 1, 0) >= 1;
a12698ab
VZ
203}
204
205int wxEpollDispatcher::Dispatch(int timeout)
206{
207 epoll_event events[16];
208
209 const int rc = DoPoll(events, WXSIZEOF(events), timeout);
210
e169ac81
VZ
211 if ( rc == -1 )
212 {
213 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
214 m_epollDescriptor);
a12698ab 215 return -1;
b46b1d59
VZ
216 }
217
a12698ab 218 int numEvents = 0;
e169ac81 219 for ( epoll_event *p = events; p < events + rc; p++ )
b46b1d59
VZ
220 {
221 wxFDIOHandler * const handler = (wxFDIOHandler *)(p->data.ptr);
222 if ( !handler )
223 {
9a83f860 224 wxFAIL_MSG( wxT("NULL handler in epoll_event?") );
b46b1d59
VZ
225 continue;
226 }
227
1a781247
VZ
228 // note that for compatibility with wxSelectDispatcher we call
229 // OnReadWaiting() on EPOLLHUP as this is what epoll_wait() returns
230 // when the write end of a pipe is closed while with select() the
231 // remaining pipe end becomes ready for reading when this happens
232 if ( p->events & (EPOLLIN | EPOLLHUP) )
b46b1d59 233 handler->OnReadWaiting();
7523de90 234 else if ( p->events & EPOLLOUT )
b46b1d59 235 handler->OnWriteWaiting();
1a781247 236 else if ( p->events & EPOLLERR )
b46b1d59 237 handler->OnExceptionWaiting();
5a557d1e
VZ
238 else
239 continue;
240
a12698ab 241 numEvents++;
b46b1d59 242 }
5a557d1e 243
a12698ab 244 return numEvents;
b46b1d59
VZ
245}
246
a1873279 247#endif // wxUSE_EPOLL_DISPATCHER