UTMessageLoop.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _UT_MESSAGE_LOOP_H_
00026 #define _UT_MESSAGE_LOOP_H_
00027
00028
00029
00030
00031
00032 #include "UTThread.h"
00033 #include "UTMutex.h"
00034 #include "UTQuitRequestTask.h"
00035
00036
00037
00038
00039
00040 class MessageLoopPlatformInfo_t;
00041
00042
00043
00044 class UT_EXPORT MessageLoop_t
00045
00046 : public Thread_t,
00047 private BTreeNode_t< MessageLoop_t, MessageLoop_t* >
00059
00086 {
00087
00088 public:
00089
00090 MessageLoop_t( thread_priority_t priority = eTHREADPRI_normal,
00091 bool support_timeout = false,
00092 int32 timeout_ms = -1 );
00111
00112 virtual ~MessageLoop_t();
00119
00120 virtual void SetTimeout(int32 timeout_ms);
00128
00129 inline int32 Token() const;
00132
00133 virtual void Start(int32 stack_size = 0);
00146
00147 void AddTask(Task_t* task);
00153
00154 void RemoveTask(Task_t* task);
00156
00157 void RequestQuit(
00158 const MessageDestination_t<QuitResponseMessage_t>& respond_to );
00171
00172 void RequestQuit();
00180
00181 virtual void WaitForThreadToExit();
00186
00187 virtual void QuitRequested();
00200
00201 virtual bool IsQuitInProgress();
00210
00211 virtual void CancelQuit();
00213
00214
00215 protected:
00216
00217 virtual void Main();
00223
00224 virtual void Timeout();
00228
00229 virtual void SetMessageAvailEvent();
00231
00232
00233 private:
00234
00235
00236
00237 inline int Compare(MessageLoop_t* address) const;
00238 inline operator MessageLoop_t*() const;
00239
00240
00241 static void QueueMessage(takes Message_t* message);
00242
00243 gives Message_t* ExtractHeadMessage();
00244
00245 void DispatchMessage( const Message_t& message,
00246 const AbstractMessageDestination_t& destination );
00247 void DispatchMessage( takes Message_t* message,
00248 const AbstractMessageDestination_t& destination );
00249 void DispatchMessages();
00250
00251 static int32 NextMessageLoopToken();
00252
00253 void ResumeQuit();
00254
00255
00256 private:
00257
00258 int32 m_message_loop_token;
00259
00260 BTree_t< Task_t, Task_t* > m_tasks;
00261
00262 bool m_started;
00263 bool m_quit_confirmed;
00264 bool m_exit_confirmed;
00265 QuitRequestTask_t m_quit_request_task;
00266
00267
00268 bool m_quit;
00269 OwnedList_t< MessageDestination_t<QuitResponseMessage_t> > m_quit_response_destinations;
00270 bool m_quit_in_progress;
00271
00272
00273
00274 Message_t* volatile m_head_message;
00275 Message_t* volatile m_tail_message;
00276 int m_dispatching_messages_depth;
00277
00278 MessageLoopPlatformInfo_t* m_platform_info;
00279 bool m_support_timeout;
00280 int32 m_timeout;
00281
00282
00283 static Mutex_t ms_message_loops_and_queues_mutex;
00284 static BTree_t< MessageLoop_t, MessageLoop_t* > ms_message_loops;
00285
00286 friend class BTreeNode_t< MessageLoop_t, MessageLoop_t* >;
00287 friend class BTree_t< MessageLoop_t, MessageLoop_t* >;
00288 friend class AbstractMessageDestination_t;
00289 friend class MessageLoopPlatformInfo_t;
00290 friend class Task_t;
00291 friend class QuitRequestTask_t;
00292 friend class App_t;
00293 friend class AppPlatformInfo_t;
00294 friend class WindowPlatformInfo_t;
00295 friend class ViewPlatformInfo_t;
00296 };
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 inline int32
00309 MessageLoop_t::Token() const
00310 {
00311 return m_message_loop_token;
00312 }
00313
00314
00315 inline int
00316 MessageLoop_t::Compare(MessageLoop_t* address) const
00317 {
00318 if(this < address)
00319 return -1;
00320 else if(this > address)
00321 return 1;
00322 else
00323 return 0;
00324 }
00325
00326
00327 inline
00328 MessageLoop_t::operator MessageLoop_t*() const
00329 {
00330 return const_cast<MessageLoop_t*>(this);
00331 }
00332
00333
00334 #endif // _UT_MESSAGE_LOOP_H_