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 52320 2011-12-16 14:16:32Z 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 * 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 
88  virtual CIRef<IPrefetchAction> GetNextAction(void) = 0;
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:
132  void operator=(const CPrefetchManager&);
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;
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;
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
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:73
This exception is used to interrupt actions canceled by user.
CRef< CPrefetchManager_Impl > m_Impl
#define NCBI_EXCEPTION_DEFAULT(exception_class, base_class)
To help declare new exception class.
Definition: ncbiexpt.hpp:1159
#define NCBI_XOBJMGR_EXPORT
Definition: ncbi_export.h:1290
CRef< CObjectFor< CMutex > > m_StateMutex
IPrefetchAction * GetAction(void) const
CRef< CPrefetchManager > m_Manager
CMutex –.
Definition: ncbimtx.hpp:733
TProgress GetProgress(void) const
virtual EStatus Execute(void)=0
Do the actual job.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
IPrefetchListener * GetListener(void) const
int TRunMode
Bitwise OR'd flags for thread creation passed to Run().
Definition: ncbithr.hpp:533
Pool of generic task-executing threads.
const CharType(& source)[N]
Definition: pointer.h:1107
EStatus
CIRef< IPrefetchAction > m_Action
Default mode.
Definition: ncbithr.hpp:515
This exception is used to report failed actions.
virtual void OnStatusChange(EStatus old)
Callback to notify on changes in the task status.
Multi-threading – classes, functions, and features.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx...
Abstract class for representing single task executing in pool of threads To use this class in applica...
Definition: thread_pool.hpp:75
CScope –.
Definition: scope.hpp:90
CRef –.
Definition: ncbiobj.hpp:616
CObjectFor –.
Definition: ncbiobj.hpp:2023
CObject –.
Definition: ncbiobj.hpp:180
bool IsDone(void) const
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:70
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:728
bool IsFinished(void) const
Check if task execution has been already finished (successfully or not)
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:483
CIRef< IPrefetchActionSource > m_Source
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
CIRef< IPrefetchListener > m_Listener
list< CRef< CPrefetchRequest > > m_ActiveTokens
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:431
Modified on Sun Nov 19 16:43:46 2017 by modify_doxy.py rev. 546573