1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Message queues for inter-thread communication
4 // Author: Evgeniy Tarassov
7 // Copyright: (C) 2007 TT-Solutions SARL
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_MSGQUEUE_H_
12 #define _WX_MSGQUEUE_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/thread.h"
22 #include "wx/stopwatch.h"
24 #include "wx/beforestd.h"
26 #include "wx/afterstd.h"
28 enum wxMessageQueueError
30 wxMSGQUEUE_NO_ERROR
= 0, // operation completed successfully
31 wxMSGQUEUE_TIMEOUT
, // no messages received before timeout expired
32 wxMSGQUEUE_MISC_ERROR
// some unexpected (and fatal) error has occurred
35 // ---------------------------------------------------------------------------
36 // Message queue allows passing message between threads.
38 // This class is typically used for communicating between the main and worker
39 // threads. The main thread calls Post() and the worker thread calls Receive().
41 // For this class a message is an object of arbitrary type T. Notice that
42 // typically there must be some special message indicating that the thread
43 // should terminate as there is no other way to gracefully shutdown a thread
44 // waiting on the message queue.
45 // ---------------------------------------------------------------------------
50 // The type of the messages transported by this queue
53 // Default ctor creates an initially empty queue
55 : m_conditionNotEmpty(m_mutex
)
59 // Add a message to this queue and signal the threads waiting for messages.
61 // This method is safe to call from multiple threads in parallel.
62 wxMessageQueueError
Post(const Message
& msg
)
64 wxMutexLocker
locker(m_mutex
);
66 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
70 m_conditionNotEmpty
.Signal();
72 return wxMSGQUEUE_NO_ERROR
;
75 // Wait no more than timeout milliseconds until a message becomes available.
77 // Setting timeout to 0 is equivalent to an infinite timeout. See Receive().
78 wxMessageQueueError
ReceiveTimeout(long timeout
, T
& msg
)
80 wxCHECK( IsOk(), wxMSGQUEUE_MISC_ERROR
);
82 wxMutexLocker
locker(m_mutex
);
84 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
86 const wxMilliClock_t waitUntil
= wxGetLocalTimeMillis() + timeout
;
87 while ( m_messages
.empty() )
89 wxCondError result
= m_conditionNotEmpty
.WaitTimeout(timeout
);
91 if ( result
== wxCOND_NO_ERROR
)
94 wxCHECK( result
== wxCOND_TIMEOUT
, wxMSGQUEUE_MISC_ERROR
);
96 const wxMilliClock_t now
= wxGetLocalTimeMillis();
98 if ( now
>= waitUntil
)
99 return wxMSGQUEUE_TIMEOUT
;
101 timeout
= (waitUntil
- now
).ToLong();
102 wxASSERT(timeout
> 0);
105 msg
= m_messages
.front();
108 return wxMSGQUEUE_NO_ERROR
;
111 // Same as ReceiveTimeout() but waits for as long as it takes for a message
112 // to become available (so it can't return wxMSGQUEUE_TIMEOUT)
113 wxMessageQueueError
Receive(T
& msg
)
115 wxCHECK( IsOk(), wxMSGQUEUE_MISC_ERROR
);
117 wxMutexLocker
locker(m_mutex
);
119 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
121 while ( m_messages
.empty() )
123 wxCondError result
= m_conditionNotEmpty
.Wait();
125 wxCHECK( result
== wxCOND_NO_ERROR
, wxMSGQUEUE_MISC_ERROR
);
128 msg
= m_messages
.front();
131 return wxMSGQUEUE_NO_ERROR
;
134 // Return false only if there was a fatal error in ctor
137 return m_conditionNotEmpty
.IsOk();
141 // Disable copy ctor and assignment operator
142 wxMessageQueue(const wxMessageQueue
<T
>& rhs
);
143 wxMessageQueue
<T
>& operator=(const wxMessageQueue
<T
>& rhs
);
145 mutable wxMutex m_mutex
;
146 wxCondition m_conditionNotEmpty
;
148 std::queue
<T
> m_messages
;
151 #endif // wxUSE_THREADS
153 #endif // _WX_MSGQUEUE_H_