gatelib  2.1
g_mthread_ObjWithLock.h
1 #pragma once
2 #include "g_mthread_CriticalSection.h"
3 #include <string>
4 
5 #define G_LOCK_DEFAULT_TIMEOUT 5.0
6 
7 namespace g
8 {
9 namespace mthread
10 {
11 
12 G_EXC_DEFINE_MSG ( GenericLockTimeoutElapsedException , g::Exception , "Lock timeout elapsed." );
13 G_EXC_DEFINE ( CircularDeadlockDetectedException , g::Exception );
14 
15 #define G_LOCK_CONTEXT(aobjwithlock) (aobjwithlock).lock ( _g_method_context )
16 #define G_LOCK_CONTEXT_TIMEOUT(aobjwithlock,atimeout) (aobjwithlock).lock ( _g_method_context , atimeout )
17 #define G_UNLOCK(aobjwithlock) (aobjwithlock).unlock ( );
18 
19 class G_LIB_ITEM ObjWithLock
20 {
21  G_COPY_CONSTRUCTOR_PROHIBITED ( ObjWithLock );
22  friend class TimeoutTicket;
23 public:
24  ObjWithLock(){}
25  virtual ~ObjWithLock(){}
26 
27  bool tryLock ( ) { return mInnerCriticalSection.tryWait(); }
28  void lock ( const char* method_context = "" ) { lock ( lock_Timeout , method_context ); }
29  void lock ( double aLockTimeout , const char* method_context = "" ) { if ( !mInnerCriticalSection.wait(aLockTimeout) ) { mCheckForDeadlock ( method_context ); } }
30  void unlock ( ) { mInnerCriticalSection.release (); }
31 
32  static double lock_Timeout;
33 private:
34  void mCheckForDeadlock ( const char* method_context );
35 
36  CriticalSection mInnerCriticalSection;
37 };
38 
39 }//namespace mthread
40 }//namespace g
#define G_EXC_DEFINE_MSG(aexctypename, abaseexctypename, amsg)
Definition: g_exception_macros.h:43
#define G_EXC_DEFINE(aexctypename, abaseexctypename)
Definition: g_exception_macros.h:39
Definition: g.mthread.ThreadSimpleEvent.h:5
Anchestor Exception class for g::lib.
Definition: g_Exception.h:17
Definition: g_mthread_CriticalSection.h:12
Definition: g_mthread_ObjWithLock.h:19