summaryrefslogtreecommitdiff
path: root/source/api_wrappers/linux/CThreadWrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/api_wrappers/linux/CThreadWrapper.cpp')
-rw-r--r--source/api_wrappers/linux/CThreadWrapper.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/source/api_wrappers/linux/CThreadWrapper.cpp b/source/api_wrappers/linux/CThreadWrapper.cpp
new file mode 100644
index 0000000..3e95da8
--- /dev/null
+++ b/source/api_wrappers/linux/CThreadWrapper.cpp
@@ -0,0 +1,119 @@
+/*******************************************************************************
+*
+* File name: CThreadWrapper.cpp
+* Language: Visual C++
+* Description: CThreadWrapper class definitions
+* The class implements OS independent thread wrapper.
+* The class contains methods for suspending resuming and waiting for threads.
+* The implementations of the methods is linux dependent,
+* but the interface of the functions is same as the appropriate
+* WIN32 implementation.
+*
+*
+* Copyright (C) ST-Ericsson SA 2011
+* License terms: 3-clause BSD license
+*
+*******************************************************************************/
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//
+
+#include <string>
+#include <signal.h>
+
+#include "Types.h"
+#include "CThreadWrapper.h"
+#include "CCriticalSection.h"
+
+// ******************************************************************************
+// Name: CThreadWrapper()
+// Desc: CEventObject constructor which initializes the class members
+// Ret:
+// ******************************************************************************
+CThreadWrapper::CThreadWrapper(void*(*pStartAddress)(void *), void *pArgument)
+{
+ m_pStartAddress = pStartAddress;
+ m_pArgument = pArgument;
+ m_tid = (pthread_t)0;
+}
+
+// ******************************************************************************
+// Name: ~CThreadWrapper()
+// Desc: CEventObject destructor
+// Ret:
+// ******************************************************************************
+CThreadWrapper::~CThreadWrapper()
+{
+}
+
+// ******************************************************************************
+// Name: Wait()
+// Desc: Waits for the thread to finish its job
+// Ret: WAIT_OBJECT_0 on success and EAIT_TIMEOUT otherwise
+// ******************************************************************************
+DWORD CThreadWrapper::Wait(DWORD dwMilliseconds)
+{
+ if ((int)m_tid != 0) {
+ return m_ThreadEndedEvt.Wait(dwMilliseconds);
+ } else {
+ return WAIT_OBJECT_0;
+ }
+}
+
+// ******************************************************************************
+// Name: ResumeThread()
+// Desc: Resumes the thread. Should be called after the thread is created
+// Ret:
+// ******************************************************************************
+void CThreadWrapper::ResumeThread()
+{
+ pthread_create(&m_tid, NULL, CThreadWrapper::ThreadFunc, this);
+}
+
+// ******************************************************************************
+// Name: ResumeThread()
+// Desc: Suspends the running thread.
+// Ret:
+// ******************************************************************************
+void CThreadWrapper::SuspendThread()
+{
+ // this will stop all active threads
+ pthread_kill(m_tid, SIGSTOP);
+}
+
+// ******************************************************************************
+// Name: WaitToDie()
+// Desc: Waits 1000ms for thread to die
+// Ret:
+// ******************************************************************************
+void CThreadWrapper::WaitToDie(DWORD dwMilliseconds)
+{
+ CThreadWrapper::Wait(dwMilliseconds);
+}
+// ******************************************************************************
+// Name: ThreadFunc()
+// Desc: Sets event when the thread finishes its job
+// Ret:
+// ******************************************************************************
+void *CThreadWrapper::ThreadFunc(void *arg)
+{
+ CThreadWrapper *pthis = (CThreadWrapper *)arg;
+
+ pthis->m_pStartAddress(pthis->m_pArgument);
+
+ // Thread has finished, set appropriate event here
+ pthis->m_ThreadEndedEvt.SetEvent();
+ pthread_detach(pthread_self());
+ pthis->m_tid = (pthread_t)0;
+ return NULL;
+}
+
+// ******************************************************************************
+// Name: GetThreadId()
+// Desc: Returns the thread ID
+// Ret: Thread ID
+// ******************************************************************************
+DWORD CThreadWrapper::GetThreadId()
+{
+ return (DWORD)m_tid;
+}