UTMessageLoop.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_MESSAGE_LOOP_H_
00026 #define _UT_MESSAGE_LOOP_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== Project headers
00031 //==================================================================================================
00032 #include "UTThread.h"
00033 #include "UTMutex.h"
00034 #include "UTQuitRequestTask.h"
00035 
00036 
00037 //==================================================================================================
00038 //=== Forward name declarations
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     // m_message_loops tree management
00237     inline int              Compare(MessageLoop_t* address) const;
00238     inline                  operator MessageLoop_t*() const;
00239 
00240     // Message queueing and dispatching
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     // This pair is protected by ms_message_loops_and_queues_mutex
00268     bool                                                        m_quit;
00269     OwnedList_t< MessageDestination_t<QuitResponseMessage_t> >  m_quit_response_destinations;
00270     bool                                                        m_quit_in_progress;
00271 
00272     // Message queue could be a LinkedList_t, but we don't need all that state, and this allows the
00273     // minimum amount of list mutex protection.
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     // The mutex protects the message loops list and, for each message loop, the tail message.
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 //=== Template function implementations: class MessageLoop_t
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_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1