]>
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 #ifdef HAVE_SYS_EPOLL_H
24 #include "wx/unix/private/epolldispatcher.h"
25 #include "wx/unix/private.h"
26 #include "wx/module.h"
33 #include <sys/epoll.h>
36 #define wxEpollDispatcher_Trace wxT("epolldispatcher")
38 static wxEpollDispatcher
*gs_epollDispatcher
= NULL
;
40 // ============================================================================
42 // ============================================================================
44 // helper: return EPOLLxxx mask corresponding to the given flags (and also log
45 // debugging messages about it)
46 static uint32_t GetEpollMask(int flags
, int fd
)
50 if ( flags
& wxFDIO_INPUT
)
53 wxLogTrace(wxEpollDispatcher_Trace
,
54 _T("Registered fd %d for input events"), fd
);
57 if ( flags
& wxFDIO_OUTPUT
)
60 wxLogTrace(wxEpollDispatcher_Trace
,
61 _T("Registered fd %d for output events"), fd
);
64 if ( flags
& wxFDIO_EXCEPTION
)
66 ep
|= EPOLLERR
| EPOLLHUP
;
67 wxLogTrace(wxEpollDispatcher_Trace
,
68 _T("Registered fd %d for exceptional events"), fd
);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 wxEpollDispatcher::wxEpollDispatcher()
80 m_epollDescriptor
= epoll_create(1024);
81 if ( m_epollDescriptor
== -1 )
83 wxLogSysError(_("Failed to create epoll descriptor"));
87 bool wxEpollDispatcher::RegisterFD(int fd
, wxFDIOHandler
* handler
, int flags
)
89 if ( !wxFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
93 ev
.events
= GetEpollMask(flags
, fd
);
94 ev
.data
.ptr
= handler
;
96 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_ADD
, fd
, &ev
);
99 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
100 fd
, m_epollDescriptor
);
108 bool wxEpollDispatcher::ModifyFD(int fd
, wxFDIOHandler
* handler
, int flags
)
110 if ( !wxFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
114 ev
.events
= GetEpollMask(flags
, fd
);
115 ev
.data
.ptr
= handler
;
117 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_MOD
, fd
, &ev
);
120 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
121 fd
, m_epollDescriptor
);
129 wxFDIOHandler
*wxEpollDispatcher::UnregisterFD(int fd
, int flags
)
131 wxFDIOHandler
* const handler
= wxFDIODispatcher::UnregisterFD(fd
, flags
);
139 if ( epoll_ctl(m_epollDescriptor
, EPOLL_CTL_DEL
, fd
, &ev
) != 0 )
141 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
142 fd
, m_epollDescriptor
);
148 void wxEpollDispatcher::RunLoop(int timeout
)
150 epoll_event events
[16];
152 const int e_num
= epoll_wait
157 timeout
== TIMEOUT_INFINITE
? -1 : timeout
162 if ( errno
!= EINTR
)
164 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
170 for ( epoll_event
*p
= events
; p
< events
+ e_num
; p
++ )
172 wxFDIOHandler
* const handler
= (wxFDIOHandler
*)(p
->data
.ptr
);
175 wxFAIL_MSG( _T("NULL handler in epoll_event?") );
179 if ( p
->events
& EPOLLIN
)
180 handler
->OnReadWaiting();
182 if ( p
->events
& EPOLLOUT
)
183 handler
->OnWriteWaiting();
185 if ( p
->events
& (EPOLLERR
| EPOLLHUP
) )
186 handler
->OnExceptionWaiting();
191 wxEpollDispatcher
*wxEpollDispatcher::Get()
193 if ( !gs_epollDispatcher
)
195 gs_epollDispatcher
= new wxEpollDispatcher
;
196 if ( !gs_epollDispatcher
->IsOk() )
198 delete gs_epollDispatcher
;
199 gs_epollDispatcher
= NULL
;
203 return gs_epollDispatcher
;
206 // ----------------------------------------------------------------------------
207 // wxEpollDispatcherModule
208 // ----------------------------------------------------------------------------
210 class wxEpollDispatcherModule
: public wxModule
213 wxEpollDispatcherModule() { }
215 virtual bool OnInit() { return true; }
216 virtual void OnExit() { wxDELETE(gs_epollDispatcher
); }
218 DECLARE_DYNAMIC_CLASS(wxEpollDispatcherModule
)
221 IMPLEMENT_DYNAMIC_CLASS(wxEpollDispatcherModule
, wxModule
)
223 #endif // HAVE_SYS_EPOLL_H