KSquare Utilities
KKThread.h
Go to the documentation of this file.
1 /* KKThread.cpp -- Manages the threads that perform the image extraction process.
2  * Copyright (C) 2012-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_KKTHREAD_)
6 #define _KKTHREAD_
7 
8 #include "MsgQueue.h"
9 
10 
11 
12 namespace KKB
13 {
14  #if !defined(_KKTHREADMANAGER_)
17  #endif
18 
19  class KKThreadList;
21 
22 
23  /**
24  *@brief The base class to be used any thread.
25  *@details This will be the base class for each one of the different types of processing.
26  */
27  class KKThread
28  {
29  public:
31 
32  enum class ThreadPriority: int
33  {Null,
34  Low,
35  Normal,
36  High
37  };
38 
39  enum class ThreadStatus: int
40  {Null,
41  NotStarted,
42  Starting,
43  Running,
44  Stopping,
45  Stopped
46  };
47 
48 
49  KKThread (const KKStr& _threadName,
50  KKThreadManagerPtr _threadManager,
51  MsgQueuePtr _msgQueue
52  );
53 
54  virtual ~KKThread ();
55 
57 
58  static const KKStr& ThreadStatusToStr (ThreadStatus);
59 
60  VolConstBool& CancelFlag () const {return terminateFlag;} /**< Another name for 'TerminateFlag'. */
61 
62  bool Crashed () const {return crashed;} /**< Signifies that this thread had to terminate on its own because of an abnormal
63  * situation; such as a memory corruption.
64  */
65 
66  const KKStr& ExceptionText () const {return exceptionText;} /**< If the overloaded 'Run' method of a derived class generates a exception that
67  * is not caught; then the ''ThreadStartCallBack' will catch it and store the related
68  * text in this field. In this case the 'Crashed()' method will return 'true'.
69  */
70 
71  KKB::MsgQueuePtr MsgQueue () {return msgQueue;}
72  ThreadStatus Status () const {return status;}
73  const KKStr& StatusStr () const {return ThreadStatusToStr (status);}
74 
75  VolConstBool& ShutdownFlag () const {return shutdownFlag;} /**< Indicates that the application wants this thread to complete what work is queued up
76  * for it top process. Threads need to monitor this flag; if it goes true they are to
77  * complete all processing that is queued up and then shutdown. This is
78  */
79 
80  VolConstBool& TerminateFlag () const {return terminateFlag;} /**< Indicates that thread is to stop processing ASAP, release any resources
81  * it holds and terminate. Threads need to monitor this flag; if it goes 'true'
82  * they need to terminate ASAP releasing all allocated resources. Typically set
83  * to true when user request to cancel processing.
84  */
85 
86  kkint32 ThreadId () const {return threadId;}
87  const KKStr& ThreadName () const {return threadName;}
88 
89  void Crashed (bool _crashed) {crashed = _crashed;}
90  void ExceptionText (const KKStr& _exceptionText) {exceptionText = _exceptionText;}
91  void Status (ThreadStatus _status) {status = _status;}
92 
93  KKStrListPtr GetMsgs ();
94 
95  virtual void Run ();
96 
97  /**
98  *@brief Specify threads that must start before this thread is started.
99  *@details This method can be called multiple times; the information is used by the 'KKTHreadManager' instance to control the
100  * start of the thread.
101  *@param[in] _thread A thread that needs to be in the 'Running' status before this thread can start; we do NOT take ownership.
102  */
103  void AddStartPrerequistite (KKThreadPtr _thread);
104 
105  /**
106  *@brief Specify threads that must stop before this thread is started.
107  *@details This method can be called multiple times; the information is used by the 'KKTHreadManager' instance to control the
108  * orderly shutdown of the controlling 'KKTHreadManager' instance.
109  *@param[in] _thread A thread that needs to be in the 'Stopped' status before this thread can be shutdown; we do NOT take ownership.
110  */
111  void AddShutdownPrerequistite (KKThreadPtr _thread);
112 
113  void Kill ();
114 
115  bool OkToShutdown () const; /**< Returns 'true' if all shutdown prerequisites are in the 'Stopped' status. */
116 
117  bool OkToStart () const; /**< Returns 'true' if all start prerequisites are in the 'Running' status. */
118 
119  bool ThreadStillProcessing () const;
120 
121  void Start (ThreadPriority _priority,
122  bool& successful
123  ); /**< Call this method to start thread; it will call the method "Run". */
124 
125  void TerminateThread (); /**< Call this method to have its thread stop right away and exit. */
126 
127  /**
128  *@brief Will be called whenever the value of 'terminateFlag' is changed; derived classes should override this method if
129  * they need to be aware that the terminaeFlag has changed. This give them a chance to let other objects/methods know that
130  * the flag has changed.
131  */
132  virtual void TerminateFlagChanged ();
133 
134  void ShutdownThread (); /**< Call this method to have its thread finish what ever is in the queue and then exit. */
135 
136  /**
137  *@brief Called by separate thread; will stay in loop until the thread controlled by this instance shutdown or it waited the 'maxTimeToWait'.
138  *@details Does not call the 'ShutdownThread' or 'TerminateThread' methods.
139  *@param[in] maxTimeToWait Will wait 'maxTimeToWait' seconds before giving up on waiting at which point will try to kill thread.
140  */
141  void WaitForThreadToStop (kkuint32 maxTimeToWait);
142 
143 
144  protected:
145  void AddMsg (KKStrPtr msg); /**< Taking ownership of 'msg' and will append to 'msgQueue'. */
146  void AddMsg (const KKStr& msg); /**< A copy of the message 'msg' will be added to the end of msgQueue. */
147 
148  bool ShutdownOrTerminateRequested () {return (terminateFlag || shutdownFlag);}
149 
150  bool ThereIsACircularReferenceStart (KKThreadPtr _thread) const;
151 
152  bool ThereIsACircularReferenceShutdown (KKThreadPtr _thread) const;
153 
154  private:
155  static KKStr threadStatusDescs[];
156 
157  void SetThreadName ();
158 
159 
160  bool crashed; /**< Signifies that this thread had to terminate on its own because
161  * of an abnormal situation.
162  */
163 
164  KKStr exceptionText; /**< If the 'ThreadStartCallBack' method catches a exception; the text of the exception
165  * will be stored hear.
166  */
167 
168  MsgQueuePtr msgQueue; /**< This MsgQueue instance will be owned by 'ExtractionManager' we will just use
169  * it to communicate messages to the controlling process.
170  */
171 
172  volatile bool shutdownFlag; /**< Indicates that the application wants this thread to complete what work is queued up
173  * for it top process. Threads need to monitor this flag; if it goes true they are to
174  * complete all processing that is queued up and then shutdown. This is
175  */
176 
177  KKThreadListPtr shutdownPrerequisites;
178 
179  KKThreadListPtr startPrerequisites;
180 
181  ThreadStatus status;
182 
183  ThreadPriority priority;
184 
185  volatile bool terminateFlag; /**< Indicates that thread is to stop processing ASAP, release any resources
186  * it holds and terminate. Threads need to monitor this flag; if it goes 'true'
187  * they need to terminate ASAP releasing all allocated resources. Typically set
188  * to true when user request to cancel processing.
189  */
190 
191  kkint32 threadId;
192 
193  KKThreadManagerPtr threadManager;
194 
195  KKStr threadName;
196 
197 #if defined(WIN32)
198  // Data structures and methods specific to Windows thread management.
199  unsigned long* windowsThreadHandle;
200  DWORD windowsThreadId;
201 #else
203 #endif
204 
205  }; /* KKThread */
206 
207 
208 
209  typedef KKThread::KKThreadPtr KKThreadPtr;
210 
212  {
213  public:
214  KKThreadList (bool _owner = true);
215  KKThreadList (const KKThreadList& list);
216  ~KKThreadList ();
217 
219  };
220 
221  typedef KKThreadList* KKThreadListPtr;
222 
223 #if defined(WIN32)
224  // global function called by the thread object.
225  // this in turn calls the overridden run()
226  extern "C"
227  {
228  unsigned int ThreadStartCallBack(void* param);
229  }
230 #endif
231 
232 
233 } /* ImageExtractionManager*/
234 
235 #endif
const KKStr & StatusStr() const
Definition: KKThread.h:73
void Start(ThreadPriority _priority, bool &successful)
Definition: KKThread.cpp:352
bool ThreadStillProcessing() const
Definition: KKThread.cpp:221
KKThreadList(const KKThreadList &list)
Definition: KKThread.cpp:562
ThreadStatus Status() const
Definition: KKThread.h:72
__int32 kkint32
Definition: KKBaseTypes.h:88
KKThread * KKThreadPtr
Definition: KKThread.h:30
bool Crashed() const
Definition: KKThread.h:62
VolConstBool & TerminateFlag() const
Definition: KKThread.h:80
KKThread(const KKStr &_threadName, KKThreadManagerPtr _threadManager, MsgQueuePtr _msgQueue)
Definition: KKThread.cpp:83
const KKStr & ThreadName() const
Definition: KKThread.h:87
virtual ~KKThread()
Definition: KKThread.cpp:109
bool ThereIsACircularReferenceShutdown(KKThreadPtr _thread) const
Definition: KKThread.cpp:480
void AddStartPrerequistite(KKThreadPtr _thread)
Specify threads that must start before this thread is started.
Definition: KKThread.cpp:501
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
kkint32 MemoryConsumedEstimated() const
Definition: KKThread.cpp:575
virtual void TerminateFlagChanged()
Will be called whenever the value of &#39;terminateFlag&#39; is changed; derived classes should override this...
Definition: KKThread.cpp:216
KKTHread * KKTHreadPtr
KKThreadList * KKThreadListPtr
Definition: KKThread.h:19
void TerminateThread()
Definition: KKThread.cpp:208
VolConstBool & CancelFlag() const
Definition: KKThread.h:60
KKThreadManager * KKThreadManagerPtr
Definition: KKThread.h:15
void ExceptionText(const KKStr &_exceptionText)
Definition: KKThread.h:90
KKStrListPtr GetMsgs()
Definition: KKThread.cpp:193
The base class to be used any thread.
Definition: KKThread.h:27
bool OkToShutdown() const
Definition: KKThread.cpp:519
bool ShutdownOrTerminateRequested()
Definition: KKThread.h:148
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void AddShutdownPrerequistite(KKThreadPtr _thread)
Specify threads that must stop before this thread is started.
Definition: KKThread.cpp:510
const KKStr & ExceptionText() const
Definition: KKThread.h:66
kkint32 ThreadId() const
Definition: KKThread.h:86
bool ThereIsACircularReferenceStart(KKThreadPtr _thread) const
Definition: KKThread.cpp:459
void Status(ThreadStatus _status)
Definition: KKThread.h:91
kkint32 MemoryConsumedEstimated()
Definition: KKThread.cpp:117
void WaitForThreadToStop(kkuint32 maxTimeToWait)
Called by separate thread; will stay in loop until the thread controlled by this instance shutdown or...
Definition: KKThread.cpp:238
KKB::MsgQueuePtr MsgQueue()
Definition: KKThread.h:71
KKThreadList(bool _owner=true)
Definition: KKThread.cpp:555
VolConstBool & ShutdownFlag() const
Definition: KKThread.h:75
void Kill()
stops the running thread and frees the thread handle
Definition: KKThread.cpp:427
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
void ShutdownThread()
Definition: KKThread.cpp:231
void AddMsg(KKStrPtr msg)
Definition: KKThread.cpp:154
unsigned int ThreadStartCallBack(void *param)
Definition: KKThread.cpp:280
static const KKStr & ThreadStatusToStr(ThreadStatus)
Definition: KKThread.cpp:144
void Crashed(bool _crashed)
Definition: KKThread.h:89
virtual void Run()
Definition: KKThread.cpp:447
bool OkToStart() const
Definition: KKThread.cpp:536
void AddMsg(const KKStr &msg)
Definition: KKThread.cpp:174
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163