Joypad Scol plugin
ou_thread.cpp
Go to the documentation of this file.
1
7/* Copyright 2000 - 2005 Vijay Mathew Pandyalakal. All rights reserved.
8 *
9 * This software may be used or modified for any purpose, personal or
10 * commercial. Open Source redistributions are permitted.
11 *
12 * Redistributions qualify as "Open Source" under one of the following terms:
13 *
14 * Redistributions are made at no charge beyond the reasonable cost of
15 * materials and delivery.
16 *
17 * Redistributions are accompanied by a copy of the Source Code or by an
18 * irrevocable offer to provide a copy of the Source Code for up to three
19 * years at the cost of materials and delivery. Such redistributions
20 * must allow further use, modification, and redistribution of the Source
21 * Code under substantially the same terms as this license.
22 *
23 * Redistributions of source code must retain the copyright notices as they
24 * appear in each source code file, these license terms, and the
25 * disclaimer/limitation of liability set forth as paragraph 6 below.
26 *
27 * Redistributions in binary form must reproduce this Copyright Notice,
28 * these license terms, and the disclaimer/limitation of liability set
29 * forth as paragraph 6 below, in the documentation and/or other materials
30 * provided with the distribution.
31 *
32 * The Software is provided on an "AS IS" basis. No warranty is
33 * provided that the Software is free of defects, or fit for a
34 * particular purpose.
35 *
36 * Limitation of Liability. The Author shall not be liable
37 * for any damages suffered by the Licensee or any third party resulting
38 * from use of the Software.
39 */
40
41#include <string>
42using namespace std;
43
44#include <windows.h>
45
46#include "ou_thread.h"
47using namespace openutils;
48
49const int Thread::P_ABOVE_NORMAL = THREAD_PRIORITY_ABOVE_NORMAL;
50const int Thread::P_BELOW_NORMAL = THREAD_PRIORITY_BELOW_NORMAL;
51const int Thread::P_HIGHEST = THREAD_PRIORITY_HIGHEST;
52const int Thread::P_IDLE = THREAD_PRIORITY_IDLE;
53const int Thread::P_LOWEST = THREAD_PRIORITY_LOWEST;
54const int Thread::P_NORMAL = THREAD_PRIORITY_NORMAL;
55const int Thread::P_CRITICAL = THREAD_PRIORITY_TIME_CRITICAL;
56
64 m_hThread = NULL;
65 m_strName = "null";
66}
67
72Thread::Thread(const char* nm) {
73 m_hThread = NULL;
74 m_strName = nm;
75}
76
78 if(m_hThread != NULL) {
79 stop();
80 }
81}
82
86void Thread::setName(const char* nm) {
87 m_strName = nm;
88}
89
93string Thread::getName() const {
94 return m_strName;
95}
96
102 // Base run
103}
104
109void Thread::sleep(long ms) {
110 Sleep(ms);
111}
112
118 DWORD tid = 0;
119 m_hThread = (unsigned long*)CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_ou_thread_proc,(Thread*)this,0,&tid);
120 if(m_hThread == NULL) {
121 throw ThreadException("Failed to create thread");
122 }
123 else
125}
126
131 if(m_hThread == NULL) return;
132 WaitForSingleObject(m_hThread, INFINITE);
133 CloseHandle(m_hThread);
134 m_hThread = NULL;
135}
136
143 if(m_hThread == NULL) {
144 throw ThreadException("Thread object is null");
145 }else {
146 if(SetThreadPriority(m_hThread,tp) == 0) {
147 throw ThreadException("Failed to set priority");
148 }
149 }
150}
151
156 if(m_hThread == NULL) {
157 throw ThreadException("Thread object is null");
158 }else {
159 if(SuspendThread(m_hThread) < 0) {
160 throw ThreadException("Failed to suspend thread");
161 }
162 }
163}
164
169 if(m_hThread == NULL) {
170 throw ThreadException("Thread object is null");
171 }else {
172 if(ResumeThread(m_hThread) < 0) {
173 throw ThreadException("Failed to resume thread");
174 }
175 }
176}
177
184bool Thread::wait(const char* m,long ms) {
185 HANDLE h = OpenMutex(MUTEX_ALL_ACCESS,FALSE,m);
186 if(h == NULL) {
187 throw ThreadException("Mutex not found");
188 }
189 DWORD d = WaitForSingleObject(h,ms);
190 switch(d) {
191 case WAIT_ABANDONED:
192 throw ThreadException("Mutex not signaled");
193 break;
194 case WAIT_OBJECT_0:
195 return true;
196 case WAIT_TIMEOUT:
197 throw ThreadException("Wait timed out");
198 break;
199 }
200 return false;
201}
202
207void Thread::release(const char* m) {
208 HANDLE h = OpenMutex(MUTEX_ALL_ACCESS,FALSE,m);
209 if(h == NULL) {
210 throw ThreadException("Invalid mutex handle");
211 }
212 if(ReleaseMutex(h) == 0) {
213 throw ThreadException("Failed to release mutex");
214 }
215}
216
224 m_hMutex = NULL;
225 m_strName = "";
226}
227
232Mutex::Mutex(const char* nm) {
233 m_strName = nm;
234 m_hMutex = (unsigned long*)CreateMutex(NULL,FALSE,nm);
235 if(m_hMutex == NULL) {
236 throw ThreadException("Failed to create mutex");
237 }
238}
239
244void Mutex::create(const char* nm) {
245 if(m_hMutex != NULL) {
246 CloseHandle(m_hMutex);
247 m_hMutex = NULL;
248 }
249 m_strName = nm;
250 m_hMutex = (unsigned long*)CreateMutex(NULL,FALSE,nm);
251 if(m_hMutex == NULL) {
252 throw ThreadException("Failed to create mutex");
253 }
254}
258unsigned long* Mutex::getMutexHandle() {
259 return m_hMutex;
260}
261
266 return m_strName;
267}
268
270 if(m_hMutex != NULL) {
271 CloseHandle(m_hMutex);
272 }
273}
274
276 /*if(m_hMutex != NULL) {
277 CloseHandle(m_hMutex);
278 }*/
279}
280
281// ThreadException
283 msg = m;
284}
285
287 return msg;
288}
289
290// global thread caallback
291unsigned int _ou_thread_proc(void* param) {
292 Thread* tp = (Thread*)param;
293 tp->run();
294 return 0;
295}
std::string getName()
void create(const char *nm)
unsigned long * getMutexHandle()
std::string getMessage() const
ThreadException(const char *m)
static const int P_BELOW_NORMAL
Definition ou_thread.h:81
static const int P_IDLE
Definition ou_thread.h:83
virtual ~Thread()
Definition ou_thread.cpp:77
static const int P_HIGHEST
Definition ou_thread.h:82
void release(const char *m)
bool wait(const char *m, long ms=5000)
static const int P_CRITICAL
Definition ou_thread.h:86
void setName(const char *nm)
Definition ou_thread.cpp:86
static const int P_ABOVE_NORMAL
Definition ou_thread.h:80
std::string getName() const
Definition ou_thread.cpp:93
void sleep(long ms)
static const int P_NORMAL
Definition ou_thread.h:85
virtual void run()
void setPriority(int p)
static const int P_LOWEST
Definition ou_thread.h:84
unsigned int _ou_thread_proc(void *param)