00001 /* 00002 * (c) Disney Enterprises, Inc. All rights reserved. 00003 * 00004 * This file is licensed under the terms of the Microsoft Public License (MS-PL) 00005 * as defined at: http://opensource.org/licenses/MS-PL. 00006 * 00007 * A complete copy of this license is included in this distribution as the file 00008 * LICENSE. 00009 */ 00010 #ifndef SeMutex_h 00011 #define SeMutex_h 00012 00013 00014 // #define DEBUG_THREADING 00015 00016 #include "SePlatform.h" 00017 00019 namespace SeExprInternal { 00020 #ifndef NDEBUG 00021 template <class T> 00022 class DebugLock : public T { 00023 public: 00024 DebugLock() : _locked(0) {} 00025 void lock() { T::lock(); _locked = 1; } 00026 void unlock() { assert(_locked); _locked = 0; T::unlock(); } 00027 bool locked() { return _locked != 0; } 00028 private: 00029 int _locked; 00030 }; 00031 #endif 00032 00034 template <class T> 00035 class AutoLock { 00036 public: 00037 AutoLock(T& m) : _m(m) { _m.lock(); } 00038 ~AutoLock() { _m.unlock(); } 00039 private: 00040 T& _m; 00041 }; 00042 00043 #ifndef NDEBUG 00044 // add debug wrappers to mutex and spinlock 00045 typedef DebugLock<_Mutex> Mutex; 00046 typedef DebugLock<_SpinLock> SpinLock; 00047 #else 00048 typedef _Mutex Mutex; 00049 typedef _SpinLock SpinLock; 00050 #endif 00051 00052 typedef AutoLock<Mutex> AutoMutex; 00053 typedef AutoLock<SpinLock> AutoSpin; 00054 } 00055 00056 #endif