2013-07-02 20:27:28 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/unix/private/wakeuppipe.h
|
|
|
|
// Purpose: Helper class allowing to wake up the main thread.
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2013-06-09 (extracted from src/unix/evtloopunix.cpp)
|
|
|
|
// Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
|
|
|
|
#define _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
|
|
|
|
|
|
|
|
#include "wx/unix/pipe.h"
|
2013-07-02 20:30:39 -04:00
|
|
|
#include "wx/evtloopsrc.h"
|
2013-07-02 20:27:28 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxWakeUpPipe: allows to wake up the event loop by writing to it
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2013-07-02 20:27:53 -04:00
|
|
|
// This class is not MT-safe, see wxWakeUpPipeMT below for a wake up pipe
|
|
|
|
// usable from other threads.
|
|
|
|
|
2013-07-02 20:30:39 -04:00
|
|
|
class wxWakeUpPipe : public wxEventLoopSourceHandler
|
2013-07-02 20:27:28 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Create and initialize the pipe.
|
|
|
|
//
|
|
|
|
// It's the callers responsibility to add the read end of this pipe,
|
|
|
|
// returned by GetReadFd(), to the code blocking on input.
|
|
|
|
wxWakeUpPipe();
|
|
|
|
|
|
|
|
// Wake up the blocking operation involving this pipe.
|
|
|
|
//
|
|
|
|
// It simply writes to the write end of the pipe.
|
|
|
|
//
|
2013-07-02 20:27:53 -04:00
|
|
|
// As indicated by its name, this method does no locking and so can be
|
|
|
|
// called only from the main thread.
|
|
|
|
void WakeUpNoLock();
|
|
|
|
|
|
|
|
// Same as WakeUp() but without locking.
|
2013-07-02 20:27:28 -04:00
|
|
|
|
|
|
|
// Return the read end of the pipe.
|
|
|
|
int GetReadFd() { return m_pipe[wxPipe::Read]; }
|
|
|
|
|
|
|
|
|
2013-07-02 20:30:39 -04:00
|
|
|
// Implement wxEventLoopSourceHandler pure virtual methods
|
2013-07-02 20:27:28 -04:00
|
|
|
virtual void OnReadWaiting();
|
|
|
|
virtual void OnWriteWaiting() { }
|
|
|
|
virtual void OnExceptionWaiting() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxPipe m_pipe;
|
|
|
|
|
|
|
|
// This flag is set to true after writing to the pipe and reset to false
|
|
|
|
// after reading from it in the main thread. Having it allows us to avoid
|
|
|
|
// overflowing the pipe with too many writes if the main thread can't keep
|
|
|
|
// up with reading from it.
|
|
|
|
bool m_pipeIsEmpty;
|
|
|
|
};
|
|
|
|
|
2013-07-02 20:27:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxWakeUpPipeMT: thread-safe version of wxWakeUpPipe
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// This class can be used from multiple threads, i.e. its WakeUp() can be
|
|
|
|
// called concurrently.
|
2013-07-14 07:34:27 -04:00
|
|
|
#if wxUSE_THREADS
|
2013-07-02 20:27:53 -04:00
|
|
|
|
|
|
|
class wxWakeUpPipeMT : public wxWakeUpPipe
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxWakeUpPipeMT() { }
|
|
|
|
|
|
|
|
// Thread-safe wrapper around WakeUpNoLock(): can be called from another
|
|
|
|
// thread to wake up the main one.
|
|
|
|
void WakeUp()
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_pipeLock);
|
|
|
|
|
|
|
|
WakeUpNoLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnReadWaiting()
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_pipeLock);
|
|
|
|
|
|
|
|
wxWakeUpPipe::OnReadWaiting();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Protects access to m_pipeIsEmpty.
|
|
|
|
wxCriticalSection m_pipeLock;
|
|
|
|
};
|
|
|
|
|
2013-07-14 07:34:27 -04:00
|
|
|
#else // !wxUSE_THREADS
|
|
|
|
|
|
|
|
typedef wxWakeUpPipe wxWakeUpPipeMT;
|
|
|
|
|
|
|
|
#endif // wxUSE_THREADS/!wxUSE_THREADS
|
|
|
|
|
2013-07-02 20:27:28 -04:00
|
|
|
#endif // _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
|