00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : ThreadMessageQueue 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef ThreadMessageQueueH 00033 #define ThreadMessageQueueH 00034 00035 #include <zeusbase/System/Interfaces/IThreadMessageQueue.hpp> 00036 #include <zeusbase/System/ZObject.h> 00037 #include <zeusbase/System/Queue.hpp> 00038 #include <zeusbase/System/Event.h> 00039 00040 BEGIN_NAMESPACE_Zeus 00041 00042 class TCriticalSection; 00043 00044 /**************************************************************************/ 00048 /**************************************************************************/ 00049 zeusbase_class TThreadMessageQueue : public TZObject, public IThreadMessageQueue 00050 { 00051 public: 00052 TThreadMessageQueue(); 00053 00054 //Methods of IThreadMessageQueue 00055 virtual void MQUALIFIER flush(); 00056 virtual Retval MQUALIFIER processObject(); 00057 virtual Retval MQUALIFIER postObject(ISynchronizeObject& rObject, bool bWaitForCompletion); 00058 virtual void MQUALIFIER lock(); 00059 virtual void MQUALIFIER unlock(); 00060 virtual bool MQUALIFIER isLocked() const; 00061 virtual bool MQUALIFIER isEmpty() const; 00062 00063 //Methods of IZUnknown 00064 MEMORY_MANAGER_DECL 00065 00066 protected: 00067 virtual ~TThreadMessageQueue(); 00068 00069 /**********************************************************************/ 00072 /**********************************************************************/ 00073 inline virtual void onItemPosted(ISynchronizeObject& /* rObject */, bool /* bWaitForCompletion */) 00074 {} 00075 00076 00077 private: 00078 00079 /************************************************************************/ 00082 /************************************************************************/ 00083 class TItemContainer : public TZObject 00084 { 00085 public: 00086 /********************************************************************/ 00089 /********************************************************************/ 00090 inline TItemContainer(ISynchronizeObject& rObject, bool bWaitForCompletion) 00091 : TZObject(), 00092 m_rObject(rObject) 00093 { 00094 m_rObject.addRef(); 00095 m_bWaitForCompletion = bWaitForCompletion; 00096 } 00097 00098 /********************************************************************/ 00101 /********************************************************************/ 00102 inline virtual ~TItemContainer() 00103 { 00104 m_rObject.release(); 00105 } 00106 00107 /********************************************************************/ 00110 /********************************************************************/ 00111 inline ISynchronizeObject& getObject() 00112 { 00113 return m_rObject; 00114 } 00115 00116 /********************************************************************/ 00119 /********************************************************************/ 00120 inline const ISynchronizeObject& getObject() const 00121 { 00122 return m_rObject; 00123 } 00124 00125 /********************************************************************/ 00128 /********************************************************************/ 00129 inline bool isWaitForCompletionSet() 00130 { 00131 return m_bWaitForCompletion; 00132 } 00133 00134 private: 00136 ISynchronizeObject& m_rObject; 00138 bool m_bWaitForCompletion; 00139 00140 }; 00141 00143 TQueue<TItemContainer*> m_lstQueue; 00145 bool m_bLocked; 00147 TCriticalSection& m_rLock; 00148 00149 00150 }; 00151 00152 00153 /**************************************************************************/ 00156 /**************************************************************************/ 00157 inline bool MQUALIFIER TThreadMessageQueue::isLocked() const 00158 { 00159 return m_bLocked; 00160 } 00161 00162 END_NAMESPACE_Zeus 00163 00164 00165 #endif