Skip to content

Commit b0f304b

Browse files
committed
Bit more proper closing sequence.
1 parent b351d89 commit b0f304b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/ftdi/abstract-ftd2xx-win32.cc

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ static void asyncCallbackTrampoline(uv_async_t* handle) {
6767
device->asyncCallback();
6868
}
6969

70-
static void asyncCloseCallback(uv_handle_t* handle) {}
70+
static void asyncCloseCallbackTrampoline(uv_handle_t* handle) {
71+
PCSX::FTDI::Device* device = reinterpret_cast<PCSX::FTDI::Device*>(handle->data);
72+
device->asyncCloseCallback();
73+
}
7174

7275
PCSX::FTDI::Device::Device() {
7376
uv_async_init(s_gui->loop(), &m_async, asyncCallbackTrampoline);
@@ -79,7 +82,9 @@ PCSX::FTDI::Device::~Device() {
7982
assert(!m_private->m_event);
8083
assert(!m_private->m_handle);
8184
delete m_private;
82-
uv_close(reinterpret_cast<uv_handle_t*>(&m_async), asyncCloseCallback);
85+
uv_loop_t* loop = m_async.loop;
86+
uv_close(reinterpret_cast<uv_handle_t*>(&m_async), asyncCloseCallbackTrampoline);
87+
while (!m_asyncClosed) uv_run(loop, UV_RUN_ONCE);
8388
}
8489

8590
void PCSX::FTDI::Device::open() {
@@ -216,6 +221,31 @@ void PCSX::FTDI::Devices::stopThread() {
216221

217222
bool PCSX::FTDI::Devices::isThreadRunning() { return s_threadRunning; }
218223

224+
void PCSX::FTDI::Devices::shutdown() {
225+
if (!isThreadRunning()) startThread();
226+
{
227+
bool run = true;
228+
while (run) {
229+
run = false;
230+
std::unique_lock<std::shared_mutex> guard(s_listLock);
231+
for (unsigned i = 0; i < s_numDevs; i++) {
232+
auto& device = s_devices[i];
233+
switch (device.m_private->m_state) {
234+
case Private::DeviceData::STATE_OPENED:
235+
device.close();
236+
case Private::DeviceData::STATE_CLOSE_PENDING:
237+
case Private::DeviceData::STATE_OPEN_PENDING:
238+
run = true;
239+
break;
240+
}
241+
}
242+
}
243+
}
244+
stopThread();
245+
delete[] s_devices;
246+
s_numDevs = 0;
247+
}
248+
219249
void PCSX::FTDI::Devices::setGUI(GUI* gui) { s_gui = gui; }
220250

221251
#endif

src/ftdi/abstract.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Device {
5555

5656
// technically private, but difficult to enforce properly
5757
void asyncCallback() {}
58+
void asyncCloseCallback() { m_asyncClosed = true; }
5859

5960
private:
6061
bool m_locked = false;
@@ -72,6 +73,7 @@ class Device {
7273
std::atomic_uint16_t m_slicesIndexes = 0;
7374

7475
uv_async_t m_async;
76+
bool m_asyncClosed = false;
7577

7678
unsigned usedSlices() {
7779
uint16_t indexes = m_slicesIndexes.load();
@@ -96,6 +98,7 @@ class Device {
9698
class Devices {
9799
public:
98100
static void scan();
101+
static void shutdown();
99102
static void iterate(std::function<bool(Device&)>);
100103
static bool isThreadRunning();
101104
static void startThread();

src/main/main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ int main(int argc, char **argv) {
227227
PCSX::g_emulator.m_gpu->shutdown();
228228
PCSX::g_emulator.m_cdrom->m_iso.shutdown();
229229

230+
PCSX::FTDI::Devices::shutdown();
231+
230232
s_gui->close();
231233

232234
delete s_gui;

0 commit comments

Comments
 (0)