00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Semaphore 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 SemaphoreH 00033 #define SemaphoreH 00034 00035 #include <zeusbase/System/ZObject.h> 00036 00037 #if defined(ENABLE_WIN32_BINDING) 00038 #elif defined(ENABLE_LINUX_BINDING) 00039 #include <semaphore.h> 00040 #endif 00041 00042 00043 BEGIN_NAMESPACE_Zeus 00044 00045 /*************************************************************************/ 00048 /*************************************************************************/ 00049 zeusbase_class TSemaphore : public TZObject 00050 { 00051 public: 00052 TSemaphore(Uint uiInitValue = 0); 00053 TSemaphore(const TString& rName, Uint uiInitValue = 0, bool bOpenExisting = false); 00054 00055 bool acquire(); 00056 bool acquire(Int iPermits); 00057 bool existedAlready() const; 00058 void free(); 00059 void free(Int iPermits); 00060 Int getAvailablePermits() const; 00061 bool hasError() const; 00062 bool isLocked() const; 00063 bool isNamed() const; 00064 bool tryAcquire(); 00065 bool tryAcquire(const Float& rfTimeOut); 00066 00067 protected: 00068 virtual ~TSemaphore(); 00069 00070 private: 00072 bool m_bSemaError; 00074 bool m_bNamed; 00076 bool m_bExistedAlready; 00077 00078 #if defined(ENABLE_WIN32_BINDING) 00079 HANDLE m_hHandle; 00080 #elif defined(ENABLE_LINUX_BINDING) 00081 mutable sem_t* m_pSemID; 00082 mutable sem_t m_SemID; 00083 #endif 00084 00085 }; 00086 00087 /****************************************************************************/ 00090 /****************************************************************************/ 00091 inline bool TSemaphore::existedAlready() const 00092 { 00093 return m_bExistedAlready; 00094 } 00095 00096 /*************************************************************************/ 00099 /*************************************************************************/ 00100 inline void TSemaphore::free(Int iPermits) 00101 { 00102 for(Int i = 0; i < iPermits; i++) 00103 { 00104 free(); 00105 } 00106 } 00107 00108 /*************************************************************************/ 00111 /*************************************************************************/ 00112 inline bool TSemaphore::hasError() const 00113 { 00114 return m_bSemaError; 00115 } 00116 00117 /*************************************************************************/ 00120 /*************************************************************************/ 00121 inline bool TSemaphore::isLocked() const 00122 { 00123 return (getAvailablePermits() == 0); 00124 } 00125 00126 /*************************************************************************/ 00129 /*************************************************************************/ 00130 inline bool TSemaphore::isNamed() const 00131 { 00132 return m_bNamed; 00133 } 00134 00135 00136 END_NAMESPACE_Zeus 00137 00138 #endif