Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vmutex_h 00009 #define vmutex_h 00010 00013 #include "vtypes.h" 00014 00015 #include "vthread_platform.h" 00016 #include "vstring.h" 00017 #include "vinstant.h" 00018 00037 class VMutex { 00038 public: 00039 00048 VMutex(const VString& name = VString::EMPTY(), bool suppressLogging = false); 00052 virtual ~VMutex(); 00053 00060 void setName(const VString& name); 00061 00066 VMutex_Type* getMutex(); 00067 00080 bool isLockedByCurrentThread() const; 00081 00082 /* PLATFORM-SPECIFIC STATIC FUNCTIONS -------------------------------- 00083 The remaining functions defined here are the low-level interfaces to 00084 the platform-specific threading APIs. These are implemented in each 00085 platform-specific version of vthread_platform.cpp. 00086 */ 00087 00094 static bool mutexInit(VMutex_Type* mutex); 00095 00101 static void mutexDestroy(VMutex_Type* mutex); 00102 00108 static bool mutexLock(VMutex_Type* mutex); 00109 00115 static bool mutexUnlock(VMutex_Type* mutex); 00116 00125 static void setLockDelayLoggingThreshold(const VDuration& threshold) { gVMutexLockDelayLoggingThreshold = threshold; } 00126 static VDuration getLockDelayLoggingThreshold() { return gVMutexLockDelayLoggingThreshold; } 00127 static void setLockDelayLoggingLevel(int logLevel) { gVMutexLockDelayLoggingLevel = logLevel; } 00128 static int getLockDelayLoggingLevel() { return gVMutexLockDelayLoggingLevel; } 00129 00130 private: 00131 00132 VMutex(const VMutex&); // not copyable 00133 VMutex& operator=(const VMutex&); // not assignable 00134 00135 // _lock() and _unlock() are only accessible to the locker and unlocker classes, and unit test. 00136 friend class VMutexLocker; 00137 friend class VMutexUnlocker; 00138 friend class VThreadsUnit; 00139 00148 void _lock(const VString& lockerName = VString::EMPTY()); 00154 void _unlock(); 00155 00156 VMutex_Type mMutex; 00157 VString mName; 00158 bool mSuppressLogging; 00159 volatile VThreadID_Type mLastLockThread; 00160 VString mLastLockerName; 00161 VInstant mLastLockTime; 00162 volatile bool mIsLocked; 00163 00164 static VDuration gVMutexLockDelayLoggingThreshold; 00165 static int gVMutexLockDelayLoggingLevel; 00166 }; 00167 00168 #endif /* vmutex_h */ 00169