Skip to content

Commit 7701e4f

Browse files
committed
Adding polling thread.
1 parent 20d51c3 commit 7701e4f

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/ftdi/abstract-ftd2xx-win32.cc

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020
#ifdef _WIN32
2121

2222
#define WIN32_LEAN_AND_MEAN
23+
24+
#include <assert.h>
25+
#include <windows.h>
26+
27+
#include <mutex>
28+
2329
#include "ftd2xx.h"
2430
#include "ftdi/abstract.h"
2531

2632
static std::vector<PCSX::FTDI::Device> s_devices;
2733

28-
void PCSX::FTDI::DeviceList::scan() {
34+
void PCSX::FTDI::Devices::scan() {
2935
FT_STATUS status;
3036
DWORD numDevs = 0;
3137

@@ -55,6 +61,40 @@ void PCSX::FTDI::DeviceList::scan() {
5561
delete[] nodes;
5662
}
5763

58-
const std::vector<PCSX::FTDI::Device>& PCSX::FTDI::DeviceList::get() { return s_devices; }
64+
const std::vector<PCSX::FTDI::Device>& PCSX::FTDI::Devices::get() { return s_devices; }
65+
66+
static HANDLE s_thread = nullptr;
67+
static HANDLE s_exitEvent = nullptr;
68+
static bool s_threadRunning = false;
69+
70+
static DWORD WINAPI threadProc(LPVOID parameter) {
71+
bool exitting = false;
72+
SetThreadDescription(GetCurrentThread(), L"abstract ftd2xx thread");
73+
while (!exitting) {
74+
HANDLE objects[1];
75+
objects[0] = s_exitEvent;
76+
DWORD idx = WaitForMultipleObjects(1, objects, FALSE, INFINITE);
77+
exitting = idx == WAIT_OBJECT_0;
78+
}
79+
CloseHandle(s_exitEvent);
80+
s_exitEvent = nullptr;
81+
s_threadRunning = false;
82+
return 0;
83+
}
84+
85+
void PCSX::FTDI::Devices::startThread() {
86+
assert(!s_threadRunning);
87+
s_exitEvent = CreateEvent(nullptr, FALSE, FALSE, L"abstract ftd2xx exit event");
88+
s_threadRunning = true;
89+
s_thread = CreateThread(nullptr, 0, threadProc, nullptr, 0, nullptr);
90+
}
91+
92+
void PCSX::FTDI::Devices::stopThread() {
93+
assert(s_threadRunning);
94+
SetEvent(s_exitEvent);
95+
WaitForSingleObject(s_thread, INFINITE);
96+
s_thread = nullptr;
97+
assert(!s_threadRunning);
98+
}
5999

60100
#endif

src/ftdi/abstract.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace PCSX {
2828

2929
namespace FTDI {
3030

31-
class DeviceList;
31+
class Devices;
3232
class Device {
3333
public:
3434
bool isLocked() const { return m_locked; }
@@ -49,13 +49,15 @@ class Device {
4949
std::string m_description = "";
5050
void* m_handle = nullptr;
5151

52-
friend class DeviceList;
52+
friend class Devices;
5353
};
5454

55-
class DeviceList {
55+
class Devices {
5656
public:
5757
static void scan();
5858
static const std::vector<Device>& get();
59+
static void startThread();
60+
static void stopThread();
5961
};
6062

6163
} // namespace FTDI

0 commit comments

Comments
 (0)