diff -r 7fdc9a71d314 -r 8ad140f3dd41 hti/PC_Tools/DataGateway/INC/thread.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hti/PC_Tools/DataGateway/INC/thread.h Wed Oct 13 16:17:58 2010 +0300 @@ -0,0 +1,200 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* This file contains the header file of template class Thread and ThreadData. +* Interface Runnable is defined. +*/ + +#ifndef THREAD_H +#define THREAD_H + +#include "sync.h" +#include + + +typedef unsigned (__stdcall *THREAD_START_FUNC) (void *); + + +//********************************************************************************** +// Template Class ThreadData +// +//********************************************************************************** + +template +struct ThreadData +{ + public: + typedef void (T::*TFunc)(); + HANDLE m_hEvent; + T* m_pThreadObject; + TFunc m_pThreadFunc; + static DWORD _ThreadFunc(ThreadData* pThis) + { + ThreadData td = *pThis; + SetEvent(td.m_hEvent); + ((*(td.m_pThreadObject)).*(td.m_pThreadFunc))(); + return 0; + } +}; + +//********************************************************************************** +// Template Class Runnable +// +//********************************************************************************** + +class Runnable +{ + public: + virtual void Run() = 0; + virtual void Stop() = 0; +}; + +//********************************************************************************** +// Template Class Thread +// +// This is the template for thread class, implemented by all threads in DataGateway +// The template encapsulates Windows API tread functionality +//********************************************************************************** + +template +class Thread : public Runnable +{ + public: + Thread(); + Thread(Runnable* runnable); + ~Thread(); + void Run(); + void Start(); + void Stop(); + void Suspend(); + void Resume(); + HANDLE ThreadHandle(); + private: + HANDLE CreateMemberThread(Runnable* p, + void (Runnable::*func)()); + private: + HANDLE m_hMemberThread; + Runnable* m_Runnable; +}; + +template +Thread::Thread() +{ + m_hMemberThread = NULL; + m_Runnable = this; +} + +template +Thread::Thread(Runnable* runnable) +{ + m_hMemberThread = NULL; + if (runnable != NULL) + { + m_Runnable = runnable; + } + else + { + m_Runnable = this; + } +} + +template +Thread::~Thread() +{ + if (m_hMemberThread != NULL) + { + CloseHandle(m_hMemberThread); + } + m_Runnable = NULL; +} + +template +void Thread::Run() +{ +} + +template +void Thread::Suspend() +{ + if (m_hMemberThread != NULL) + { + SuspendThread(m_hMemberThread); + } +} + +template +void Thread::Resume() +{ + if (m_hMemberThread != NULL) + { + ResumeThread(m_hMemberThread); + } +} + +template +void Thread::Start() +{ + if (m_Runnable != NULL) + { + m_hMemberThread = CreateMemberThread(m_Runnable, + &Runnable::Run); + if (m_hMemberThread == NULL) + { + cerr << "Thread: could not create thread" << endl; + } + } + else + { + m_hMemberThread = NULL; + cerr << "Thread: threadfunc NULL" << endl; + } +} + +template +void Thread::Stop() +{ + m_Runnable->Stop(); +} + +template +HANDLE Thread::ThreadHandle() +{ + return m_hMemberThread; +} + +template +HANDLE Thread::CreateMemberThread(Runnable* p, + void (Runnable::*func)()) +{ + ThreadData td; + td.m_pThreadObject = p; + td.m_pThreadFunc = func; + td.m_hEvent = CreateEvent(NULL, 0, 0, NULL); + + unsigned int Dummy; + HANDLE ThreadHandle = (HANDLE)_beginthreadex(NULL, + NULL, + (THREAD_START_FUNC)ThreadData::_ThreadFunc, + &td, + NULL, + &Dummy); + + WaitForSingleObject(td.m_hEvent,INFINITE); + CloseHandle(td.m_hEvent); + return ThreadHandle; +} + +#endif + +// End of the file \ No newline at end of file