Skip to content

Commit ac950e7

Browse files
slompqdot
authored andcommitted
added QuickThread::Sleep() and QuickThread::Yield()
added QuickThread::Myself() to get a QuickThread object that operates on the calling thread. libusbemu has no platform-specific dependencies anymore: system-specific wrappers are provided through libusbemu_threads.h Signed-off-by: Marcos Paulo Berteli Slomp <[email protected]>
1 parent 439ee92 commit ac950e7

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "libusbemu_internal.h"
4444
#include <cassert>
4545
#include <algorithm>
46-
#include <windows.h>
4746
#include <conio.h>
4847
#include "freenect_internal.h"
4948

@@ -52,6 +51,10 @@ using namespace libusbemu;
5251
int libusb_init(libusb_context** context)
5352
{
5453
usb_init();
54+
const usb_version* version = usb_get_version();
55+
fprintf(stdout, "libusb-win32 version %d.%d.%d.%d (driver %d.%d.%d.%d)\n",
56+
version->dll.major, version->dll.minor, version->dll.micro, version->dll.nano,
57+
version->driver.major, version->driver.minor, version->driver.micro, version->driver.nano);
5558
// there is no such a thing like 'context' in libusb-0.1...
5659
// however, it is wise to emulate such context structure to localize and
5760
// keep track of any resource and/or internal data structures, as well as
@@ -382,11 +385,7 @@ int libusb_handle_events(libusb_context* ctx)
382385
// to alleviate sequence losses; however, even at such extreme conditions,
383386
// sequence losses still happen at frequent pace without ReapThreaded().
384387
if (ReapStrategy != ReapThreaded)
385-
{
386-
HANDLE hThread = GetCurrentThread();
387-
SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
388-
assert(THREAD_PRIORITY_TIME_CRITICAL == GetThreadPriority(hThread)); // paranoid...
389-
}
388+
QuickThread::Myself().RaisePriority();
390389

391390
//HANDLE hMyself (GetCurrentThread());
392391
// ctx->mutex.Enter();
@@ -451,7 +450,7 @@ int ReapSequential(const libusb_device& dev)
451450
if (NULL != wrapper)
452451
ReapTransfer(wrapper, 100);
453452
}
454-
//Sleep(1);
453+
//QuickThread::Yield();
455454
return(0);
456455
}
457456

@@ -560,7 +559,7 @@ int ReapThreaded(const libusb_device& dev)
560559
}
561560
}
562561
}
563-
Sleep(1);
562+
QuickThread::Yield();
564563
return(0);
565564
}
566565

platform/windows/libusb10emu/libusb-1.0/libusbemu_threads_win32.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ struct QuickThread
5858
}
5959
};
6060

61+
// allow the creation of pseudo-handles to the calling thread
62+
// this constructor cannot and should never be called explicitly!
63+
// use QuickThread::Myself() to spawn a pseudo-handle QuickThread
64+
inline QuickThread() : hThread(GetCurrentThread()) {}
65+
6166
public:
6267
template<typename F>
6368
inline QuickThread(F* proc, void* params) : hThread(NULL)
@@ -82,7 +87,14 @@ struct QuickThread
8287
inline ~QuickThread()
8388
{
8489
CloseHandle(hThread);
85-
fprintf(stdout, "Thread resources released.\n");
90+
// echo only if not a pseudo-handle...
91+
if (hThread != GetCurrentThread())
92+
fprintf(stdout, "Thread resources released.\n");
93+
}
94+
95+
static inline QuickThread Myself()
96+
{
97+
return(QuickThread());
8698
}
8799

88100
inline void Join()
@@ -98,12 +110,34 @@ struct QuickThread
98110
inline bool LowerPriority()
99111
{
100112
return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL));
101-
};
113+
}
102114

103115
inline bool RaisePriority()
104116
{
105117
return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL));
106-
};
118+
}
119+
120+
static inline void Sleep(int milliseconds)
121+
{
122+
::Sleep(milliseconds);
123+
}
124+
125+
// Yield is already a Win32 macro (WinBase.h)...
126+
// http://winapi.freetechsecrets.com/win32/WIN32Yield.htm
127+
#ifdef Yield
128+
#undef Yield
129+
#endif
130+
// A pragma push/pop could be used instead, but it does not solve the issues
131+
// http://stackoverflow.com/questions/1793800/can-i-redefine-a-c-macro-for-a-few-includes-and-then-define-it-back
132+
//#pragma push_macro("Yield")
133+
//#undef Yield
134+
static inline void Yield()
135+
{
136+
// Sleep(0) or Sleep(1) ?!
137+
// http://stackoverflow.com/questions/1413630/switchtothread-thread-yield-vs-thread-sleep0-vs-thead-sleep1
138+
::Sleep(1);
139+
}
140+
//#pragma pop_macro("Yield")
107141
};
108142

109143

0 commit comments

Comments
 (0)