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

Go to the SVN repository for this file.

1 #ifndef PREFETCH_MANAGER__HPP
2 #define PREFETCH_MANAGER__HPP
3 
4 /* $Id: prefetch_manager.hpp 84612 2018-11-21 14:24:48Z ucko $
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 * Prefetch manager
33 *
34 */
35 
36 #include <corelib/ncbistd.hpp>
37 #include <corelib/ncbiobj.hpp>
38 #include <corelib/ncbithr.hpp>
39 #include <util/thread_pool.hpp>
40 
43 
44 class CScope;
45 
46 /** @addtogroup ObjectManagerCore
47  *
48  * @{
49  */
50 
51 
52 class CPrefetchRequest;
53 class CPrefetchManager;
55 
57 {
58  enum EState {
59  eInvalid, // no prefetch token available
60  eQueued, // placed in queue
61  eStarted, // moved from queue to processing
62  eAdvanced, // got new data while processing
63  eCompleted, // finished processing successfully
64  eCanceled, // canceled by user request
65  eFailed // finished processing unsuccessfully
66  };
67  typedef EState EEvent;
68 
69  typedef int TPriority;
70  typedef int TProgress;
71 };
72 
73 
75 {
76 public:
77  virtual ~IPrefetchAction(void);
78 
79  virtual bool Execute(CRef<CPrefetchRequest> token) = 0;
80 };
81 
82 
84 {
85 public:
86  virtual ~IPrefetchActionSource(void);
87 
89 };
90 
91 
93 {
94 public:
95  virtual ~IPrefetchListener(void);
96 
97  virtual void PrefetchNotify(CRef<CPrefetchRequest> token, EEvent event) = 0;
98 };
99 
100 
102  public CObject,
103  public SPrefetchTypes
104 {
105 public:
106  CPrefetchManager(void);
107  explicit CPrefetchManager(unsigned max_threads,
108  CThread::TRunMode threads_mode = CThread::fRunDefault);
109  ~CPrefetchManager(void);
110 
111  CRef<CPrefetchRequest> AddAction(TPriority priority,
112  IPrefetchAction* action,
113  IPrefetchListener* listener = 0);
114  CRef<CPrefetchRequest> AddAction(IPrefetchAction* action,
115  IPrefetchListener* listener = 0);
116 
117  // Checks if prefetch is active in current thread.
118  // Throws CPrefetchCanceled exception if the current token is canceled.
119  static bool IsActive(void);
120 
121  // Send cancel requests to all tasks, queued and executing
122  void CancelAllTasks(void);
123 
124  // Clears manager queue and stops all worker threads.
125  void Shutdown(void);
126 
127 private:
129 
130 private:
133 };
134 
135 
136 /// This exception is used to report failed actions
138 {
139 public:
140  enum EErrCode {
141  eFailed
142  };
143  virtual const char* GetErrCodeString(void) const override;
145 };
146 
147 
148 /// This exception is used to interrupt actions canceled by user
150 {
151 public:
152  enum EErrCode {
153  eCanceled
154  };
155  virtual const char* GetErrCodeString(void) const override;
157 };
158 
159 
161 {
162 public:
165  size_t active_size = 10);
166  ~CPrefetchSequence(void);
167 
168  /// Returns next action waiting for its result if necessary
169  CRef<CPrefetchRequest> GetNextToken(void);
170 
171 protected:
172  void EnqueNextAction(void);
173 
174 private:
178  list< CRef<CPrefetchRequest> > m_ActiveTokens;
179 };
180 
181 
183  : public CThreadPool_Task,
184  public SPrefetchTypes
185 {
186 public:
188  IPrefetchAction* action,
189  IPrefetchListener* listener,
190  unsigned int priority);
191  ~CPrefetchRequest(void);
192 
194  {
195  return m_Action.GetNCPointer();
196  }
197 
199  {
200  return m_Listener.GetNCPointerOrNull();
201  }
202  void SetListener(IPrefetchListener* listener);
203 
204  EState GetState(void) const;
205 
206  // in one of final states: completed, failed, canceled
207  bool IsDone(void) const
208  {
209  return IsFinished();
210  }
211 
212  TProgress GetProgress(void) const
213  {
214  return m_Progress;
215  }
216  TProgress SetProgress(TProgress progress);
217 
218  virtual EStatus Execute(void);
219 
220  virtual void OnStatusChange(EStatus /* old */);
221 
222 private:
223  friend class CPrefetchManager;
224  friend class CPrefetchManager_Impl;
225 
226  // back references
228 
232 };
233 
234 
235 /* @} */
236 
237 
240 
241 #endif // PREFETCH_MANAGER__HPP
EStatus
CMutex –.
Definition: ncbimtx.hpp:749
CObjectFor –.
Definition: ncbiobj.hpp:2335
CObject –.
Definition: ncbiobj.hpp:180
This exception is used to interrupt actions canceled by user.
This exception is used to report failed actions.
CRef –.
Definition: ncbiobj.hpp:618
CScope –.
Definition: scope.hpp:92
Abstract class for representing single task executing in pool of threads To use this class in applica...
Definition: thread_pool.hpp:76
Include a standard set of the NCBI C++ Toolkit most basic headers.
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
TProgress GetProgress(void) const
virtual CIRef< IPrefetchAction > GetNextAction(void)=0
CIRef< IPrefetchActionSource > m_Source
CIRef< IPrefetchAction > m_Action
virtual bool Execute(CRef< CPrefetchRequest > token)=0
CRef< CObjectFor< CMutex > > m_StateMutex
CIRef< IPrefetchListener > m_Listener
IPrefetchAction * GetAction(void) const
IPrefetchListener * GetListener(void) const
CRef< CPrefetchManager > m_Manager
NCBI_EXCEPTION_DEFAULT(CPrefetchFailed, CException)
NCBI_EXCEPTION_DEFAULT(CPrefetchCanceled, CException)
bool IsDone(void) const
virtual void PrefetchNotify(CRef< CPrefetchRequest > token, EEvent event)=0
void operator=(const CPrefetchManager &)
CPrefetchManager(const CPrefetchManager &)
CRef< CPrefetchManager_Impl > m_Impl
list< CRef< CPrefetchRequest > > m_ActiveTokens
#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
int TRunMode
Bitwise OR'd flags for thread creation passed to Run().
Definition: ncbithr.hpp:558
@ fRunDefault
Default mode.
Definition: ncbithr.hpp:540
#define NCBI_XOBJMGR_EXPORT
Definition: ncbi_export.h:1307
string Execute(const string &cmmd, const vector< string > &args, const string &data=kEmptyStr)
const CharType(& source)[N]
Definition: pointer.h:1149
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Multi-threading – classes, functions, and features.
@ eCanceled
Request canceled.
Pool of generic task-executing threads.
Modified on Tue Apr 23 07:39:57 2024 by modify_doxy.py rev. 669887