00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Benjamin Hadorn (bhadorn@swissinfo.org) * 00003 *************************************************************************** 00004 * Projekt : Zeus 00005 * Module : Event 00006 * Package : System 00007 * Author : Benjamin Hadorn 00008 * Datum : $Date: 5.10.09 22:14 $ 00009 * Ablage : $File$ 00010 * System : Cell Computing Model 00011 *************************************************************************** 00012 * Licence: * 00013 * This library is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU Lesser General Public License as * 00015 * published by the Free Software Foundation; either version * 00016 * 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00021 * GNU Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00026 ***************************************************************************/ 00027 00028 /*************************************************************************** 00029 Changes : 00030 $Log: /Development_F/StuderWIN/dev/Zeus/src/zeusbase/System/Event.h $ 00031 ** 00032 ** 15 5.10.09 22:14 Bha 00033 ** improving speed using inline methods 00034 ** 00035 ** 14 27.03.09 15:11 Bha 00036 ** new version 1.0 implemented 00037 ** 00038 ** 13 26.02.07 9:00 bha 00039 ** 00040 ** 9 2.10.06 9:20 bha 00041 ** Implementation for Linux 00042 ***************************************************************************/ 00043 00044 00045 #ifndef EventH 00046 #define EventH 00047 00048 #include <zeusbase/System/ZObject.h> 00049 00050 00051 #if defined(ENABLE_WIN32_BINDING) 00052 #include <windows.h> 00053 #elif defined(ENABLE_LINUX_BINDING) 00054 #include <pthread.h> 00055 #endif 00056 00057 BEGIN_NAMESPACE_Zeus 00058 00059 /***************************************************************************/ 00062 /***************************************************************************/ 00063 zeusbase_class TEvent : public TZObject 00064 { 00065 public: 00066 /***********************************************************************/ 00069 /***********************************************************************/ 00070 enum EWaitType 00071 { 00072 etBlocked = 0, 00073 etActive = 1 00074 }; 00075 00076 00077 TEvent(EWaitType eType = etBlocked); 00078 00079 void set(); 00080 void reset(); 00081 bool wait(Float64 dSeconds = 10.0); 00082 bool waitInfinite(); 00083 00084 protected: 00085 virtual ~TEvent(); 00086 00087 private : 00088 //Type of the event 00089 EWaitType m_eType; 00090 00091 #if defined(ENABLE_LINUX_BINDING) 00092 //Mutex 00093 pthread_mutex_t m_Mutex; 00094 //condition (event) 00095 pthread_cond_t m_CondVar; 00096 //Flag if the event is set 00097 bool m_bSet; 00098 00099 bool wait_internal(); 00100 static void cleanup(void* pPtr); 00101 00102 #else 00104 HANDLE m_hHandle; 00105 bool waitForActiveObject(Uint uiTimeOut); 00106 #endif 00107 }; 00108 00109 /***************************************************************************/ 00112 /***************************************************************************/ 00113 inline void TEvent::set() 00114 { 00115 #if defined(ENABLE_LINUX_BINDING) 00116 pthread_mutex_lock(&m_Mutex); 00117 m_bSet = true; 00118 00119 // signal all threads to wake up 00120 pthread_cond_broadcast(&m_CondVar); 00121 pthread_mutex_unlock(&m_Mutex); 00122 #else 00123 ::SetEvent(m_hHandle); 00124 #endif 00125 } 00126 00127 /***************************************************************************/ 00130 /***************************************************************************/ 00131 inline void TEvent::reset() 00132 { 00133 #if defined(ENABLE_LINUX_BINDING) 00134 m_bSet = false; 00135 #else 00136 ::ResetEvent(m_hHandle); 00137 #endif 00138 } 00139 00140 00141 END_NAMESPACE_Zeus 00142 00143 #endif