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 99880 2023-05-18 17:38:00Z 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  // assign a mutex to 'init' and store it also to CRef<> 'mutex'
93  // if the 'init' was initialized already and 'force' == false then no mutex is assigned
94  // return true if the mutex was assigned
95  bool AcquireMutex(CInitMutex_Base& init, CRef<TMutex>& mutex, bool force = false);
96  // release mutex from 'init' if it was successfully initialized
97  void ReleaseMutex(CInitMutex_Base& init, CRef<TMutex>& mutex);
98 
99 private:
100  typedef list< CRef<TMutex> > TMutexList;
103 
104 private:
107 };
108 
109 
111 {
112 public:
113  DECLARE_OPERATOR_BOOL(m_Initialized.load(memory_order_acquire));
114 
115 protected:
117  : m_Initialized(false)
118  {
119  }
120  // Copy constructor to allow CInitMutex_Base placement in STL containers.
121  // It doesn't copy mutex/object, just verifies that source is empty too.
123  : m_Initialized(false)
124  {
125  _ASSERT(!mutex);
126  }
128  {
129  _ASSERT(!m_Mutex || m_Mutex->ReferencedOnlyOnce());
130  }
131 
132  friend class CInitMutexPool;
133 
135 
138  atomic<bool> m_Initialized;
139 
140 private:
141  const CInitMutex_Base& operator=(const CInitMutex_Base&) = delete;
142 };
143 
144 
145 template<class C>
147 {
148 public:
149  typedef C TObjectType;
150 
152 
153  void Reset()
154  {
155  m_Initialized = false;
156  m_Object.Reset();
157  m_Initialized = false;
158  }
159  void Reset(TObjectType* object)
160  {
161  m_Initialized = false;
162  m_Object.Reset(object);
163  m_Initialized = (object != nullptr);
164  }
165 
166  inline
168  {
169  return static_cast<TObjectType&>(m_Object.GetObject());
170  }
171  inline
172  const TObjectType& GetObject(void) const
173  {
174  return static_cast<const TObjectType&>(m_Object.GetObject());
175  }
176  inline
178  {
179  return static_cast<TObjectType*>(m_Object.GetPointer());
180  }
181  inline
182  const TObjectType* GetPointer(void) const
183  {
184  return static_cast<const TObjectType*>(m_Object.GetPointer());
185  }
186  inline
188  {
189  return static_cast<TObjectType*>(m_Object.GetPointerOrNull());
190  }
191  inline
192  const TObjectType* GetPointerOrNull(void) const
193  {
194  return
195  static_cast<const TObjectType*>(m_Object.GetPointerOrNull());
196  }
197 
198  inline
200  {
201  return GetObject();
202  }
203  inline
205  {
206  return GetPointer();
207  }
208  inline
209  const TObjectType& operator*(void) const
210  {
211  return GetObject();
212  }
213  inline
214  const TObjectType* operator->(void) const
215  {
216  return GetPointer();
217  }
218 
220  {
221  Reset(ref.GetNCPointerOrNull());
222  return *this;
223  }
224  operator CRef<TObjectType>(void) const
225  {
226  return CRef<TObjectType>(const_cast<TObjectType*>(GetPointer()));
227  }
228  operator CConstRef<TObjectType>(void) const
229  {
231  }
232 };
233 
234 
236 {
237 public:
238  enum EForce {
239  force
240  };
243  {
244  if ( !init && pool.AcquireMutex(init, m_Mutex) ) {
245  m_Guard.Guard(m_Mutex->GetMutex());
246  if ( init ) {
247  x_Release();
248  }
249  }
250  }
253  {
254  if ( pool.AcquireMutex(init, m_Mutex, true) ) {
255  m_Guard.Guard(m_Mutex->GetMutex());
256  }
257  }
259  {
260  Release();
261  }
262 
263  void Release(void)
264  {
265  if ( m_Mutex ) {
266  x_Release();
267  }
268  }
269 
271  {
272  pool.AcquireMutex(init, m_Mutex, true);
273  m_Guard.Guard(m_Mutex->GetMutex());
274  }
275 
276  // true means that this thread should perform initialization
278 
279 protected:
281 
282  void x_Release(void)
283  {
284  m_Mutex->GetPool().ReleaseMutex(m_Init, m_Mutex);
285  m_Guard.Release();
286  }
287 
291 
292 private:
295 };
296 
297 
299 
300 #endif /* UTIL___MUTEX_POOL__HPP */
#define false
Definition: bool.h:36
CConstRef –.
Definition: ncbiobj.hpp:1266
CFastMutex –.
Definition: ncbimtx.hpp:667
void Guard(resource_type &resource)
Manually force the guard to protect some other resource.
Definition: guard.hpp:175
void Release()
Manually force the resource to be released.
Definition: guard.hpp:166
CInitMutex_Base & m_Init
Definition: mutex_pool.hpp:288
~CInitGuard(void)
Definition: mutex_pool.hpp:258
void Release(void)
Definition: mutex_pool.hpp:263
CInitGuard(const CInitGuard &)
DECLARE_OPERATOR_BOOL(!m_Init)
void x_Release(void)
Definition: mutex_pool.hpp:282
CInitMutexPool::TMutex TMutex
Definition: mutex_pool.hpp:280
const CInitGuard & operator=(const CInitGuard &)
CRef< TMutex > m_Mutex
Definition: mutex_pool.hpp:289
CInitGuard(CInitMutex_Base &init, CInitMutexPool &pool, EForce)
Definition: mutex_pool.hpp:251
void ForceGuard(CInitMutex_Base &init, CInitMutexPool &pool)
Definition: mutex_pool.hpp:270
CInitGuard(CInitMutex_Base &init, CInitMutexPool &pool)
Definition: mutex_pool.hpp:241
CMutexGuard m_Guard
Definition: mutex_pool.hpp:290
CInitMutexPool & m_Pool
Definition: mutex_pool.hpp:84
CInitMutexPool & GetPool(void) const
Definition: mutex_pool.hpp:74
CPoolMutex(CInitMutexPool &pool)
Definition: mutex_pool.hpp:66
list< CRef< TMutex > > TMutexList
Definition: mutex_pool.hpp:100
CFastMutex m_Pool_Mtx
Definition: mutex_pool.hpp:102
TMutexList m_MutexList
Definition: mutex_pool.hpp:101
const CInitMutexPool & operator=(const CInitMutexPool &)
CInitMutexPool(const CInitMutexPool &)
CPoolMutex TMutex
Definition: mutex_pool.hpp:87
bool AcquireMutex(CInitMutex_Base &init, CRef< TMutex > &mutex, bool force=false)
Definition: mutex_pool.cpp:53
CRef< TMutex > m_Mutex
Definition: mutex_pool.hpp:136
const CInitMutex_Base & operator=(const CInitMutex_Base &)=delete
atomic< bool > m_Initialized
Definition: mutex_pool.hpp:138
CRef< CObject > m_Object
Definition: mutex_pool.hpp:137
DECLARE_OPERATOR_BOOL(m_Initialized.load(memory_order_acquire))
~CInitMutex_Base(void)
Definition: mutex_pool.hpp:127
CInitMutexPool::TMutex TMutex
Definition: mutex_pool.hpp:134
TObjectType * GetPointerOrNull(void)
Definition: mutex_pool.hpp:187
const TObjectType * GetPointerOrNull(void) const
Definition: mutex_pool.hpp:192
TObjectType * operator->(void)
Definition: mutex_pool.hpp:204
TObjectType & GetObject(void)
Definition: mutex_pool.hpp:167
const TObjectType & operator*(void) const
Definition: mutex_pool.hpp:209
const TObjectType & GetObject(void) const
Definition: mutex_pool.hpp:172
void Reset(TObjectType *object)
Definition: mutex_pool.hpp:159
const TObjectType * GetPointer(void) const
Definition: mutex_pool.hpp:182
TObjectType & operator*(void)
Definition: mutex_pool.hpp:199
void Reset()
Definition: mutex_pool.hpp:153
const TObjectType * operator->(void) const
Definition: mutex_pool.hpp:214
TObjectType * GetPointer(void)
Definition: mutex_pool.hpp:177
const CInitMutex< TObjectType > & operator=(const CRef< TObjectType > &ref)
Definition: mutex_pool.hpp:219
CMutex –.
Definition: ncbimtx.hpp:749
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define C(s)
Definition: common.h:231
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
#define _DEBUG_ARG(arg)
Definition: ncbidbg.hpp:134
TObjectType * GetNCPointerOrNull(void) const THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:1162
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType & GetObject(void)
Get object.
Definition: ncbiobj.hpp:1011
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:986
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
@ eEmptyGuard
Definition: guard.hpp:94
Multi-threading – mutexes; rw-locks; semaphore.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
NCBI_XUTIL_EXPORT
Parameter to control printing diagnostic message about conversion of static array data from a differe...
Definition: static_set.hpp:72
#define _ASSERT
Modified on Thu Feb 29 12:16:38 2024 by modify_doxy.py rev. 669887