]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/wakeuppipe.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/wakeuppipe.cpp
3 // Purpose: Implementation of wxWakeUpPipe class.
4 // Author: Vadim Zeitlin
5 // Created: 2013-06-09 (extracted from src/unix/evtloopunix.cpp)
7 // Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/unix/private/wakeuppipe.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 #define TRACE_EVENTS wxT("events")
39 // ============================================================================
40 // wxWakeUpPipe implementation
41 // ============================================================================
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxWakeUpPipe::wxWakeUpPipe()
51 if ( !m_pipe
.Create() )
53 wxLogError(_("Failed to create wake up pipe used by event loop."));
58 if ( !m_pipe
.MakeNonBlocking(wxPipe::Read
) )
60 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
64 wxLogTrace(TRACE_EVENTS
, wxT("Wake up pipe (%d, %d) created"),
65 m_pipe
[wxPipe::Read
], m_pipe
[wxPipe::Write
]);
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 void wxWakeUpPipe::WakeUp()
74 wxCriticalSectionLocker
lock(m_pipeLock
);
76 // No need to do anything if the pipe already contains something.
80 if ( write(m_pipe
[wxPipe::Write
], "s", 1) != 1 )
82 // don't use wxLog here, we can be in another thread and this could
83 // result in dead locks
84 perror("write(wake up pipe)");
88 // We just wrote to it, so it's not empty any more.
89 m_pipeIsEmpty
= false;
93 void wxWakeUpPipe::OnReadWaiting()
95 // got wakeup from child thread, remove the data that provoked it from the
98 wxCriticalSectionLocker
lock(m_pipeLock
);
103 const int size
= read(GetReadFd(), buf
, WXSIZEOF(buf
));
107 wxASSERT_MSG( size
== 1, "Too many writes to wake-up pipe?" );
112 if ( size
== 0 || (size
== -1 && errno
== EAGAIN
) )
114 // No data available, not an error (but still surprising,
119 if ( errno
== EINTR
)
121 // We were interrupted, try again.
125 wxLogSysError(_("Failed to read from wake-up pipe"));
130 // The pipe is empty now, so future calls to WakeUp() would need to write
132 m_pipeIsEmpty
= true;
134 // writing to the wake up pipe will make wxConsoleEventLoop return from
135 // wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing
136 // else needs to be done