35 #define MS_VC_EXCEPTION 0x406D1388
57 info.szName = threadName.Str ();
58 info.dwThreadID = windowsThreadId;
64 RaiseException(
MS_VC_EXCEPTION, 0,
sizeof(info)/
sizeof(ULONG_PTR), (ULONG_PTR*)&info );
66 __except(EXCEPTION_EXECUTE_HANDLER)
73 void KKThread::SetThreadName ()
91 shutdownPrerequisites (NULL),
92 startPrerequisites (NULL),
94 terminateFlag (
false),
96 threadName
(_threadName
) 100 windowsThreadHandle = NULL;
111 delete shutdownPrerequisites; shutdownPrerequisites = NULL;
112 delete startPrerequisites; startPrerequisites = NULL;
119 kkint32 estMem =
sizeof (crashed) +
121 sizeof (shutdownFlag) +
122 sizeof (startPrerequisites) +
123 sizeof (shutdownPrerequisites) +
125 sizeof (terminateFlag) +
131 if (startPrerequisites)
134 if (shutdownPrerequisites)
141 KKStr KKThread::threadStatusDescs[] = {
"NULL",
"Started",
"Running",
"Stopping",
"Stopped"};
149 return threadStatusDescs[(
int)ts];
156 if (msgQueue == NULL)
159 <<
"KKThread::AddMsg ***ERROR*** msgQuue is not defined." << endl
160 <<
" Msg[" << *msg <<
"]." << endl
176 if (msgQueue == NULL)
179 <<
"KKThread::AddMsg ***ERROR*** msgQuue is not defined." << endl
180 <<
" Msg[" << msg <<
"]." << endl
199 results->PushOnBack (msg);
210 terminateFlag =
true;
254 if (maxTimeToWait > 0)
257 timeWaitedSoFar = (
kkuint32)(now - startTime);
258 if (timeWaitedSoFar > maxTimeToWait)
282 KKThreadPtr tp = (KKThreadPtr)param;
293 catch (
const std::exception e2)
296 const char* e2What = e2.what ();
297 KKStr msg (30 + strlen (e2What));
298 msg <<
"std::exception: " << e2What;
314 void* ThreadStartCallBack (
void* param)
316 KKThreadPtr tp = (KKThreadPtr)param;
317 tp->Status (KKThread::Starting);
322 catch (
const KKException& e1)
325 tp->ExceptionText (e1.ToString ());
327 catch (
const std::exception e2)
330 const char* e2What = e2.what ();
331 KKStr msg (30 + strlen (e2What));
332 msg <<
"std::exception: " << e2What;
333 tp->ExceptionText (msg);
338 const char* e2What = e2.what ();
339 tp->ExceptionText (
"exception(...) trapped.");
341 tp->Status (KKThread::Stopped);
356 priority = _priority;
358 windowsThreadHandle = (
unsigned long*)CreateThread (NULL,
360 (LPTHREAD_START_ROUTINE)ThreadStartCallBack,
365 if (windowsThreadHandle == NULL)
372 threadId = (kkint32)windowsThreadId;
376 case ThreadPriority::Null:
377 case ThreadPriority::Normal:
378 SetThreadPriority (windowsThreadHandle, THREAD_PRIORITY_BELOW_NORMAL);
381 case ThreadPriority::Low:
382 SetThreadPriority (windowsThreadHandle, THREAD_PRIORITY_NORMAL);
385 case ThreadPriority::High:
386 SetThreadPriority (windowsThreadHandle, THREAD_PRIORITY_ABOVE_NORMAL);
396 void KKThread::Start (ThreadPriority _priority,
400 int returnCd = pthread_create (&linuxThreadId,
408 throw KKException (
"Failed to create thread");
412 threadId = (kkint32)linuxThreadId;
429 if (windowsThreadHandle == NULL)
432 WaitForSingleObject (windowsThreadHandle, INFINITE);
433 CloseHandle (windowsThreadHandle);
434 windowsThreadHandle = NULL;
437 void KKThread::Kill ()
439 pthread_cancel (linuxThreadId);
450 const char* msg =
"KKThread::Run ***ERROR*** This method should have been over ridden by a derived class.";
452 cerr << endl << endl << msg << endl
461 if (!startPrerequisites)
464 KKThreadList::const_iterator idx;
465 for (idx = startPrerequisites->begin (); idx != startPrerequisites->end (); ++idx)
468 if (preReq == _thread)
482 if (!shutdownPrerequisites)
485 KKThreadList::const_iterator idx;
486 for (idx = shutdownPrerequisites->begin (); idx != shutdownPrerequisites->end (); ++idx)
489 if (preReq == _thread)
503 if (!startPrerequisites)
505 startPrerequisites->PushOnBack (_thread);
512 if (!shutdownPrerequisites)
514 shutdownPrerequisites->PushOnBack (_thread);
521 if (!shutdownPrerequisites)
524 KKThreadList::const_iterator idx;
525 for (idx = shutdownPrerequisites->begin (); idx != shutdownPrerequisites->end (); ++idx)
538 if (!startPrerequisites)
541 KKThreadList::const_iterator idx;
542 for (idx = startPrerequisites->begin (); idx != startPrerequisites->end (); ++idx)
579 KKThreadList::const_iterator idx;
580 for (idx = begin (); idx != end (); ++idx)
582 KKThreadPtr t = *idx;
void AddMsg(KKStrPtr msg)
Take ownership of 'msg' and add to end of the queue.
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
void Start(ThreadPriority _priority, bool &successful)
bool ThreadStillProcessing() const
KKThreadList(const KKThreadList &list)
ThreadStatus Status() const
kkint32 MemoryConsumedEstimated() const
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
KKThread(const KKStr &_threadName, KKThreadManagerPtr _threadManager, MsgQueuePtr _msgQueue)
kkint32 MemoryConsumedEstimated()
Returns an estimate of the amount of memory consumed in bytes by this instance.
void AddMsg(const KKStr &msg)
bool ThereIsACircularReferenceShutdown(KKThreadPtr _thread) const
void AddStartPrerequistite(KKThreadPtr _thread)
Specify threads that must start before this thread is started.
unsigned __int32 kkuint32
kkint32 MemoryConsumedEstimated() const
virtual void TerminateFlagChanged()
Will be called whenever the value of 'terminateFlag' is changed; derived classes should override this...
KKStrPtr GetNextMsg()
Removes from the queue the oldest message added to the queue that has not been removed.
kkuint32 Len() const
Returns the number of characters in the string.
KKThreadManager * KKThreadManagerPtr
void ExceptionText(const KKStr &_exceptionText)
KKStr(const KKStr &str)
Copy Constructor.
The base class to be used any thread.
bool OkToShutdown() const
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
void AddShutdownPrerequistite(KKThreadPtr _thread)
Specify threads that must stop before this thread is started.
void osSleepMiliSecs(kkuint32 numMiliSecs)
static const KKStr & EmptyStr()
Static method that returns an Empty String.
bool ThereIsACircularReferenceStart(KKThreadPtr _thread) const
void Status(ThreadStatus _status)
kkint32 MemoryConsumedEstimated()
unsigned __int64 kkuint64
void WaitForThreadToStop(kkuint32 maxTimeToWait)
Called by separate thread; will stay in loop until the thread controlled by this instance shutdown or...
KKThreadList(bool _owner=true)
KKException(const char *_exceptionStr)
void Kill()
stops the running thread and frees the thread handle
void AddMsg(KKStrPtr msg)
unsigned int ThreadStartCallBack(void *param)
static const KKStr & ThreadStatusToStr(ThreadStatus)
void Crashed(bool _crashed)
virtual const KKStr & ToString() const
void AddMsg(const KKStr &msg)
const TimeType & Time() const