|
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 |
|
20 /* |
|
21 ------------------------------------------------------------------------------- |
|
22 |
|
23 DESCRIPTION |
|
24 |
|
25 This file contains the header file of template class Thread and ThreadData. |
|
26 Interface Runnable is defined. |
|
27 |
|
28 ------------------------------------------------------------------------------- |
|
29 */ |
|
30 |
|
31 #ifndef THREAD_H |
|
32 #define THREAD_H |
|
33 |
|
34 #include "sync.h" |
|
35 #include <process.h> |
|
36 |
|
37 |
|
38 typedef unsigned (__stdcall *THREAD_START_FUNC) (void *); |
|
39 |
|
40 |
|
41 //********************************************************************************** |
|
42 // Template Class ThreadData |
|
43 // |
|
44 //********************************************************************************** |
|
45 |
|
46 template <class T> |
|
47 struct ThreadData |
|
48 { |
|
49 public: |
|
50 typedef void (T::*TFunc)(); |
|
51 HANDLE m_hEvent; |
|
52 T* m_pThreadObject; |
|
53 TFunc m_pThreadFunc; |
|
54 static DWORD _ThreadFunc(ThreadData<T>* pThis) |
|
55 { |
|
56 ThreadData<T> td = *pThis; |
|
57 SetEvent(td.m_hEvent); |
|
58 ((*(td.m_pThreadObject)).*(td.m_pThreadFunc))(); |
|
59 return 0; |
|
60 } |
|
61 }; |
|
62 |
|
63 //********************************************************************************** |
|
64 // Template Class Runnable |
|
65 // |
|
66 //********************************************************************************** |
|
67 |
|
68 class Runnable |
|
69 { |
|
70 public: |
|
71 virtual void Run() = 0; |
|
72 virtual void Stop() = 0; |
|
73 }; |
|
74 |
|
75 //********************************************************************************** |
|
76 // Template Class Thread |
|
77 // |
|
78 // This is the template for thread class, implemented by all threads in DataGateway |
|
79 // The template encapsulates Windows API tread functionality |
|
80 //********************************************************************************** |
|
81 |
|
82 template <class T> |
|
83 class Thread : public Runnable |
|
84 { |
|
85 public: |
|
86 Thread(); |
|
87 Thread(Runnable* runnable); |
|
88 ~Thread(); |
|
89 void Run(); |
|
90 void Start(); |
|
91 void Stop(); |
|
92 void Suspend(); |
|
93 void Resume(); |
|
94 HANDLE ThreadHandle(); |
|
95 private: |
|
96 HANDLE CreateMemberThread(Runnable* p, |
|
97 void (Runnable::*func)()); |
|
98 private: |
|
99 HANDLE m_hMemberThread; |
|
100 Runnable* m_Runnable; |
|
101 }; |
|
102 |
|
103 template <class T> |
|
104 Thread<T>::Thread() |
|
105 { |
|
106 m_hMemberThread = NULL; |
|
107 m_Runnable = this; |
|
108 } |
|
109 |
|
110 template <class T> |
|
111 Thread<T>::Thread(Runnable* runnable) |
|
112 { |
|
113 m_hMemberThread = NULL; |
|
114 if (runnable != NULL) |
|
115 { |
|
116 m_Runnable = runnable; |
|
117 } |
|
118 else |
|
119 { |
|
120 m_Runnable = this; |
|
121 } |
|
122 } |
|
123 |
|
124 template <class T> |
|
125 Thread<T>::~Thread() |
|
126 { |
|
127 if (m_hMemberThread != NULL) |
|
128 { |
|
129 CloseHandle(m_hMemberThread); |
|
130 } |
|
131 m_Runnable = NULL; |
|
132 } |
|
133 |
|
134 template <class T> |
|
135 void Thread<T>::Run() |
|
136 { |
|
137 } |
|
138 |
|
139 template <class T> |
|
140 void Thread<T>::Suspend() |
|
141 { |
|
142 if (m_hMemberThread != NULL) |
|
143 { |
|
144 SuspendThread(m_hMemberThread); |
|
145 } |
|
146 } |
|
147 |
|
148 template <class T> |
|
149 void Thread<T>::Resume() |
|
150 { |
|
151 if (m_hMemberThread != NULL) |
|
152 { |
|
153 ResumeThread(m_hMemberThread); |
|
154 } |
|
155 } |
|
156 |
|
157 template <class T> |
|
158 void Thread<T>::Start() |
|
159 { |
|
160 if (m_Runnable != NULL) |
|
161 { |
|
162 m_hMemberThread = CreateMemberThread(m_Runnable, |
|
163 &Runnable::Run); |
|
164 if (m_hMemberThread == NULL) |
|
165 { |
|
166 cerr << "Thread: could not create thread" << endl; |
|
167 } |
|
168 } |
|
169 else |
|
170 { |
|
171 m_hMemberThread = NULL; |
|
172 cerr << "Thread: threadfunc NULL" << endl; |
|
173 } |
|
174 } |
|
175 |
|
176 template <class T> |
|
177 void Thread<T>::Stop() |
|
178 { |
|
179 m_Runnable->Stop(); |
|
180 } |
|
181 |
|
182 template <class T> |
|
183 HANDLE Thread<T>::ThreadHandle() |
|
184 { |
|
185 return m_hMemberThread; |
|
186 } |
|
187 |
|
188 template <class T> |
|
189 HANDLE Thread<T>::CreateMemberThread(Runnable* p, |
|
190 void (Runnable::*func)()) |
|
191 { |
|
192 ThreadData<Runnable> td; |
|
193 td.m_pThreadObject = p; |
|
194 td.m_pThreadFunc = func; |
|
195 td.m_hEvent = CreateEvent(NULL, 0, 0, NULL); |
|
196 |
|
197 unsigned int Dummy; |
|
198 HANDLE ThreadHandle = (HANDLE)_beginthreadex(NULL, |
|
199 NULL, |
|
200 (THREAD_START_FUNC)ThreadData<Runnable>::_ThreadFunc, |
|
201 &td, |
|
202 NULL, |
|
203 &Dummy); |
|
204 |
|
205 WaitForSingleObject(td.m_hEvent,INFINITE); |
|
206 CloseHandle(td.m_hEvent); |
|
207 return ThreadHandle; |
|
208 } |
|
209 |
|
210 #endif |
|
211 |
|
212 // End of the file |