KSquare Utilities
KKB::KKThreadManager Class Reference

#include <KKThreadManager.h>

+ Inheritance diagram for KKB::KKThreadManager:

Public Member Functions

 KKThreadManager (const KKStr &_managerName, kkuint32 _maxNumThreads, MsgQueuePtr _msgQueue)
 
 ~KKThreadManager ()
 
void AddThread (KKThreadPtr _thread)
 
bool AllThreadsTerminated ()
 
bool AnyProcessorsCrashed ()
 
const bool & Crashed () const
 
const bool & DoneExecuting () const
 
void KillAllRunningThreads ()
 
void ManageThreads (bool &successful)
 
void RequestShutdown ()
 
void RequestTerminate ()
 
virtual void Run ()
 
const bool & ShutdownFlag () const
 
void ShutdownProcessing (kkint32 miliSecsToWait)
 Shutdown all threads in a orderly way; observing prerequisite ordering. More...
 
void TerminateProcessing ()
 
- Public Member Functions inherited from KKB::KKThread
 KKThread (const KKStr &_threadName, KKThreadManagerPtr _threadManager, MsgQueuePtr _msgQueue)
 
virtual ~KKThread ()
 
void AddShutdownPrerequistite (KKThreadPtr _thread)
 Specify threads that must stop before this thread is started. More...
 
void AddStartPrerequistite (KKThreadPtr _thread)
 Specify threads that must start before this thread is started. More...
 
VolConstBoolCancelFlag () const
 
bool Crashed () const
 
void Crashed (bool _crashed)
 
const KKStrExceptionText () const
 
void ExceptionText (const KKStr &_exceptionText)
 
KKStrListPtr GetMsgs ()
 
void Kill ()
 stops the running thread and frees the thread handle More...
 
kkint32 MemoryConsumedEstimated ()
 
KKB::MsgQueuePtr MsgQueue ()
 
bool OkToShutdown () const
 
bool OkToStart () const
 
VolConstBoolShutdownFlag () const
 
void ShutdownThread ()
 
void Start (ThreadPriority _priority, bool &successful)
 
ThreadStatus Status () const
 
void Status (ThreadStatus _status)
 
const KKStrStatusStr () const
 
VolConstBoolTerminateFlag () const
 
virtual void TerminateFlagChanged ()
 Will be called whenever the value of 'terminateFlag' is changed; derived classes should override this method if they need to be aware that the terminaeFlag has changed. This give them a chance to let other objects/methods know that the flag has changed. More...
 
void TerminateThread ()
 
kkint32 ThreadId () const
 
const KKStrThreadName () const
 
bool ThreadStillProcessing () const
 
void WaitForThreadToStop (kkuint32 maxTimeToWait)
 Called by separate thread; will stay in loop until the thread controlled by this instance shutdown or it waited the 'maxTimeToWait'. More...
 

Additional Inherited Members

- Public Types inherited from KKB::KKThread
typedef KKThreadKKThreadPtr
 
enum  ThreadPriority : int { ThreadPriority::Null, ThreadPriority::Low, ThreadPriority::Normal, ThreadPriority::High }
 
enum  ThreadStatus : int {
  ThreadStatus::Null, ThreadStatus::NotStarted, ThreadStatus::Starting, ThreadStatus::Running,
  ThreadStatus::Stopping, ThreadStatus::Stopped
}
 
- Static Public Member Functions inherited from KKB::KKThread
static const KKStrThreadStatusToStr (ThreadStatus)
 
- Protected Member Functions inherited from KKB::KKThread
void AddMsg (KKStrPtr msg)
 
void AddMsg (const KKStr &msg)
 
bool ShutdownOrTerminateRequested ()
 
bool ThereIsACircularReferenceShutdown (KKThreadPtr _thread) const
 
bool ThereIsACircularReferenceStart (KKThreadPtr _thread) const
 

Detailed Description

There are three ways to stop Thread processing with the first being the most graceful and the last being the most abrupt.
1) 'ShutdownProcessing' The preferred method; each thread is flagged for 'Shutdown' after all its prerequisite shutdown threads
have shutdown.
2) 'TerminateProcessing' Similar to 'ShutdownProcessing' except the prerequisites are not checked; each thread is immediately flagged
to Terminate which indicates to the thread that it is to cease all processing and exit while releasing all resources.
3) 'KillAllRunningThreads' A last resort; you would call this if for some reason threads are not terminating on there own.

Definition at line 37 of file KKThreadManager.h.

Constructor & Destructor Documentation

KKThreadManager::KKThreadManager ( const KKStr _managerName,
kkuint32  _maxNumThreads,
MsgQueuePtr  _msgQueue 
)

Definition at line 23 of file KKThreadManager.cpp.

References KKB::KKThread::KKThread(), and KKB::KKThreadList::KKThreadList().

26  :
27  KKThread (_managerName, NULL, _msgQueue),
28  crashed (false),
29  doneExecuting (false),
30  maxNumThreads (_maxNumThreads),
31  shutdownFlag (false),
32  shutdownRequested (false),
33  terminateFlag (false),
34  terminateRequested (false),
35  threads (NULL)
36 
37 {
38  threads = new KKThreadList (true);
39 }
KKThread(const KKStr &_threadName, KKThreadManagerPtr _threadManager, MsgQueuePtr _msgQueue)
Definition: KKThread.cpp:83
KKThreadManager::~KKThreadManager ( )

Definition at line 44 of file KKThreadManager.cpp.

45 {
46  delete threads;
47  threads = NULL;
48 }

Member Function Documentation

void KKThreadManager::AddThread ( KKThreadPtr  _thread)

Add thread that is to be managed; will take ownership

Definition at line 52 of file KKThreadManager.cpp.

References KKB::KKThreadList::KKThreadList().

53 {
54  if (!threads)
55  threads = new KKThreadList (true);
56 
57  threads->PushOnBack (_thread);
58 }
virtual void PushOnBack(EntryPtr _entry)
Definition: KKQueue.h:398
bool KKThreadManager::AllThreadsTerminated ( )

Definition at line 280 of file KKThreadManager.cpp.

References KKB::KKThread::Status(), and KKB::KKThread::Stopped.

281 {
282  bool allThreadsTerminated = true;
283 
284  if (threads)
285  {
287  for (idx = threads->begin (); idx != threads->end (); ++idx)
288  {
289  KKThreadPtr t = *idx;
291  {
292  allThreadsTerminated = false;
293  break;
294  }
295  }
296  }
297 
298  return allThreadsTerminated;
299 } /* AllThreadsTerminated */
ThreadStatus Status() const
Definition: KKThread.h:72
std::vector< KKThread * >::const_iterator const_iterator
Definition: KKQueue.h:89
The base class to be used any thread.
Definition: KKThread.h:27
bool KKThreadManager::AnyProcessorsCrashed ( )

Definition at line 255 of file KKThreadManager.cpp.

References KKB::KKThread::Crashed().

Referenced by ManageThreads().

256 {
257  bool anyProcessorsCrashed = false;
258 
259  if (threads)
260  {
262  for (idx = threads->begin (); idx != threads->end (); ++idx)
263  {
264  KKThreadPtr t = *idx;
265  if (t->Crashed ())
266  {
267  anyProcessorsCrashed = true;
268  break;
269  }
270  }
271  }
272 
273  crashed = anyProcessorsCrashed;
274 
275  return anyProcessorsCrashed;
276 } /* AnyProcessorsCrash */
std::vector< KKThread * >::iterator iterator
Definition: KKQueue.h:88
bool Crashed() const
Definition: KKThread.h:62
The base class to be used any thread.
Definition: KKThread.h:27
const bool& KKB::KKThreadManager::Crashed ( ) const
inline

Definition at line 48 of file KKThreadManager.h.

48 {return crashed;}
const bool& KKB::KKThreadManager::DoneExecuting ( ) const
inline

Definition at line 49 of file KKThreadManager.h.

49 {return doneExecuting;}
void KKThreadManager::KillAllRunningThreads ( )

Will STOP ALL running threads; NOT Grace-full;

Definition at line 185 of file KKThreadManager.cpp.

References KKB::KKThread::Kill().

Referenced by ShutdownProcessing().

186 {
187  if (!threads)
188  return;
189 
191  for (idx = threads->begin (); idx != threads->end (); ++idx)
192  {
193  KKThreadPtr thread = *idx;
194  thread->Kill ();
195  }
196 } /* KillAllRunningThreads*/
std::vector< KKThread * >::iterator iterator
Definition: KKQueue.h:88
The base class to be used any thread.
Definition: KKThread.h:27
void Kill()
stops the running thread and frees the thread handle
Definition: KKThread.cpp:427
void KKThreadManager::ManageThreads ( bool &  successful)

Definition at line 305 of file KKThreadManager.cpp.

References AnyProcessorsCrashed(), KKB::osSleep(), ShutdownProcessing(), and TerminateProcessing().

306 {
307  successful= false;
308  if (!threads)
309  return;
310 
311  StartThreads (successful);
312  if (!successful)
313  return;
314 
315 
316  while (true)
317  {
318  if (shutdownRequested)
319  {
320  shutdownRequested = false;
321  ShutdownProcessing (0);
322  break;
323  }
324 
325  else if (terminateRequested)
326  {
327  terminateRequested = false;
329  break;
330  }
331 
332  else
333  {
334  osSleep (0.05f);
335  }
336  }
337 
338 
339  if (AnyProcessorsCrashed ())
340  {
341  AddMsg ("One or more processors crashed!!!");
342  crashed = true;
343  }
344 } /* ManageThreads */
void ShutdownProcessing(kkint32 miliSecsToWait)
Shutdown all threads in a orderly way; observing prerequisite ordering.
void osSleep(float numOfSecs)
void AddMsg(KKStrPtr msg)
Definition: KKThread.cpp:154
void KKThreadManager::RequestShutdown ( )

Definition at line 62 of file KKThreadManager.cpp.

63 {
64  shutdownRequested = true;
65 }
void KKThreadManager::RequestTerminate ( )

Definition at line 68 of file KKThreadManager.cpp.

69 {
70  terminateRequested = true;
71 }
void KKThreadManager::Run ( )
virtual

Reimplemented from KKB::KKThread.

Definition at line 348 of file KKThreadManager.cpp.

349 {
350 }
const bool& KKB::KKThreadManager::ShutdownFlag ( ) const
inline

Definition at line 47 of file KKThreadManager.h.

47 {return shutdownFlag;}
void KKThreadManager::ShutdownProcessing ( kkint32  miliSecsToWait)

Shutdown all threads in a orderly way; observing prerequisite ordering.

Definition at line 93 of file KKThreadManager.cpp.

References KillAllRunningThreads(), KKB::KKThread::NotStarted, KKB::KKThread::Null, KKB::KKThread::OkToShutdown(), KKB::osSleep(), KKB::KKThread::Running, KKB::KKThread::ShutdownFlag(), KKB::KKThread::ShutdownThread(), KKB::KKThread::Starting, KKB::KKThread::Status(), KKB::KKThread::Stopped, and KKB::KKThread::Stopping.

Referenced by ManageThreads().

94 {
95  shutdownFlag = true;
96 
97  if (!threads)
98  return;
99 
100  kkint32 numMiliSecsWaited = 0;
101  shutdownFlag = true;
102 
103  kkint32 lastNumThreadsRunning = 0;
104  kkint32 lastNumThreadsStopped = 0;
105 
106  bool allTreadsAreShutdown = false;
107 
108  while (!allTreadsAreShutdown)
109  {
110  allTreadsAreShutdown = true;
111  kkint32 numThreadsRunning = 0;
112  kkint32 numThreadsStopped = 0;
113 
115  for (idx = threads->begin (); idx != threads->end (); ++idx)
116  {
117  KKThreadPtr thread = *idx;
118 
119  switch (thread->Status ())
120  {
122  break;
123 
126  // Nothing to do; this thread is not running,
127  ++numThreadsStopped;
128  break;
129 
131  allTreadsAreShutdown = false;
132  ++numThreadsRunning;
133  break;
134 
136  {
137  allTreadsAreShutdown = false;
138  if (!(thread->ShutdownFlag ()))
139  {
140  if (thread->OkToShutdown ())
141  {
142  thread->ShutdownThread ();
143  numMiliSecsWaited = 0;
144  }
145  }
146  ++numThreadsRunning;
147  }
148  break;
149 
151  // Have to let it finish starting before we can flag it to shutdown.
152  ++numThreadsRunning;
153  break;
154  } /* End of Switch */
155  }
156 
157  if ((numThreadsRunning != lastNumThreadsRunning) ||
158  (numThreadsStopped != lastNumThreadsStopped)
159  )
160  {
161  // The status of one or more threads changed; can reset the 'numMiliSecsWaited' clock.
162  numMiliSecsWaited = 0;
163  }
164 
165  if (!allTreadsAreShutdown)
166  {
167  if ((miliSecsToWait > 0) && (numMiliSecsWaited > miliSecsToWait))
168  {
169  // The shutdown process is taking longer than the 'miliSecsToWait' parameter will allow for; we need to terminate
170  // all running threads now.
172  break;
173  }
174  else
175  {
176  osSleep (0.01f);
177  numMiliSecsWaited += 10;
178  }
179  }
180  }
181 } /* ShutdownProcessing */
ThreadStatus Status() const
Definition: KKThread.h:72
std::vector< KKThread * >::iterator iterator
Definition: KKQueue.h:88
__int32 kkint32
Definition: KKBaseTypes.h:88
void osSleep(float numOfSecs)
The base class to be used any thread.
Definition: KKThread.h:27
bool OkToShutdown() const
Definition: KKThread.cpp:519
VolConstBool & ShutdownFlag() const
Definition: KKThread.h:75
void ShutdownThread()
Definition: KKThread.cpp:231
void KKThreadManager::TerminateProcessing ( )

Flag all All running threads to Terminate ASAP and release all allocated resources.

Definition at line 76 of file KKThreadManager.cpp.

References KKB::KKThread::TerminateThread().

Referenced by ManageThreads().

77 {
78  terminateFlag = true;
79  if (threads)
80  {
82  for (idx = threads->begin (); idx != threads->end (); ++idx)
83  {
84  KKThreadPtr thread = *idx;
85  thread->TerminateThread ();
86  }
87  }
88 } /* TerminateProcessing */
std::vector< KKThread * >::iterator iterator
Definition: KKQueue.h:88
void TerminateThread()
Definition: KKThread.cpp:208
The base class to be used any thread.
Definition: KKThread.h:27

The documentation for this class was generated from the following files: