From 8e4ee25c7f9e6a8a18b25fbe6e9c5c2f43c40a93 Mon Sep 17 00:00:00 2001 From: Devin Anderson Date: Tue, 29 Mar 2011 11:27:21 -0700 Subject: [PATCH] Make thread termination more elegant. --- windows/winmme/JackWinMMEOutputPort.cpp | 52 +++++++++++++++++-------- windows/winmme/JackWinMMEOutputPort.h | 3 ++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/windows/winmme/JackWinMMEOutputPort.cpp b/windows/winmme/JackWinMMEOutputPort.cpp index 3e615742..aa87de0f 100644 --- a/windows/winmme/JackWinMMEOutputPort.cpp +++ b/windows/winmme/JackWinMMEOutputPort.cpp @@ -247,10 +247,7 @@ JackWinMMEOutputPort::HandleMessage(UINT message, DWORD_PTR param1, jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device closed."); break; case MOM_DONE: - if (! ReleaseSemaphore(sysex_semaphore, 1, NULL)) { - WriteOSError("JackWinMMEOutputPort::HandleMessage", - "ReleaseSemaphore"); - } + Signal(sysex_semaphore); break; case MOM_OPEN: jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device opened."); @@ -294,14 +291,21 @@ JackWinMMEOutputPort::ProcessJack(JackMidiBuffer *port_buffer, event->size); break; default: - if (! ReleaseSemaphore(thread_queue_semaphore, 1, NULL)) { - WriteOSError("JackWinMMEOutputPort::ProcessJack", - "ReleaseSemaphore"); - } + Signal(thread_queue_semaphore); } } } +bool +JackWinMMEOutputPort::Signal(Handle semaphore) +{ + bool result = (bool) ReleaseSemaphore(semaphore, 1, NULL); + if (! result) { + WriteOSError("JackWinMMEOutputPort::Signal", "ReleaseSemaphore"); + } + return result; +} + bool JackWinMMEOutputPort::Start() { @@ -319,15 +323,31 @@ JackWinMMEOutputPort::Start() bool JackWinMMEOutputPort::Stop() { - bool result = thread->GetStatus() == JackThread::kIdle; - if (! result) { - result = ! thread->Kill(); - if (! result) { - jack_error("JackWinMMEOutputPort::Stop - failed to stop MIDI " - "processing thread."); - } + + jack_info("JackWinMMEOutputPort::Stop - stopping MIDI output port " + "processing thread."); + + int result; + const char *verb; + switch (thread->GetStatus()) { + case JackThread::kIniting: + case JackThread::kStarting: + result = thread->Kill(); + verb = "kill"; + break; + case JackThread::kRunning: + Signal(thread_queue_semaphore); + result = thread->Stop(); + verb = "stop"; + break; + default: + return true; } - return result; + if (result) { + jack_error("JackWinMMEOutputPort::Stop - could not %s MIDI processing " + "thread.", verb); + } + return ! result; } bool diff --git a/windows/winmme/JackWinMMEOutputPort.h b/windows/winmme/JackWinMMEOutputPort.h index ef1bd65d..1033ec3e 100644 --- a/windows/winmme/JackWinMMEOutputPort.h +++ b/windows/winmme/JackWinMMEOutputPort.h @@ -46,6 +46,9 @@ namespace Jack { void HandleMessage(UINT message, DWORD_PTR param1, DWORD_PTR param2); + bool + Signal(Handle semaphore); + bool Wait(Handle semaphore);