NCBI C++ ToolKit
mutex_pool.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL___MUTEX_POOL__HPP
2 #define UTIL___MUTEX_POOL__HPP
3 
4 /* $Id: mutex_pool.hpp 52751 2012-01-24 19:50:51Z vasilche $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 * CMutexPool -- to distribute mutex pool among several objects.
33 *
34 */
35 
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbiobj.hpp>
39 #include <corelib/ncbimtx.hpp>
40 
41 #include <list>
42 
44 
45 class CInitMutexPool;
46 class CInitMutex_Base;
47 class CInitGuard;
48 
49 ////////////////////////////////////////////////////////////////////
50 //
51 // CMutexPool::
52 //
53 // Distribute a mutex pool among multiple objects
54 //
55 
56 
58 {
59 public:
60  CInitMutexPool(void);
61  ~CInitMutexPool(void);
62 
63  class CPoolMutex : public CObject
64  {
65  public:
67  : m_Pool(pool)
68  {
69  }
71  {
72  }
73 
74  CInitMutexPool& GetPool(void) const
75  {
76  return m_Pool;
77  }
79  {
80  return m_Mutex;
81  }
82 
83  private:
86  };
87  typedef CPoolMutex TMutex;
88 
89 protected:
90  friend class CInitGuard;
91 
92  bool AcquireMutex(CInitMutex_Base& init, CRef<TMutex>& mutex);
93  void ReleaseMutex(CInitMutex_Base& init, CRef<TMutex>& mutex);
94 
95 private:
96  typedef list< CRef<TMutex> > TMutexList;
97  TMutexList m_MutexList;
99 
100 private:
102  const CInitMutexPool& operator=(const CInitMutexPool&);
103 };
104 
105 
107 {
108 public:
109  DECLARE_OPERATOR_BOOL_REF(m_Object);
110 
111 protected:
113  {
114  }
115  // Copy constructor to allow CInitMutex_Base placement in STL containers.
116  // It doesn't copy mutex/object, just verifies that source is empty too.
118  {
119  _ASSERT(!mutex.m_Mutex && !mutex.m_Object);
120  }
122  {
123  _ASSERT(!m_Mutex || m_Mutex->ReferencedOnlyOnce());
124  }
125 
126  friend class CInitMutexPool;
127 
129 
132 
133 private:
135 };
136 
137 
138 template<class C>
140 {
141 public:
142  typedef C TObjectType;
143 
144  void Reset(void)
145  {
146  m_Object.Reset();
147  }
148  void Reset(TObjectType* object)
149  {
150  m_Object.Reset(object);
151  }
152 
153  inline
154  TObjectType& GetObject(void)
155  {
156  return static_cast<TObjectType&>(m_Object.GetObject());
157  }
158  inline
159  const TObjectType& GetObject(void) const
160  {
161  return static_cast<const TObjectType&>(m_Object.GetObject());
162  }
163  inline
164  TObjectType* GetPointer(void)
165  {
166  return static_cast<TObjectType*>(m_Object.GetPointer());
167  }
168  inline
169  const TObjectType* GetPointer(void) const
170  {
171  return static_cast<const TObjectType*>(m_Object.GetPointer());
172  }
173  inline
174  TObjectType* GetPointerOrNull(void)
175  {
176  return static_cast<TObjectType*>(m_Object.GetPointerOrNull());
177  }
178  inline
179  const TObjectType* GetPointerOrNull(void) const
180  {
181  return
182  static_cast<const TObjectType*>(m_Object.GetPointerOrNull());
183  }
184 
185  inline
186  TObjectType& operator*(void)
187  {
188  return GetObject();
189  }
190  inline
191  TObjectType* operator->(void)
192  {
193  return GetPointer();
194  }
195  inline
196  const TObjectType& operator*(void) const
197  {
198  return GetObject();
199  }
200  inline
201  const TObjectType* operator->(void) const
202  {
203  return GetPointer();
204  }
205 
207  {
209  return *this;
210  }
211  operator CRef<TObjectType>(void) const
212  {
213  return CRef<TObjectType>(const_cast<TObjectType*>(GetPointer()));
214  }
215  operator CConstRef<TObjectType>(void) const
216  {
218  }
219 };
220 
221 
223 {
224 public:
226  : m_Init(init), m_Guard(eEmptyGuard)
227  {
228  if ( !init && pool.AcquireMutex(init, m_Mutex) ) {
229  m_Guard.Guard(m_Mutex->GetMutex());
230  if ( init ) {
231  x_Release();
232  }
233  }
234  }
236  {
237  Release();
238  }
239 
240  void Release(void)
241  {
242  if ( m_Mutex ) {
243  x_Release();
244  }
245  }
246 
247  // true means that this thread should perform initialization
249 
250 protected:
252 
253  void x_Release(void)
254  {
255  m_Mutex->GetPool().ReleaseMutex(m_Init, m_Mutex);
256  m_Guard.Release();
257  }
258 
262 
263 private:
264  CInitGuard(const CInitGuard&);
265  const CInitGuard& operator=(const CInitGuard&);
266 };
267 
268 
270 
271 #endif /* UTIL___MUTEX_POOL__HPP */
void Release()
Manually force the resource to be released.
Definition: guard.hpp:167
CPoolMutex(CInitMutexPool &pool)
Definition: mutex_pool.hpp:66
CConstRef –.
Definition: ncbiobj.hpp:1192
void x_Release(void)
Definition: mutex_pool.hpp:253
const TObjectType * GetPointerOrNull(void) const
Definition: mutex_pool.hpp:179
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:970
const TObjectType & GetObject(void) const
Definition: mutex_pool.hpp:159
static int init(DBPROCESS *dbproc, const char *name)
Definition: bcp.c:44
TMutexList m_MutexList
Definition: mutex_pool.hpp:97
const TObjectType * GetPointer(void) const
Definition: mutex_pool.hpp:169
CInitMutexPool::TMutex TMutex
Definition: mutex_pool.hpp:128
void Guard(resource_type &resource)
Manually force the guard to protect some other resource.
Definition: guard.hpp:176
NCBI_XUTIL_EXPORT
Parameter to control printing diagnostic message about conversion of static array data from a differe...
Definition: static_set.hpp:72
TObjectType & operator*(void)
Definition: mutex_pool.hpp:186
CFastMutex –.
Definition: ncbimtx.hpp:630
list< CRef< TMutex > > TMutexList
Definition: mutex_pool.hpp:96
#define _DEBUG_ARG(arg)
Definition: ncbidbg.hpp:132
CMutex –.
Definition: ncbimtx.hpp:712
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:958
~CInitMutex_Base(void)
Definition: mutex_pool.hpp:121
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
const CInitMutex< TObjectType > & operator=(const CRef< TObjectType > &ref)
Definition: mutex_pool.hpp:206
CPoolMutex TMutex
Definition: mutex_pool.hpp:87
CMutexGuard m_Guard
Definition: mutex_pool.hpp:261
const CInitGuard & operator=(const CInitGuard &)
CInitMutexPool & GetPool(void) const
Definition: mutex_pool.hpp:74
CRef< TMutex > m_Mutex
Definition: mutex_pool.hpp:130
TObjectType * operator->(void)
Definition: mutex_pool.hpp:191
#define DECLARE_OPERATOR_BOOL_REF(Ref)
Declaration of safe bool operator from CRef<>/CConstRef<> expression.
Definition: ncbimisc.hpp:197
TObjectType * GetNCPointerOrNull(void) const THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:1134
void Reset(void)
Definition: mutex_pool.hpp:144
CFastMutex m_Pool_Mtx
Definition: mutex_pool.hpp:98
TObjectType * GetPointerOrNull(void)
Definition: mutex_pool.hpp:174
~CInitGuard(void)
Definition: mutex_pool.hpp:235
TObjectType * GetPointer(void)
Definition: mutex_pool.hpp:164
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx...
const TObjectType * operator->(void) const
Definition: mutex_pool.hpp:201
void Reset(TObjectType *object)
Definition: mutex_pool.hpp:148
CRef –.
Definition: ncbiobj.hpp:616
#define C(s)
Definition: common.h:226
DECLARE_OPERATOR_BOOL(!m_Init)
const TObjectType & operator*(void) const
Definition: mutex_pool.hpp:196
Multi-threading – mutexes; rw-locks; semaphore.
CObject –.
Definition: ncbiobj.hpp:180
CInitGuard(CInitMutex_Base &init, CInitMutexPool &pool)
Definition: mutex_pool.hpp:225
CRef< TMutex > m_Mutex
Definition: mutex_pool.hpp:260
TObjectType & GetObject(void)
Definition: mutex_pool.hpp:154
#define _ASSERT
CInitMutexPool & m_Pool
Definition: mutex_pool.hpp:84
void Release(void)
Definition: mutex_pool.hpp:240
bool AcquireMutex(CInitMutex_Base &init, CRef< TMutex > &mutex)
Definition: mutex_pool.cpp:53
CInitMutex_Base(const CInitMutex_Base &mutex)
Definition: mutex_pool.hpp:117
const CInitMutexPool & operator=(const CInitMutexPool &)
TObjectType & GetObject(void)
Get object.
Definition: ncbiobj.hpp:983
CRef< CObject > m_Object
Definition: mutex_pool.hpp:131
CInitMutex_Base & m_Init
Definition: mutex_pool.hpp:259
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
CInitMutexPool::TMutex TMutex
Definition: mutex_pool.hpp:251
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:756
Modified on Tue Jul 26 18:34:56 2016 by modify_doxy.py rev. 506947