1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
#pragma once
#include <windows.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
namespace libusbemu {
struct QuickEvent
{
friend struct EventList;
private:
HANDLE hEvent;
public:
inline QuickEvent(const bool signaled=false) : hEvent(NULL)
{
hEvent = CreateEvent(NULL, TRUE, (BOOL)signaled, NULL);
}
inline ~QuickEvent()
{
CloseHandle(hEvent);
}
inline void Signal()
{
SetEvent(hEvent);
}
inline void Reset()
{
ResetEvent(hEvent);
}
inline bool Check()
{
return(WAIT_OBJECT_0 == WaitForSingleObject(hEvent, 0));
}
inline const bool WaitUntilTimeout(const unsigned int milliseconds)
{
return(WAIT_OBJECT_0 == WaitForSingleObject(hEvent, (DWORD)milliseconds));
}
inline void Wait()
{
WaitUntilTimeout(INFINITE);
}
};
struct QuickThread
{
private:
HANDLE hThread;
// Type-safe wrapper that converts arbitrary function signatures into the
// required signature of Win32 Thread Procedures (LPTHREAD_START_ROUTINE).
// Any Win32 LPTHREAD_START_ROUTINE declared routine can be wrapped as well.
// The wrapper is also capable of cleaning up itself upon thread termination.
// This wrapper can be extended in the future to support member-functions
// to run as thread procedures.
template<typename F>
struct ThreadWrapper
{
struct State
{
F* routine;
void* params;
bool release;
QuickThread* instance;
QuickEvent* sigclone;
};
static DWORD WINAPI Thunk(LPVOID lpParameter)
{
State state = *((State*)lpParameter); // clone state (no heap alloc!)
state.sigclone->Signal(); // done cloning, signal back to creator
state.sigclone = NULL;
// start wrapped thread procedure
DWORD ret = (DWORD)state.routine(state.params);
// release the associated QuickThread instance if requested
if (state.release)
delete(state.instance);
return(ret);
}
};
// allow the creation of pseudo-handles to the calling thread
// this constructor cannot and should never be called explicitly!
// use QuickThread::Myself() to spawn a pseudo-handle QuickThread
inline QuickThread() : hThread(GetCurrentThread()) {}
public:
template<typename F>
inline QuickThread(F* proc, void* params, const bool auto_release=false) : hThread(NULL)
{
// the 'typename' is required here because of dependent names...
// MSVC relaxes this constraint, but it goes against the standard.
typename ThreadWrapper<F>::State state;
state.routine = proc;
state.params = params;
state.release = auto_release;
state.instance = this;
// in order to prevent heap allocation, an event is created so that the
// thunk function can signal back when it is done cloning the state; this
// may look like unnecessary overhead, but the less heap memory control,
// the better becomes the management and maintenance of this class.
QuickEvent hWaitThunkCloneState;
state.sigclone = &hWaitThunkCloneState;
// Ready to issue the thread creation:
hThread = CreateThread(NULL, 0, &ThreadWrapper<F>::Thunk, (LPVOID)&state, 0, NULL);
// Wait for the thread thunk to clone the state...
hWaitThunkCloneState.Wait();
// Event object will then be automatically released upon return
}
inline ~QuickThread()
{
// only if not a pseudo-handle...
if (hThread == GetCurrentThread())
return;
CloseHandle(hThread);
}
static inline QuickThread Myself()
{
return(QuickThread());
}
inline void Join()
{
WaitForSingleObject(hThread, INFINITE);
}
inline bool TryJoin()
{
return(WAIT_OBJECT_0 == WaitForSingleObject(hThread, 0));
}
inline bool LowerPriority()
{
return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL));
}
inline bool RaisePriority()
{
return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL));
}
static inline void Sleep(int milliseconds)
{
::Sleep(milliseconds);
}
// Yield is already a Win32 macro (WinBase.h)...
// http://winapi.freetechsecrets.com/win32/WIN32Yield.htm
#ifdef Yield
#undef Yield
#endif
// A pragma push/pop could be used instead, but it does not solve the issues
// http://stackoverflow.com/questions/1793800/can-i-redefine-a-c-macro-for-a-few-includes-and-then-define-it-back
//#pragma push_macro("Yield")
//#undef Yield
static inline void Yield()
{
// Sleep(0) or Sleep(1) ?!
// http://stackoverflow.com/questions/1413630/switchtothread-thread-yield-vs-thread-sleep0-vs-thead-sleep1
::Sleep(1);
// could also use the following (but the semantics are quite shady...):
// http://msdn.microsoft.com/en-us/library/ms686352(v=vs.85).aspx
// SwitchToThread();
}
//#pragma pop_macro("Yield")
};
struct QuickMutex
{
private:
CRITICAL_SECTION cs;
public:
inline QuickMutex()
{
InitializeCriticalSection(&cs);
}
inline ~QuickMutex()
{
DeleteCriticalSection(&cs);
}
inline const bool TryEnter()
{
return(0 != TryEnterCriticalSection(&cs));
}
inline void Enter()
{
EnterCriticalSection(&cs);
}
inline void Leave()
{
LeaveCriticalSection(&cs);
}
};
struct EventList
{
QuickMutex mutex;
std::vector<QuickEvent*> m_vEvents;
std::vector<HANDLE> m_vHandles;
EventList() {};
~EventList() {};
const bool AttachEvent(QuickEvent* poEvent)
{
mutex.Enter();
m_vEvents.push_back(poEvent);
m_vHandles.push_back(poEvent->hEvent);
mutex.Leave();
return(true);
}
const bool DetachEvent(QuickEvent* poEvent)
{
mutex.Enter();
std::vector<QuickEvent*>::iterator it1 = std::find(m_vEvents.begin(), m_vEvents.end(), poEvent);
m_vEvents.erase(it1);
std::vector<HANDLE>::iterator it2 = std::find(m_vHandles.begin(), m_vHandles.end(), poEvent->hEvent);
m_vHandles.erase(it2);
mutex.Leave();
return(true);
}
int WaitAnyUntilTimeout(const unsigned int milliseconds)
{
int index (-1);
mutex.Enter();
DWORD ret (WAIT_FAILED);
const unsigned int nHandles (m_vHandles.size());
if (nHandles > 0)
ret = WaitForMultipleObjects(nHandles, &m_vHandles[0], FALSE, milliseconds);
if (ret - WAIT_OBJECT_0 < nHandles)
index = (int)(ret - WAIT_OBJECT_0);
mutex.Leave();
return(index);
}
int WaitAny()
{
return(WaitAnyUntilTimeout(INFINITE));
}
int CheckAny()
{
return(WaitAnyUntilTimeout(0));
}
const bool WaitAllUntilTimeout(const unsigned int milliseconds)
{
bool waited (false);
mutex.Enter();
const unsigned int nHandles (m_vHandles.size());
if (nHandles > 0)
waited = (WAIT_TIMEOUT != WaitForMultipleObjects(nHandles, &m_vHandles[0], FALSE, milliseconds));
mutex.Leave();
return(waited);
}
const bool WaitAll()
{
return(WaitAllUntilTimeout(INFINITE));
}
QuickEvent* operator [] (const unsigned int index)
{
QuickEvent* poEvent (NULL);
mutex.Enter();
poEvent = m_vEvents[index];
mutex.Leave();
return(poEvent);
}
};
} //end of 'namespace libusbemu'
|