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

Go to the SVN repository for this file.

1 #ifndef GBLOADER_UTIL_HPP_INCLUDED
2 #define GBLOADER_UTIL_HPP_INCLUDED
3 
4 /* $Id: gbload_util.hpp 63636 2014-07-16 15:38:41Z 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: Michael Kimelman
30 *
31 * File Description:
32 * GB loader Utilities
33 *
34 * ===========================================================================
35 */
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbimtx.hpp>
39 #include <corelib/ncbiobj.hpp>
40 #include <corelib/ncbitime.hpp>
41 
42 #include <map>
43 #include <list>
44 
47 
48 //========================================================================
49 #if 0
51 {
52 public:
53  CTimer(void);
54  time_t Time(void);
55  void Start(void);
56  void Stop(void);
57  time_t RetryTime(void);
58  bool NeedCalibration(void);
59 private:
60  time_t m_ReasonableRefreshDelay;
61  int m_RequestsDevider;
62  int m_Requests;
63  CMutex m_RequestsLock;
64  time_t m_Time;
65  time_t m_LastCalibrated;
66 
67  time_t m_StartTime;
68  CMutex m_TimerLock;
69 };
70 #endif
71 
72 #if 0
73 class NCBI_XLOADER_GENBANK_EXPORT CRefresher
74 {
75 public:
76  CRefresher(void)
77  : m_RefreshTime(0)
78  {
79  }
80 
81  void Reset(CTimer &timer);
82  void Reset(void)
83  {
84  m_RefreshTime = 0;
85  }
86 
87  bool NeedRefresh(CTimer &timer) const;
88 
89 private:
90  time_t m_RefreshTime;
91 };
92 
93 
94 class NCBI_XLOADER_GENBANK_EXPORT CMutexPool
95 {
96 #if defined(NCBI_THREADS)
97  int m_size;
98  CMutex *m_Locks;
99  int *spread;
100 #else
101  static CMutex sm_Lock;
102 #endif
103 public:
104 #if defined(NCBI_THREADS)
105  CMutexPool(void);
106  ~CMutexPool(void);
107 
108  void SetSize(int size);
109 
110  CMutex& GetMutex(int x)
111  {
112  int y=x%m_size; spread[y]++; return m_Locks[y];
113  }
114 
115  int Select(unsigned key) const
116  {
117  return key % m_size;
118  }
119  template<class A> int Select(A *a) const
120  {
121  return Select((unsigned long)a/sizeof(A));
122  }
123 #else
124  CMutexPool(void)
125  {
126  }
127  ~CMutexPool(void)
128  {
129  }
130  void SetSize(int /* size */)
131  {
132  }
133  CMutex& GetMutex(int /* x */)
134  {
135  return sm_Lock;
136  }
137  int Select(unsigned /* key */) const
138  {
139  return 0;
140  }
141  template<class A> int Select(A *a) const
142  {
143  return 0;
144  }
145 #endif
146 };
147 
148 
149 class NCBI_XLOADER_GENBANK_EXPORT CGBLGuard
150 {
151 public:
152  enum EState
153  {
154  eNone,
155  eMain,
156  eBoth,
157  eLocal,
158  eNoneToMain,
159  eLast
160  };
161 
162  struct SLeveledMutex
163  {
164  unsigned m_SlowTraverseMode;
165  CMutex m_Lookup;
166  CMutexPool m_Pool;
167  };
168 
169  typedef SLeveledMutex TLMutex;
170 
171 private:
172  TLMutex *m_Locks;
173  const char *m_Loc;
174  EState m_orig;
175  EState m_current;
176  int m_select;
177 
178 public:
179 
180  CGBLGuard(TLMutex& lm,EState orig,const char *loc="",int select=-1); // just accept start position
181  CGBLGuard(TLMutex& lm,const char *loc=""); // assume orig=eNone, switch to e.Main in constructor
182  CGBLGuard(CGBLGuard &g,const char *loc); // inherit state from g1 - for SubGuards
183 
184  ~CGBLGuard();
185  template<class A> void Lock(A *a) { Switch(eBoth,a); }
186  template<class A> void Unlock(A *a) { Switch(eMain,a); }
187  void Lock(unsigned key) { Switch(eBoth,key); }
188  void Unlock(unsigned key) { Switch(eMain,key); }
189  void Lock() { Switch(eMain);};
190  void Unlock() { Switch(eNone);};
191  void Local() { Switch(eLocal);};
192 
193 private:
194 
195 #if defined (NCBI_THREADS)
196  void MLock();
197  void MUnlock();
198  void PLock();
199  void PUnlock();
200  void Select(int s);
201 
202  template<class A> void Switch(EState newstate,A *a)
203  {
204  Select(m_Locks->m_Pool.Select(a));
205  Switch(newstate);
206  }
207  void Switch(EState newstate,unsigned key)
208  {
209  Select(m_Locks->m_Pool.Select(key));
210  Switch(newstate);
211  }
212  void Switch(EState newstate);
213 #else
214  void Switch(EState /* newstate */) {}
215  template<class A> void Switch(EState newstate,A *a) {}
216  void Switch(EState /* newstate */, unsigned /* key */) {}
217 #endif
218 };
219 #endif
220 
221 //#define LOAD_INFO_MAP_CHECK 1
222 //#define LOAD_INFO_MAP_ALWAYS_CLEAR 1
223 //#define LOAD_INFO_MAP_NO_SPLICE 1
224 
225 template<class Key, class Info>
227 {
228 public:
229  enum {
231  };
232 
233  CLoadInfoMap(size_t max_size = 0)
234  : m_MaxSize(max_size? max_size: kDefaultMaxSize)
235  {
236  }
237 
238  size_t GetMaxSize(void) const
239  {
240  return m_MaxSize;
241  }
242 
243  void SetMaxSize(size_t max_size)
244  {
245  TWriteLockGuard guard(m_Lock);
246 #ifdef LOAD_INFO_MAP_CHECK
248  _ASSERT(m_Queue.size() == m_Index.size());
249 #endif
250  m_MaxSize = max_size? max_size: kDefaultMaxSize;
251  x_GC();
252 #ifdef LOAD_INFO_MAP_CHECK
254  _ASSERT(m_Queue.size() == m_Index.size());
255 #endif
256  }
257 
258  CRef<Info> Get(const Key& key)
259  {
260  TWriteLockGuard guard(m_Lock);
261 #ifdef LOAD_INFO_MAP_CHECK
263  _ASSERT(m_Queue.size() == m_Index.size());
264 #endif
265 #ifdef LOAD_INFO_MAP_ALWAYS_CLEAR
266  if ( !m_Queue.empty() && m_Queue.begin()->first != key ) {
267  m_Queue.clear();
268  m_Index.clear();
269  }
270  if ( m_Queue.empty() ) {
271  m_Queue.push_front(TQueueValue(key, Ref(new Info(key))));
272  m_Index.insert(TIndexValue(key, m_Queue.begin()));
273  }
274 #else
275  pair<TIndexIter, bool> ins =
276  m_Index.insert(TIndexValue(key, m_Queue.end()));
277  _ASSERT(ins.first->first == key);
278  if ( ins.second ) {
279  // new slot
280 #ifdef LOAD_INFO_MAP_CHECK
281  _ASSERT(m_Index.size() == m_Queue.size() + 1);
282 #endif
283  m_Queue.push_front(TQueueValue(key, Ref(new Info(key))));
284  x_GC();
285  }
286  else {
287  // old slot
288  _ASSERT(ins.first->second->first == key);
289  // move old slot to the back of queue
290 #ifdef LOAD_INFO_MAP_NO_SPLICE
291  CRef<Info> info = ins.first->second->second;
292  m_Queue.erase(ins.first->second);
293  m_Queue.push_front(TQueueValue(key, info));
294 #else
295  m_Queue.splice(m_Queue.begin(), m_Queue, ins.first->second);
296 #endif
297  }
298  // update for new position in queue
299  _ASSERT(m_Queue.begin()->first == key);
300  ins.first->second = m_Queue.begin();
301 #endif
302 #ifdef LOAD_INFO_MAP_CHECK
304  _ASSERT(m_Queue.size() == m_Index.size());
305 #endif
306  return m_Queue.begin()->second;
307  }
308 
309  void GC(void)
310  {
311  TWriteLockGuard guard(m_Lock);
312  x_GC();
313  }
314 
315  void Clear(void)
316  {
317  TWriteLockGuard guard(m_Lock);
318  m_Queue.clear();
319  m_Index.clear();
320  }
321 
322 protected:
323  typedef CFastMutex TLock;
326 
327  TLock m_Lock;
328 
329  void x_GC(void)
330  {
331  while ( m_Index.size() > m_MaxSize ) {
332  if ( !m_Queue.back().second->ReferencedOnlyOnce() ) {
333  break;
334  }
335  m_Index.erase(m_Queue.back().first);
336  m_Queue.pop_back();
337  }
338  }
339 
340 private:
341  typedef pair<Key, CRef<Info> > TQueueValue;
342  typedef list<TQueueValue> TQueue;
343  typedef typename TQueue::iterator TQueueIter;
345  typedef typename TIndex::value_type TIndexValue;
346  typedef typename TIndex::iterator TIndexIter;
347 
348  size_t m_MaxSize;
349  TQueue m_Queue;
350  TIndex m_Index;
351 
352 private:
353  // prevent copying
355  void operator=(const CLoadInfoMap<Key, Info>&);
356 };
357 
358 
361 
362 #endif
pair< Key, CRef< Info > > TQueueValue
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
size_type size() const
Definition: map.hpp:148
void x_GC(void)
TLock::TWriteLockGuard TWriteLockGuard
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:1906
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:73
const struct ncbi::grid::netcache::search::fields::KEY key
void clear()
Definition: map.hpp:169
void GC(void)
TLock::TReadLockGuard TReadLockGuard
CFastMutex –.
Definition: ncbimtx.hpp:666
CFastMutex TLock
CMutex –.
Definition: ncbimtx.hpp:748
Defines: CTimeFormat - storage class for time format.
Both preliminary and traceback stages.
Definition: blast_def.h:332
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
TIndex::iterator TIndexIter
TIndex::value_type TIndexValue
int g(Seg_Gsm *spe, Seq_Mtf *psm, Thd_Gsm *tdg)
Definition: thrddgri.c:44
size_t GetMaxSize(void) const
bool empty() const
Definition: map.hpp:149
#define A(i)
Definition: ecp_curves.c:882
int size
static MDB_envinfo info
Definition: mdb_load.c:37
static PyObject * Time(PyObject *self, PyObject *args)
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx...
TQueue::iterator TQueueIter
size_t m_MaxSize
CRef –.
Definition: ncbiobj.hpp:616
void erase(iterator pos)
Definition: map.hpp:167
Multi-threading – mutexes; rw-locks; semaphore.
CRef< Info > Get(const Key &key)
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1029
None specified.
Definition: blast_def.h:326
list< TQueueValue > TQueue
void operator=(const CLoadInfoMap< Key, Info > &)
void SetMaxSize(size_t max_size)
#define _ASSERT
#define NCBI_XLOADER_GENBANK_EXPORT
Definition: ncbi_export.h:1162
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:70
void Clear(void)
CLoadInfoMap(size_t max_size=0)
map< Key, TQueueIter > TIndex
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
Modified on Mon Apr 23 11:46:15 2018 by modify_doxy.py rev. 546573