2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2010-10-30 19:51:18 -04:00
|
|
|
// Name: wx/msgqueue.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxMessageQueue<T>
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-10-30 19:51:18 -04:00
|
|
|
/**
|
|
|
|
Error codes for wxMessageQueue<> operations.
|
|
|
|
|
|
|
|
This enum contains the possible return value of wxMessageQueue<> methods.
|
|
|
|
|
|
|
|
@since 2.9.0
|
|
|
|
@category{threading}
|
|
|
|
*/
|
|
|
|
enum wxMessageQueueError
|
|
|
|
{
|
|
|
|
/// Indicates that the operation completed successfully.
|
|
|
|
wxMSGQUEUE_NO_ERROR = 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
Indicates that no messages were received before timeout expired.
|
|
|
|
|
|
|
|
This return value is only used by wxMessageQueue<>::ReceiveTimeout().
|
|
|
|
*/
|
|
|
|
wxMSGQUEUE_TIMEOUT,
|
|
|
|
|
|
|
|
/// Some unexpected (and fatal) error has occurred.
|
|
|
|
wxMSGQUEUE_MISC_ERROR
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
wxMessageQueue allows passing messages between threads.
|
|
|
|
|
|
|
|
This class should be typically used to communicate between the main and worker
|
2008-10-11 09:10:48 -04:00
|
|
|
threads. The main thread calls wxMessageQueue::Post and the worker thread
|
|
|
|
calls wxMessageQueue::Receive.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2009-02-20 06:34:52 -05:00
|
|
|
@tparam T
|
|
|
|
For this class a message is an object of arbitrary type T.
|
|
|
|
|
2008-10-11 09:10:48 -04:00
|
|
|
Notice that often there is a some special message indicating that the thread
|
2008-03-08 09:43:31 -05:00
|
|
|
should terminate as there is no other way to gracefully shutdown a thread
|
|
|
|
waiting on the message queue.
|
|
|
|
|
2010-05-22 12:14:16 -04:00
|
|
|
@since 2.9.0
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@nolibrary
|
2009-02-20 06:34:52 -05:00
|
|
|
@category{threading}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxThread
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2009-02-20 06:34:52 -05:00
|
|
|
template <typename T>
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxMessageQueue<T>
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-10-11 09:10:48 -04:00
|
|
|
/**
|
|
|
|
Default and only constructor.
|
|
|
|
Use wxMessageQueue::IsOk to check if the object was successfully initialized.
|
|
|
|
*/
|
|
|
|
wxMessageQueue();
|
|
|
|
|
2010-05-22 12:14:16 -04:00
|
|
|
/**
|
|
|
|
Remove all messages from the queue.
|
|
|
|
|
|
|
|
This method is meant to be called from the same thread(s) that call
|
|
|
|
Post() to discard any still pending requests if they became
|
|
|
|
unnecessary.
|
|
|
|
|
|
|
|
@since 2.9.1
|
|
|
|
*/
|
|
|
|
wxMessageQueueError Clear();
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Returns @true if the object had been initialized successfully, @false
|
2008-03-08 08:52:38 -05:00
|
|
|
if an error occurred.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOk() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Add a message to this queue and signal the threads waiting for messages
|
|
|
|
(i.e. the threads which called wxMessageQueue::Receive or
|
|
|
|
wxMessageQueue::ReceiveTimeout).
|
2008-10-11 09:10:48 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
This method is safe to call from multiple threads in parallel.
|
|
|
|
*/
|
|
|
|
wxMessageQueueError Post(T const& msg);
|
|
|
|
|
|
|
|
/**
|
2008-10-11 09:10:48 -04:00
|
|
|
Block until a message becomes available in the queue.
|
|
|
|
Waits indefinitely long or until an error occurs.
|
|
|
|
|
|
|
|
The message is returned in @a msg.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxMessageQueueError Receive(T& msg);
|
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Block until a message becomes available in the queue, but no more than
|
2008-03-09 08:33:59 -04:00
|
|
|
@a timeout milliseconds has elapsed.
|
2008-10-11 09:10:48 -04:00
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
If no message is available after @a timeout milliseconds then returns
|
2008-03-08 09:43:31 -05:00
|
|
|
@b wxMSGQUEUE_TIMEOUT.
|
2008-10-11 09:10:48 -04:00
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
If @a timeout is 0 then checks for any messages present in the queue
|
2008-03-08 09:43:31 -05:00
|
|
|
and returns immediately without waiting.
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2008-10-11 09:10:48 -04:00
|
|
|
The message is returned in @a msg.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-11 09:10:48 -04:00
|
|
|
wxMessageQueueError ReceiveTimeout(long timeout, T& msg);
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|