NCBI C++ ToolKit
prefetch_manager.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: prefetch_manager.cpp 99890 2023-05-18 18:07:50Z vasilche $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * Prefetch implementation
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
37 #include <corelib/ncbithr.hpp>
39 
40 
43 
44 
46 {
47 }
48 
49 
51 {
52 }
53 
54 
56 {
57 }
58 
59 
61  : m_Impl(new CPrefetchManager_Impl(3, CThread::fRunDefault))
62 {
63 }
64 
65 
67  CThread::TRunMode threads_mode)
68  : m_Impl(new CPrefetchManager_Impl(max_threads, threads_mode))
69 {
70 }
71 
72 
74 {
75 }
76 
77 
79  IPrefetchAction* action,
80  IPrefetchListener* listener)
81 {
82  if ( !action ) {
83  NCBI_THROW(CObjMgrException, eOtherError,
84  "CPrefetchManager::AddAction: action is null");
85  }
86  return m_Impl->AddAction(priority, action, listener);
87 }
88 
89 
91  IPrefetchListener* listener)
92 {
93  return AddAction(0, action, listener);
94 }
95 
96 
98 {
101 }
102 
103 
105 {
107 }
108 
109 
110 const char* CPrefetchFailed::GetErrCodeString(void) const
111 {
112  switch ( GetErrCode() ) {
113  case eFailed: return "eFailed";
114  default: return CException::GetErrCodeString();
115  }
116 }
117 
118 
119 const char* CPrefetchCanceled::GetErrCodeString(void) const
120 {
121  switch ( GetErrCode() ) {
122  case eCanceled: return "eCanceled";
123  default: return CException::GetErrCodeString();
124  }
125 }
126 
127 
128 /////////////////////////////////////////////////////////////////////////////
129 // CPrefetchSequence
130 
131 
134  size_t active_size)
135  : m_Manager(&manager),
136  m_Source(source)
137 {
138  for ( size_t i = 0; i < active_size; ++i ) {
139  EnqueNextAction();
140  }
141 }
142 
143 
145 {
146  CMutexGuard guard(m_Mutex);
148  it->GetNCPointer()->RequestToCancel();
149  }
150 }
151 
152 
154 {
155  if ( !m_Source ) {
156  return;
157  }
158  CIRef<IPrefetchAction> action(m_Source->GetNextAction());
159  if ( !action ) {
160  m_Source.Reset();
161  return;
162  }
163  m_ActiveTokens.push_back(m_Manager->AddAction(action));
164 }
165 
166 
168 {
170  CMutexGuard guard(m_Mutex);
171  if ( !m_ActiveTokens.empty() ) {
172  EnqueNextAction();
173  ret = m_ActiveTokens.front();
174  m_ActiveTokens.pop_front();
175  }
176  return ret;
177 }
178 
179 
Base class for all object manager exceptions.
CRef –.
Definition: ncbiobj.hpp:618
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
virtual ~IPrefetchListener(void)
void CancelAllTasks(void)
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
CIRef< IPrefetchActionSource > m_Source
CPrefetchSequence(CPrefetchManager &manager, IPrefetchActionSource *source, size_t active_size=10)
CRef< CPrefetchRequest > GetNextToken(void)
Returns next action waiting for its result if necessary.
virtual ~IPrefetchActionSource(void)
CRef< CPrefetchManager > m_Manager
CRef< CPrefetchRequest > AddAction(TPriority priority, IPrefetchAction *action, IPrefetchListener *listener)
CRef< CPrefetchManager_Impl > m_Impl
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
list< CRef< CPrefetchRequest > > m_ActiveTokens
CRef< CPrefetchRequest > AddAction(TPriority priority, IPrefetchAction *action, IPrefetchListener *listener=0)
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#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
void Abort(const CTimeSpan *timeout=NULL)
Abort all functions of the pool – cancel all queued tasks, send cancellation notifications to all cur...
void CancelTasks(TExclusiveFlags tasks_group)
Cancel the selected groups of tasks in the pool.
@ fCancelQueuedTasks
Cancel all tasks waiting in the queue and not yet executing.
@ fCancelExecutingTasks
Cancel all currently executing tasks.
int TRunMode
Bitwise OR'd flags for thread creation passed to Run().
Definition: ncbithr.hpp:558
int i
const CharType(& source)[N]
Definition: pointer.h:1149
Static variables safety - create on demand, destroy on application termination.
Multi-threading – classes, functions, and features.
Modified on Wed Nov 29 02:21:42 2023 by modify_doxy.py rev. 669887