00001 //------------------------------------------------------------------------------ 00002 // Lamp : Open source game middleware 00003 // Copyright (C) 2004 Junpei Ohtani ( Email : junpee@users.sourceforge.jp ) 00004 // 00005 // This library is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // This library is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with this library; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 //------------------------------------------------------------------------------ 00019 00020 /** @file 00021 * クリティカルセクションヘッダ 00022 * @author Junpee 00023 */ 00024 00025 #ifndef CRITICAL_SECTION_H_ 00026 #define CRITICAL_SECTION_H_ 00027 00028 #include <Core/Thread/LockObject.h> 00029 00030 namespace Lamp{ 00031 00032 //------------------------------------------------------------------------------ 00033 /** 00034 * クリティカルセクション 00035 */ 00036 class CriticalSection : public LockObject{ 00037 public: 00038 /** 00039 * コンストラクタ 00040 */ 00041 CriticalSection() : lockCount_(0), threadID_(invalidThreadID){ 00042 ::InitializeCriticalSection(&criticalSection_); 00043 } 00044 00045 /** 00046 * デストラクタ 00047 */ 00048 virtual ~CriticalSection(){ 00049 Assert(threadID_ == invalidThreadID); 00050 Assert(lockCount_ == 0); 00051 ::DeleteCriticalSection(&criticalSection_); 00052 } 00053 00054 /** 00055 * ロック 00056 */ 00057 virtual void lock(){ 00058 ::EnterCriticalSection(&criticalSection_); 00059 lockCount_++; 00060 threadID_ = ::GetCurrentThreadId(); 00061 } 00062 00063 /** 00064 * アンロック 00065 */ 00066 virtual void unlock(){ 00067 Assert(lockCount_ > 0); 00068 lockCount_--; 00069 if(lockCount_ == 0){ threadID_ = invalidThreadID; } 00070 ::LeaveCriticalSection(&criticalSection_); 00071 } 00072 00073 /** 00074 * カレントスレッドによってロックされているか 00075 * @return カレントスレッドによってロックされていればtrue 00076 */ 00077 virtual bool isLockedByCurrentThread() const{ 00078 return (::GetCurrentThreadId() == threadID_); 00079 } 00080 00081 protected: 00082 /// クリティカルセクションメンバ 00083 CRITICAL_SECTION criticalSection_; 00084 /// ロックカウント 00085 int lockCount_; 00086 /// スレッドID 00087 u_int threadID_; 00088 00089 /// 無効なスレッドID 00090 static const u_int invalidThreadID = 0xffffffff; 00091 00092 }; 00093 00094 //------------------------------------------------------------------------------ 00095 } // End of namespace Lamp 00096 #endif // End of CRITICAL_SECTION_H_ 00097 //------------------------------------------------------------------------------