00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Benjamin Hadorn (bhadorn@swissinfo.org) * 00003 *************************************************************************** 00004 * Projekt : Zeus 00005 * Module : CriticalSection 00006 * Package : System 00007 * Author : Benjamin Hadorn 00008 * Datum : $Date: 5.10.09 22:13 $ 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/CriticalSection.h $ 00031 ** 00032 ** 11 5.10.09 22:13 Bha 00033 ** improving speed using inline methods 00034 ** 00035 ** 10 27.03.09 15:11 Bha 00036 ** new version 1.0 implemented 00037 ** 00038 ** 9 23.04.08 16:24 Bha 00039 ** windows includes depending on the usage flag 00040 ** 00041 ** 8 26.02.07 9:00 bha 00042 ***************************************************************************/ 00043 00044 #ifndef CriticalSectionH 00045 #define CriticalSectionH 00046 00047 #include <zeusbase/System/Interfaces/ICriticalSection.hpp> 00048 #include <zeusbase/System/ZObject.h> 00049 00050 #if defined(ENABLE_WIN32_BINDING) 00051 #if defined(USE_WINDOWS_H) 00052 #include <windows.h> 00053 #endif 00054 #elif defined(ENABLE_LINUX_BINDING) 00055 #include <pthread.h> 00056 #endif 00057 00058 00059 BEGIN_NAMESPACE_Zeus 00060 00061 /***************************************************************************/ 00064 /***************************************************************************/ 00065 zeusbase_class TCriticalSection : public TZObject, public ICriticalSection 00066 { 00067 public: 00068 /***********************************************************************/ 00072 /***********************************************************************/ 00073 enum ELockType 00074 { 00075 etFast, 00076 etRecursive, 00077 etError 00078 }; 00079 00080 TCriticalSection(ELockType eType = etFast); 00081 bool isLocked(); 00082 00083 //Methods of ICriticalSection 00084 virtual void MQUALIFIER enter(); 00085 virtual void MQUALIFIER leave(); 00086 00087 //Methods of IZUnknown 00088 MEMORY_MANAGER_DECL 00089 00090 protected: 00091 virtual ~TCriticalSection(); 00092 00093 private: 00094 #if defined(ENABLE_WIN32_BINDING) 00095 CRITICAL_SECTION m_CriticalSection; 00096 #elif defined(ENABLE_LINUX_BINDING) 00098 pthread_mutex_t m_Mutex; 00100 pthread_mutexattr_t m_Attribute; 00101 #endif 00102 }; 00103 00104 /**************************************************************************/ 00107 /**************************************************************************/ 00108 inline void MQUALIFIER TCriticalSection::enter() 00109 { 00110 #if defined(ENABLE_WIN32_BINDING) 00111 ::EnterCriticalSection(&m_CriticalSection); 00112 #elif defined(ENABLE_LINUX_BINDING) 00113 ::pthread_mutex_lock(&m_Mutex); 00114 #endif 00115 } 00116 00117 00118 /**************************************************************************/ 00121 /**************************************************************************/ 00122 inline void MQUALIFIER TCriticalSection::leave() 00123 { 00124 #if defined(ENABLE_WIN32_BINDING) 00125 ::LeaveCriticalSection(&m_CriticalSection); 00126 #elif defined(ENABLE_LINUX_BINDING) 00127 ::pthread_mutex_unlock(&m_Mutex); 00128 #endif 00129 } 00130 00131 00132 END_NAMESPACE_Zeus 00133 00134 #endif 00135