]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/epolldispatcher.cpp
8df776cba17e6ed1030414924947d495983554ab
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 #ifdef wxUSE_EPOLL_DISPATCHER
24 #include "wx/unix/private/epolldispatcher.h"
25 #include "wx/unix/private.h"
32 #include <sys/epoll.h>
35 #define wxEpollDispatcher_Trace wxT("epolldispatcher")
37 // ============================================================================
39 // ============================================================================
41 // helper: return EPOLLxxx mask corresponding to the given flags (and also log
42 // debugging messages about it)
43 static uint32_t GetEpollMask(int flags
, int fd
)
47 if ( flags
& wxFDIO_INPUT
)
50 wxLogTrace(wxEpollDispatcher_Trace
,
51 _T("Registered fd %d for input events"), fd
);
54 if ( flags
& wxFDIO_OUTPUT
)
57 wxLogTrace(wxEpollDispatcher_Trace
,
58 _T("Registered fd %d for output events"), fd
);
61 if ( flags
& wxFDIO_EXCEPTION
)
63 ep
|= EPOLLERR
| EPOLLHUP
;
64 wxLogTrace(wxEpollDispatcher_Trace
,
65 _T("Registered fd %d for exceptional events"), fd
);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
76 wxEpollDispatcher
*wxEpollDispatcher::Create()
78 int epollDescriptor
= epoll_create(1024);
79 if ( epollDescriptor
== -1 )
81 wxLogSysError(_("Failed to create epoll descriptor"));
85 return new wxEpollDispatcher(epollDescriptor
);
88 wxEpollDispatcher::wxEpollDispatcher(int epollDescriptor
)
90 wxASSERT_MSG( epollDescriptor
!= -1, _T("invalid descriptor") );
92 m_epollDescriptor
= epollDescriptor
;
95 wxEpollDispatcher::~wxEpollDispatcher()
97 if ( close(m_epollDescriptor
) != 0 )
99 wxLogSysError(_("Error closing epoll descriptor"));
103 bool wxEpollDispatcher::RegisterFD(int fd
, wxFDIOHandler
* handler
, int flags
)
106 ev
.events
= GetEpollMask(flags
, fd
);
107 ev
.data
.ptr
= handler
;
109 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_ADD
, fd
, &ev
);
112 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
113 fd
, m_epollDescriptor
);
121 bool wxEpollDispatcher::ModifyFD(int fd
, wxFDIOHandler
* handler
, int flags
)
124 ev
.events
= GetEpollMask(flags
, fd
);
125 ev
.data
.ptr
= handler
;
127 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_MOD
, fd
, &ev
);
130 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
131 fd
, m_epollDescriptor
);
139 bool wxEpollDispatcher::UnregisterFD(int fd
)
145 if ( epoll_ctl(m_epollDescriptor
, EPOLL_CTL_DEL
, fd
, &ev
) != 0 )
147 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
148 fd
, m_epollDescriptor
);
154 void wxEpollDispatcher::Dispatch(int timeout
)
156 epoll_event events
[16];
158 const int e_num
= epoll_wait
163 timeout
== TIMEOUT_INFINITE
? -1 : timeout
168 if ( errno
!= EINTR
)
170 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
176 for ( epoll_event
*p
= events
; p
< events
+ e_num
; p
++ )
178 wxFDIOHandler
* const handler
= (wxFDIOHandler
*)(p
->data
.ptr
);
181 wxFAIL_MSG( _T("NULL handler in epoll_event?") );
185 if ( p
->events
& EPOLLIN
)
186 handler
->OnReadWaiting();
187 else if ( p
->events
& EPOLLOUT
)
188 handler
->OnWriteWaiting();
189 else if ( p
->events
& (EPOLLERR
| EPOLLHUP
) )
190 handler
->OnExceptionWaiting();
194 #endif // wxUSE_EPOLL_DISPATCHER