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/beforestd.h"
24 #include "wx/afterstd.h"
26 enum wxMessageQueueError
28 wxMSGQUEUE_NO_ERROR
= 0, // operation completed successfully
29 wxMSGQUEUE_TIMEOUT
, // no messages received before timeout expired
30 wxMSGQUEUE_MISC_ERROR
// some unexpected (and fatal) error has occurred
33 // ---------------------------------------------------------------------------
34 // Message queue allows passing message between threads.
36 // This class is typically used for communicating between the main and worker
37 // threads. The main thread calls Post() and the worker thread calls Receive().
39 // For this class a message is an object of arbitrary type T. Notice that
40 // typically there must be some special message indicating that the thread
41 // should terminate as there is no other way to gracefully shutdown a thread
42 // waiting on the message queue.
43 // ---------------------------------------------------------------------------
48 // The type of the messages transported by this queue
51 // Default ctor creates an initially empty queue
53 : m_conditionNotEmpty(m_mutex
)
57 // Add a message to this queue and signal the threads waiting for messages.
59 // This method is safe to call from multiple threads in parallel.
60 wxMessageQueueError
Post(const Message
& msg
)
62 wxMutexLocker
locker(m_mutex
);
64 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
68 m_conditionNotEmpty
.Signal();
70 return wxMSGQUEUE_NO_ERROR
;
73 // Wait no more than timeout milliseconds until a message becomes available.
75 // Setting timeout to 0 is equivalent to an infinite timeout. See Receive().
76 wxMessageQueueError
ReceiveTimeout(long timeout
, T
& msg
)
78 wxCHECK( IsOk(), wxMSGQUEUE_MISC_ERROR
);
80 wxMutexLocker
locker(m_mutex
);
82 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
84 const wxMilliClock_t waitUntil
= wxGetLocalTimeMillis() + timeout
;
85 while ( m_messages
.empty() )
87 wxCondError result
= m_conditionNotEmpty
.WaitTimeout(timeout
);
89 if ( result
== wxCOND_NO_ERROR
)
92 wxCHECK( result
== wxCOND_TIMEOUT
, wxMSGQUEUE_MISC_ERROR
);
94 const wxMilliClock_t now
= wxGetLocalTimeMillis();
96 if ( now
>= waitUntil
)
97 return wxMSGQUEUE_TIMEOUT
;
99 timeout
= (waitUntil
- now
).ToLong();
100 wxASSERT(timeout
> 0);
103 msg
= m_messages
.front();
106 return wxMSGQUEUE_NO_ERROR
;
109 // Same as ReceiveTimeout() but waits for as long as it takes for a message
110 // to become available (so it can't return wxMSGQUEUE_TIMEOUT)
111 wxMessageQueueError
Receive(T
& msg
)
113 wxCHECK( IsOk(), wxMSGQUEUE_MISC_ERROR
);
115 wxMutexLocker
locker(m_mutex
);
117 wxCHECK( locker
.IsOk(), wxMSGQUEUE_MISC_ERROR
);
119 while ( m_messages
.empty() )
121 wxCondError result
= m_conditionNotEmpty
.Wait();
123 wxCHECK( result
== wxCOND_NO_ERROR
, wxMSGQUEUE_MISC_ERROR
);
126 msg
= m_messages
.front();
129 return wxMSGQUEUE_NO_ERROR
;
132 // Return false only if there was a fatal error in ctor
135 return m_conditionNotEmpty
.IsOk();
139 // Disable copy ctor and assignment operator
140 wxMessageQueue(const wxMessageQueue
<T
>& rhs
);
141 wxMessageQueue
<T
>& operator=(const wxMessageQueue
<T
>& rhs
);
143 mutable wxMutex m_mutex
;
144 wxCondition m_conditionNotEmpty
;
146 std::queue
<T
> m_messages
;
149 #endif // wxUSE_THREADS
151 #endif // _WX_MSGQUEUE_H_