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

Go to the SVN repository for this file.

1 #ifndef OBJECTS_OBJMGR_IMPL___TSE_LOCK__HPP
2 #define OBJECTS_OBJMGR_IMPL___TSE_LOCK__HPP
3 
4 /* $Id: tse_lock.hpp 44911 2010-02-25 16:06:44Z 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 * CTSE_Lock -- class to lock TSEs from garbage collector
33 *
34 */
35 
36 #include <corelib/ncbiobj.hpp>
37 
40 
41 class CTSE_Info;
42 class CTSE_LoadLock;
43 
45 {
46 public:
47  CTSE_Lock(void)
48  {
49  }
50  CTSE_Lock(const CTSE_Lock& lock)
51  {
52  x_Assign(lock);
53  }
54  CTSE_Lock(const CTSE_LoadLock& load_lock)
55  {
56  x_Assign(load_lock);
57  }
58  ~CTSE_Lock(void)
59  {
60  Reset();
61  }
63  {
64  if ( m_Info != lock.m_Info ) {
65  Reset();
66  x_Assign(lock);
67  }
68  return *this;
69  }
70 
71  operator const CTSE_Info*(void) const
72  {
73  return GetPointerOrNull();
74  }
75 
76  bool operator==(const CTSE_Lock& lock) const
77  {
78  return m_Info == lock.m_Info;
79  }
80  bool operator!=(const CTSE_Lock& lock) const
81  {
82  return m_Info != lock.m_Info;
83  }
84  bool operator<(const CTSE_Lock& lock) const
85  {
86  return m_Info < lock.m_Info;
87  }
88 
89  const CTSE_Info* GetPointerOrNull(void) const
90  {
91  return reinterpret_cast<const CTSE_Info*>
92  (m_Info.GetPointerOrNull());
93  }
94  const CTSE_Info* GetNonNullPointer(void) const
95  {
96  return reinterpret_cast<const CTSE_Info*>
97  (m_Info.GetNonNullPointer());
98  }
99  const CTSE_Info& operator*(void) const
100  {
101  return *GetNonNullPointer();
102  }
103  const CTSE_Info* operator->(void) const
104  {
105  return GetNonNullPointer();
106  }
107 
108  void Reset(void)
109  {
110  if ( m_Info ) {
111  x_Unlock();
112  }
113  }
114  void Drop(void)
115  {
116  if ( m_Info ) {
117  x_Drop();
118  }
119  }
120 
121  void Swap(CTSE_Lock& lock);
122 
123 protected:
124  // TSE locks can be aquired only through CDataSource.
125  friend class CDataSource;
126 
127  // returns true if first lock is aquired
128  bool Lock(const CTSE_Info* info)
129  {
130  if ( GetPointerOrNull() != info ) {
131  Reset();
132  if ( info ) {
133  return x_Lock(info);
134  }
135  }
136  return false;
137  }
138 
139  void x_Assign(const CTSE_Lock& lock)
140  {
141  const CTSE_Info* info = lock.GetPointerOrNull();
142  if ( info ) {
143  x_Relock(info);
144  }
145  }
146  void x_Assign(const CTSE_LoadLock& load_lock);
147 
148  void x_Unlock(void);
149  void x_Drop(void);
150  bool x_Lock(const CTSE_Info* info);
151  void x_Relock(const CTSE_Info* info);
152 
153 private:
154  // m_Info is declared as CRef<CObject> to avoid inclusion of tse_info.hpp
156 };
157 
158 
160 {
161 public:
164  const_iterator begin(void) const
165  {
166  return m_TSE_LockSet.begin();
167  }
168  const_iterator end(void) const
169  {
170  return m_TSE_LockSet.end();
171  }
172 
173  bool empty(void) const
174  {
175  return m_TSE_LockSet.empty();
176  }
177  size_t size(void) const
178  {
179  return m_TSE_LockSet.size();
180  }
181 
182  void clear(void);
183  void Drop(void);
184 
185  CTSE_Lock FindLock(const CTSE_Info* info) const;
186 
187  bool AddLock(const CTSE_Lock& lock);
188  bool PutLock(CTSE_Lock& lock);
189  bool RemoveLock(const CTSE_Lock& lock);
190  bool RemoveLock(const CTSE_Info* info);
191 
192  set<CTSE_Lock> GetBestTSEs(void) const;
193  static bool IsBetter(const CTSE_Info& tse1, const CTSE_Info& tse2);
194 
195 private:
196 
198 };
199 
200 
201 /////////////////////////////////////////////////////////////////////
202 //
203 // Inline methods
204 //
205 /////////////////////////////////////////////////////////////////////
206 
209 
210 #endif//OBJECTS_OBJMGR_IMPL___TSE_LOCK__HPP
void x_Assign(CObject_id &dst, const CObject_id &src)
Definition: Seq_id.cpp:203
TTSE_LockSet::const_iterator const_iterator
Definition: tse_lock.hpp:163
TTSE_LockSet m_TSE_LockSet
Definition: tse_lock.hpp:197
const_iterator begin(void) const
Definition: tse_lock.hpp:164
size_t size(void) const
Definition: tse_lock.hpp:177
bool empty(void) const
Definition: tse_lock.hpp:173
const_iterator end(void) const
Definition: tse_lock.hpp:168
map< const CTSE_Info *, CTSE_Lock > TTSE_LockSet
Definition: tse_lock.hpp:162
const CTSE_Info * operator->(void) const
Definition: tse_lock.hpp:103
bool operator==(const CTSE_Lock &lock) const
Definition: tse_lock.hpp:76
bool operator!=(const CTSE_Lock &lock) const
Definition: tse_lock.hpp:80
const CTSE_Info & operator*(void) const
Definition: tse_lock.hpp:99
CTSE_Lock(const CTSE_LoadLock &load_lock)
Definition: tse_lock.hpp:54
CConstRef< CObject > m_Info
Definition: tse_lock.hpp:155
void Drop(void)
Definition: tse_lock.hpp:114
CTSE_Lock(const CTSE_Lock &lock)
Definition: tse_lock.hpp:50
~CTSE_Lock(void)
Definition: tse_lock.hpp:58
CTSE_Lock & operator=(const CTSE_Lock &lock)
Definition: tse_lock.hpp:62
void Reset(void)
Definition: tse_lock.hpp:108
const CTSE_Info * GetPointerOrNull(void) const
Definition: tse_lock.hpp:89
CTSE_Lock(void)
Definition: tse_lock.hpp:47
void x_Assign(const CTSE_Lock &lock)
Definition: tse_lock.hpp:139
bool operator<(const CTSE_Lock &lock) const
Definition: tse_lock.hpp:84
bool Lock(const CTSE_Info *info)
Definition: tse_lock.hpp:128
const CTSE_Info * GetNonNullPointer(void) const
Definition: tse_lock.hpp:94
Definition: set.hpp:45
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NCBI_XOBJMGR_EXPORT
Definition: ncbi_export.h:1307
static MDB_envinfo info
Definition: mdb_load.c:37
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
Definition: swap.h:33
static string x_Lock(EMT_Lock how)
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Wed May 08 12:13:44 2024 by modify_doxy.py rev. 669887