00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Event 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 00033 #ifndef EventH 00034 #define EventH 00035 00036 #include <zeusbase/System/ZObject.h> 00037 #include <zeusbase/System/Interfaces/IEvent.hpp> 00038 00039 00040 #if defined(ENABLE_WIN32_BINDING) 00041 #include <windows.h> 00042 #elif defined(ENABLE_LINUX_BINDING) 00043 #include <pthread.h> 00044 #endif 00045 00046 BEGIN_NAMESPACE_Zeus 00047 00048 class TCriticalSection; 00049 00050 /***************************************************************************/ 00053 /***************************************************************************/ 00054 zeusbase_class TEvent : public TZObject, public IEvent 00055 { 00056 public: 00057 TEvent(EWaitType eType = etBlocked); 00058 00059 //Methods of IEvent 00060 virtual void MQUALIFIER set(); 00061 virtual void MQUALIFIER reset(); 00062 virtual bool MQUALIFIER wait(const Float64& rfSeconds = 10.0); 00063 virtual bool MQUALIFIER waitInfinite(); 00064 00065 //Methods of IZUnknown 00066 MEMORY_MANAGER_DECL 00067 00068 protected: 00069 virtual ~TEvent(); 00070 00071 private : 00072 //Type of the event 00073 EWaitType m_eType; 00075 //TCriticalSection& m_rLock; 00076 00077 #if defined(ENABLE_LINUX_BINDING) 00078 //Mutex 00079 pthread_mutex_t m_Mutex; 00080 //condition (event) 00081 pthread_cond_t m_CondVar; 00082 //Flag if the event is set 00083 bool m_bSet; 00084 00085 bool wait_internal(); 00086 static void cleanup(void* pPtr); 00087 00088 #else 00089 00090 HANDLE m_hHandle; 00091 bool waitForActiveObject(Uint uiTimeOut); 00092 #endif 00093 }; 00094 00095 /***************************************************************************/ 00098 /***************************************************************************/ 00099 inline void MQUALIFIER TEvent::set() 00100 { 00101 #if defined(ENABLE_LINUX_BINDING) 00102 pthread_mutex_lock(&m_Mutex); 00103 m_bSet = true; 00104 00105 // signal all threads to wake up 00106 pthread_cond_broadcast(&m_CondVar); 00107 pthread_mutex_unlock(&m_Mutex); 00108 #else 00109 ::SetEvent(m_hHandle); 00110 #endif 00111 } 00112 00113 /***************************************************************************/ 00116 /***************************************************************************/ 00117 inline void MQUALIFIER TEvent::reset() 00118 { 00119 #if defined(ENABLE_LINUX_BINDING) 00120 m_bSet = false; 00121 #else 00122 ::ResetEvent(m_hHandle); 00123 #endif 00124 } 00125 00126 00127 END_NAMESPACE_Zeus 00128 00129 #endif