From 142fb9792c38729ea5159f51e300018770a12fde Mon Sep 17 00:00:00 2001 From: sletz Date: Fri, 2 Nov 2007 22:12:49 +0000 Subject: [PATCH] Add JackWinSemaphore class. git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@1699 0c269be4-1314-0410-8aa9-9f06e86f4224 --- ChangeLog | 5 +- windows/JackWinEvent.h | 2 +- windows/JackWinSemaphore.cpp | 156 +++++++++++++++++++++++++++++++++++ windows/JackWinSemaphore.h | 69 ++++++++++++++++ 4 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 windows/JackWinSemaphore.cpp create mode 100644 windows/JackWinSemaphore.h diff --git a/ChangeLog b/ChangeLog index eb211412..c9ed6069 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,11 +14,12 @@ Andrzej Szombierski Jackdmp changes log --------------------------- -2007-11-01 Stephane Letz +2007-11-02 Stephane Letz * Correct ALSA driver Attach method: internal driver may have changed the buffer_size and sample_rate values. + * Add JackWinSemaphore class. -2007-10-31 Stephane Letz +2007-10-01 Stephane Letz * Server and user directory related code moved in a JackTools file. * Client name rewritting to remove path characters (used in fifo naming). diff --git a/windows/JackWinEvent.h b/windows/JackWinEvent.h index de717b4f..d29e08b4 100644 --- a/windows/JackWinEvent.h +++ b/windows/JackWinEvent.h @@ -31,7 +31,7 @@ namespace Jack //http://bob.developpez.com/tutapiwin/article_56.php /*! -\brief Inter process synchronization using system wide events. +\brief Inter process synchronization using system wide events. */ class JackWinEvent : public JackSynchro diff --git a/windows/JackWinSemaphore.cpp b/windows/JackWinSemaphore.cpp new file mode 100644 index 00000000..ae9e9115 --- /dev/null +++ b/windows/JackWinSemaphore.cpp @@ -0,0 +1,156 @@ +/* +Copyright (C) 2004-2005 Grame + +This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "JackWinSemaphore.h" +#include "JackError.h" + +namespace Jack +{ + +void JackWinSemaphore::BuildName(const char* name, const char* server_name, char* res) +{ + sprintf(res, "jack_pipe.%s_%s", server_name, name); +} + +bool JackWinSemaphore::Signal() +{ + BOOL res; + assert(fSemaphore); + + if (fFlush) + return true; + + if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) { + jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError()); + } + + return res; +} + +bool JackWinSemaphore::SignalAll() +{ + BOOL res; + assert(fSemaphore); + + if (fFlush) + return true; + + if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) { + jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError()); + } + + return res; +} + +bool JackWinSemaphore::Wait() +{ + DWORD res; + + if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) { + jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName); + } + + return (res == WAIT_OBJECT_0); +} + +bool JackWinSemaphore::TimedWait(long usec) +{ + DWORD res; + + if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) { + jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName); + } + + return (res == WAIT_OBJECT_0); +} + +// Client side : get the published semaphore from server +bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name) +{ + BuildName(name, server_name, fName); + JackLog("JackWinSemaphore::Connect %s\n", fName); + + // Temporary... + if (fSemaphore) { + JackLog("Already connected name = %s\n", name); + return true; + } + + if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) { + jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError()); + return false; + } else { + return true; + } +} + +bool JackWinSemaphore::Connect(const char* name, const char* server_name) +{ + return ConnectInput(name, server_name); +} + +bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name) +{ + return ConnectInput(name, server_name); +} + +bool JackWinSemaphore::Disconnect() +{ + if (fSemaphore) { + JackLog("JackWinSemaphore::Disconnect %s\n", fName); + CloseHandle(fSemaphore); + fSemaphore = NULL; + return true; + } else { + return false; + } +} + +bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value) +{ + BuildName(name, server_name, fName); + JackLog("JackWinSemaphore::Allocate name = %s val = %ld\n", fName, value); + + if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) { + jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError()); + return false; + } else if (GetLastError() == ERROR_ALREADY_EXISTS) { + jack_error("Allocate: named semaphore already exist name = %s", fName); + CloseHandle(fSemaphore); + fSemaphore = NULL; + return false; + } else { + return true; + } +} + +void JackWinSemaphore::Destroy() +{ + if (fSemaphore != NULL) { + JackLog("JackWinSemaphore::Destroy %s\n", fName); + CloseHandle(fSemaphore); + fSemaphore = NULL; + } else { + jack_error("JackWinSemaphore::Destroy synchro == NULL"); + } +} + + +} // end of namespace + diff --git a/windows/JackWinSemaphore.h b/windows/JackWinSemaphore.h new file mode 100644 index 00000000..f985b557 --- /dev/null +++ b/windows/JackWinSemaphore.h @@ -0,0 +1,69 @@ +/* +Copyright (C) 2004-2005 Grame + +This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __JackWinSemaphore__ +#define __JackWinSemaphore__ + +#include "JackSynchro.h" +#include +#include +#include + +namespace Jack +{ + +/*! +\brief Inter process synchronization using system wide semaphore. +*/ + +class JackWinSemaphore : public JackSynchro +{ + + private: + + HANDLE fSemaphore; + + protected: + + void BuildName(const char* name, const char* server_name, char* res); + + public: + + JackWinSemaphore(): JackSynchro(), fSemaphore(NULL) + {} + virtual ~JackWinSemaphore() + {} + + bool Signal(); + bool SignalAll(); + bool Wait(); + bool TimedWait(long usec); + + bool Allocate(const char* name, const char* server_name, int value); + bool Connect(const char* name, const char* server_name); + bool ConnectInput(const char* name, const char* server_name); + bool ConnectOutput(const char* name, const char* server_name); + bool Disconnect(); + void Destroy(); +}; + +} // end of namespace + +#endif +