00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : CriticalSection 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 CriticalSectionH 00033 #define CriticalSectionH 00034 00035 #include <zeusbase/System/Interfaces/ICriticalSection.hpp> 00036 #include <zeusbase/System/ZObject.h> 00037 00038 #if defined(ENABLE_WIN32_BINDING) 00039 #if defined(USE_WINDOWS_H) 00040 #include <windows.h> 00041 #endif 00042 #elif defined(ENABLE_LINUX_BINDING) 00043 #include <pthread.h> 00044 #endif 00045 00046 00047 BEGIN_NAMESPACE_Zeus 00048 00049 /***************************************************************************/ 00052 /***************************************************************************/ 00053 zeusbase_class TCriticalSection : public TZObject, public ICriticalSection 00054 { 00055 public: 00056 /***********************************************************************/ 00060 /***********************************************************************/ 00061 enum ELockType 00062 { 00063 etFast, 00064 etRecursive, 00065 etError 00066 }; 00067 00068 TCriticalSection(ELockType eType = etFast); 00069 bool isLocked(); 00070 00071 //Methods of ICriticalSection 00072 virtual void MQUALIFIER enter(); 00073 virtual void MQUALIFIER leave(); 00074 00075 //Methods of IZUnknown 00076 MEMORY_MANAGER_DECL 00077 00078 protected: 00079 virtual ~TCriticalSection(); 00080 00081 private: 00082 #if defined(ENABLE_WIN32_BINDING) 00083 CRITICAL_SECTION m_CriticalSection; 00084 #elif defined(ENABLE_LINUX_BINDING) 00085 00086 pthread_mutex_t m_Mutex; 00088 pthread_mutexattr_t m_Attribute; 00089 #endif 00090 }; 00091 00092 /**************************************************************************/ 00095 /**************************************************************************/ 00096 inline void MQUALIFIER TCriticalSection::enter() 00097 { 00098 #if defined(ENABLE_WIN32_BINDING) 00099 ::EnterCriticalSection(&m_CriticalSection); 00100 #elif defined(ENABLE_LINUX_BINDING) 00101 ::pthread_mutex_lock(&m_Mutex); 00102 #endif 00103 } 00104 00105 00106 /**************************************************************************/ 00109 /**************************************************************************/ 00110 inline void MQUALIFIER TCriticalSection::leave() 00111 { 00112 #if defined(ENABLE_WIN32_BINDING) 00113 ::LeaveCriticalSection(&m_CriticalSection); 00114 #elif defined(ENABLE_LINUX_BINDING) 00115 ::pthread_mutex_unlock(&m_Mutex); 00116 #endif 00117 } 00118 00119 00120 END_NAMESPACE_Zeus 00121 00122 #endif 00123