|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * This file contains the header file of template class Thread and ThreadData. |
|
16 * Interface Runnable is defined. |
|
17 */ |
|
18 |
|
19 #ifndef THREAD_H |
|
20 #define THREAD_H |
|
21 |
|
22 #include "sync.h" |
|
23 #include <process.h> |
|
24 |
|
25 |
|
26 typedef unsigned (__stdcall *THREAD_START_FUNC) (void *); |
|
27 |
|
28 |
|
29 //********************************************************************************** |
|
30 // Template Class ThreadData |
|
31 // |
|
32 //********************************************************************************** |
|
33 |
|
34 template <class T> |
|
35 struct ThreadData |
|
36 { |
|
37 public: |
|
38 typedef void (T::*TFunc)(); |
|
39 HANDLE m_hEvent; |
|
40 T* m_pThreadObject; |
|
41 TFunc m_pThreadFunc; |
|
42 static DWORD _ThreadFunc(ThreadData<T>* pThis) |
|
43 { |
|
44 ThreadData<T> td = *pThis; |
|
45 SetEvent(td.m_hEvent); |
|
46 ((*(td.m_pThreadObject)).*(td.m_pThreadFunc))(); |
|
47 return 0; |
|
48 } |
|
49 }; |
|
50 |
|
51 //********************************************************************************** |
|
52 // Template Class Runnable |
|
53 // |
|
54 //********************************************************************************** |
|
55 |
|
56 class Runnable |
|
57 { |
|
58 public: |
|
59 virtual void Run() = 0; |
|
60 virtual void Stop() = 0; |
|
61 }; |
|
62 |
|
63 //********************************************************************************** |
|
64 // Template Class Thread |
|
65 // |
|
66 // This is the template for thread class, implemented by all threads in DataGateway |
|
67 // The template encapsulates Windows API tread functionality |
|
68 //********************************************************************************** |
|
69 |
|
70 template <class T> |
|
71 class Thread : public Runnable |
|
72 { |
|
73 public: |
|
74 Thread(); |
|
75 Thread(Runnable* runnable); |
|
76 ~Thread(); |
|
77 void Run(); |
|
78 void Start(); |
|
79 void Stop(); |
|
80 void Suspend(); |
|
81 void Resume(); |
|
82 HANDLE ThreadHandle(); |
|
83 private: |
|
84 HANDLE CreateMemberThread(Runnable* p, |
|
85 void (Runnable::*func)()); |
|
86 private: |
|
87 HANDLE m_hMemberThread; |
|
88 Runnable* m_Runnable; |
|
89 }; |
|
90 |
|
91 template <class T> |
|
92 Thread<T>::Thread() |
|
93 { |
|
94 m_hMemberThread = NULL; |
|
95 m_Runnable = this; |
|
96 } |
|
97 |
|
98 template <class T> |
|
99 Thread<T>::Thread(Runnable* runnable) |
|
100 { |
|
101 m_hMemberThread = NULL; |
|
102 if (runnable != NULL) |
|
103 { |
|
104 m_Runnable = runnable; |
|
105 } |
|
106 else |
|
107 { |
|
108 m_Runnable = this; |
|
109 } |
|
110 } |
|
111 |
|
112 template <class T> |
|
113 Thread<T>::~Thread() |
|
114 { |
|
115 if (m_hMemberThread != NULL) |
|
116 { |
|
117 CloseHandle(m_hMemberThread); |
|
118 } |
|
119 m_Runnable = NULL; |
|
120 } |
|
121 |
|
122 template <class T> |
|
123 void Thread<T>::Run() |
|
124 { |
|
125 } |
|
126 |
|
127 template <class T> |
|
128 void Thread<T>::Suspend() |
|
129 { |
|
130 if (m_hMemberThread != NULL) |
|
131 { |
|
132 SuspendThread(m_hMemberThread); |
|
133 } |
|
134 } |
|
135 |
|
136 template <class T> |
|
137 void Thread<T>::Resume() |
|
138 { |
|
139 if (m_hMemberThread != NULL) |
|
140 { |
|
141 ResumeThread(m_hMemberThread); |
|
142 } |
|
143 } |
|
144 |
|
145 template <class T> |
|
146 void Thread<T>::Start() |
|
147 { |
|
148 if (m_Runnable != NULL) |
|
149 { |
|
150 m_hMemberThread = CreateMemberThread(m_Runnable, |
|
151 &Runnable::Run); |
|
152 if (m_hMemberThread == NULL) |
|
153 { |
|
154 cerr << "Thread: could not create thread" << endl; |
|
155 } |
|
156 } |
|
157 else |
|
158 { |
|
159 m_hMemberThread = NULL; |
|
160 cerr << "Thread: threadfunc NULL" << endl; |
|
161 } |
|
162 } |
|
163 |
|
164 template <class T> |
|
165 void Thread<T>::Stop() |
|
166 { |
|
167 m_Runnable->Stop(); |
|
168 } |
|
169 |
|
170 template <class T> |
|
171 HANDLE Thread<T>::ThreadHandle() |
|
172 { |
|
173 return m_hMemberThread; |
|
174 } |
|
175 |
|
176 template <class T> |
|
177 HANDLE Thread<T>::CreateMemberThread(Runnable* p, |
|
178 void (Runnable::*func)()) |
|
179 { |
|
180 ThreadData<Runnable> td; |
|
181 td.m_pThreadObject = p; |
|
182 td.m_pThreadFunc = func; |
|
183 td.m_hEvent = CreateEvent(NULL, 0, 0, NULL); |
|
184 |
|
185 unsigned int Dummy; |
|
186 HANDLE ThreadHandle = (HANDLE)_beginthreadex(NULL, |
|
187 NULL, |
|
188 (THREAD_START_FUNC)ThreadData<Runnable>::_ThreadFunc, |
|
189 &td, |
|
190 NULL, |
|
191 &Dummy); |
|
192 |
|
193 WaitForSingleObject(td.m_hEvent,INFINITE); |
|
194 CloseHandle(td.m_hEvent); |
|
195 return ThreadHandle; |
|
196 } |
|
197 |
|
198 #endif |
|
199 |
|
200 // End of the file |