Ice 3.7 C++11 API Reference
MutexPtrLock.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_MUTEX_PTR_LOCK_H
6 #define ICE_UTIL_MUTEX_PTR_LOCK_H
7 
8 #include <IceUtil/Config.h>
10 
11 namespace IceUtilInternal
12 {
13 
14 template<class T>
15 class MutexPtrLock
16 {
17 public:
18 
19  MutexPtrLock(T* mutex) :
20  _mutex(mutex),
21  _acquired(false)
22  {
23  if(_mutex)
24  {
25  _mutex->lock();
26  _acquired = true;
27  }
28  }
29 
30  ~MutexPtrLock()
31  {
32  if(_mutex && _acquired)
33  {
34  _mutex->unlock();
35  }
36  }
37 
38  void acquire() const
39  {
40  if(_mutex)
41  {
42  _mutex->lock();
43  _acquired = true;
44  }
45  }
46 
47  void release() const
48  {
49  if(_mutex)
50  {
51  if(!_acquired)
52  {
53  throw IceUtil::ThreadLockedException(__FILE__, __LINE__);
54  }
55  _mutex->unlock();
56  _acquired = false;
57  }
58  }
59 
60  bool acquired() const
61  {
62  return _acquired;
63  }
64 
65 private:
66 
67  // Not implemented; prevents accidental use.
68  //
69  MutexPtrLock(const MutexPtrLock<T>&);
70  MutexPtrLock& operator=(const MutexPtrLock<T>&);
71 
72  const T* _mutex;
73  mutable bool _acquired;
74 };
75 
76 } // End namespace IceUtilInternal
77 
78 #endif
IceUtil::ThreadLockedException
Definition: ThreadException.h:27
Config.h
ThreadException.h