00001 /*************************************************************************** 00002 * Copyright (C) 2009 by Benjamin Hadorn (b_hadorn@bluewin.ch) * 00003 *************************************************************************** 00004 * Projekt : Zeus Base Library 00005 * Module : Semaphore 00006 * Package : System 00007 * Author : Benjamin Hadorn 00008 * Datum : $Date: 27.03.09 13:58 $ 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/Semaphore.h $ 00031 ** 00032 ** 1 27.03.09 13:58 Bha 00033 ** created 00034 ***************************************************************************/ 00035 00036 #ifndef SemaphoreH 00037 #define SemaphoreH 00038 00039 #include <zeusbase/System/ZObject.h> 00040 00041 #if defined(ENABLE_WIN32_BINDING) 00042 #elif defined(ENABLE_LINUX_BINDING) 00043 #include <semaphore.h> 00044 #endif 00045 00046 00047 BEGIN_NAMESPACE_Zeus 00048 00049 /*************************************************************************/ 00052 /*************************************************************************/ 00053 zeusbase_class TSemaphore : public TZObject 00054 { 00055 public: 00056 TSemaphore(Uint uiInitValue = 0); 00057 TSemaphore(const TString& rName, Uint uiInitValue = 0, bool bOpenExisting = false); 00058 00059 bool acquire(); 00060 bool acquire(Int iPermits); 00061 bool existedAlready() const; 00062 void free(); 00063 void free(Int iPermits); 00064 Int getAvailablePermits() const; 00065 bool hasError() const; 00066 bool isLocked() const; 00067 bool isNamed() const; 00068 bool tryAcquire(); 00069 bool tryAcquire(const Float& rfTimeOut); 00070 00071 protected: 00072 virtual ~TSemaphore(); 00073 00074 private: 00076 bool m_bSemaError; 00078 bool m_bNamed; 00080 bool m_bExistedAlready; 00081 00082 #if defined(ENABLE_WIN32_BINDING) 00083 HANDLE m_hHandle; 00084 #elif defined(ENABLE_LINUX_BINDING) 00085 mutable sem_t* m_pSemID; 00086 mutable sem_t m_SemID; 00087 #endif 00088 00089 }; 00090 00091 /****************************************************************************/ 00094 /****************************************************************************/ 00095 inline bool TSemaphore::existedAlready() const 00096 { 00097 return m_bExistedAlready; 00098 } 00099 00100 /*************************************************************************/ 00103 /*************************************************************************/ 00104 inline void TSemaphore::free(Int iPermits) 00105 { 00106 for(Int i = 0; i < iPermits; i++) 00107 { 00108 free(); 00109 } 00110 } 00111 00112 /*************************************************************************/ 00115 /*************************************************************************/ 00116 inline bool TSemaphore::hasError() const 00117 { 00118 return m_bSemaError; 00119 } 00120 00121 /*************************************************************************/ 00124 /*************************************************************************/ 00125 inline bool TSemaphore::isLocked() const 00126 { 00127 return (getAvailablePermits() == 0); 00128 } 00129 00130 /*************************************************************************/ 00133 /*************************************************************************/ 00134 inline bool TSemaphore::isNamed() const 00135 { 00136 return m_bNamed; 00137 } 00138 00139 00140 END_NAMESPACE_Zeus 00141 00142 #endif