]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/epolldispatcher.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/epolldispatcher.cpp
3 // Purpose: implements dispatcher for epoll_wait() call
4 // Author: Lukasz Michalski
7 // Copyright: (c) 2007 Lukasz Michalski
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if wxUSE_EPOLL_DISPATCHER
24 #include "wx/unix/private/epolldispatcher.h"
25 #include "wx/unix/private.h"
32 #include <sys/epoll.h>
36 #define wxEpollDispatcher_Trace wxT("epolldispatcher")
38 // ============================================================================
40 // ============================================================================
42 // helper: return EPOLLxxx mask corresponding to the given flags (and also log
43 // debugging messages about it)
44 static uint32_t GetEpollMask(int flags
, int WXUNUSED_UNLESS_DEBUG(fd
))
48 if ( flags
& wxFDIO_INPUT
)
51 wxLogTrace(wxEpollDispatcher_Trace
,
52 _T("Registered fd %d for input events"), fd
);
55 if ( flags
& wxFDIO_OUTPUT
)
58 wxLogTrace(wxEpollDispatcher_Trace
,
59 _T("Registered fd %d for output events"), fd
);
62 if ( flags
& wxFDIO_EXCEPTION
)
64 ep
|= EPOLLERR
| EPOLLHUP
;
65 wxLogTrace(wxEpollDispatcher_Trace
,
66 _T("Registered fd %d for exceptional events"), fd
);
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
77 wxEpollDispatcher
*wxEpollDispatcher::Create()
79 int epollDescriptor
= epoll_create(1024);
80 if ( epollDescriptor
== -1 )
82 wxLogSysError(_("Failed to create epoll descriptor"));
86 return new wxEpollDispatcher(epollDescriptor
);
89 wxEpollDispatcher::wxEpollDispatcher(int epollDescriptor
)
91 wxASSERT_MSG( epollDescriptor
!= -1, _T("invalid descriptor") );
93 m_epollDescriptor
= epollDescriptor
;
96 wxEpollDispatcher::~wxEpollDispatcher()
98 if ( close(m_epollDescriptor
) != 0 )
100 wxLogSysError(_("Error closing epoll descriptor"));
104 bool wxEpollDispatcher::RegisterFD(int fd
, wxFDIOHandler
* handler
, int flags
)
107 ev
.events
= GetEpollMask(flags
, fd
);
108 ev
.data
.ptr
= handler
;
110 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_ADD
, fd
, &ev
);
113 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
114 fd
, m_epollDescriptor
);
122 bool wxEpollDispatcher::ModifyFD(int fd
, wxFDIOHandler
* handler
, int flags
)
125 ev
.events
= GetEpollMask(flags
, fd
);
126 ev
.data
.ptr
= handler
;
128 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_MOD
, fd
, &ev
);
131 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
132 fd
, m_epollDescriptor
);
140 bool wxEpollDispatcher::UnregisterFD(int fd
)
146 if ( epoll_ctl(m_epollDescriptor
, EPOLL_CTL_DEL
, fd
, &ev
) != 0 )
148 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
149 fd
, m_epollDescriptor
);
155 void wxEpollDispatcher::Dispatch(int timeout
)
157 epoll_event events
[16];
159 const int e_num
= epoll_wait
164 timeout
== TIMEOUT_INFINITE
? -1 : timeout
169 if ( errno
!= EINTR
)
171 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
177 for ( epoll_event
*p
= events
; p
< events
+ e_num
; p
++ )
179 wxFDIOHandler
* const handler
= (wxFDIOHandler
*)(p
->data
.ptr
);
182 wxFAIL_MSG( _T("NULL handler in epoll_event?") );
186 if ( p
->events
& EPOLLIN
)
187 handler
->OnReadWaiting();
188 else if ( p
->events
& EPOLLOUT
)
189 handler
->OnWriteWaiting();
190 else if ( p
->events
& (EPOLLERR
| EPOLLHUP
) )
191 handler
->OnExceptionWaiting();
195 #endif // wxUSE_EPOLL_DISPATCHER