From 852c9d13e51ea60230dd18c9a29c614973fda963 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 28 Jun 2019 20:32:32 +0200 Subject: Fix inclusion of semaphore.h is hardcoded to glibc non-multiarch - aka rename sempahore.h to sem.h --- drumgizmo/output/jackaudio.h | 2 +- plugin/Makefile.mingw32.in | 2 +- plugingui/Makefile.mingw32 | 2 +- src/Makefile.am | 4 +- src/audiocacheeventhandler.h | 2 +- src/drumkitloader.h | 2 +- src/sem.cc | 186 ++++++++++++++++++++++++++++++++++++++++++ src/sem.h | 49 ++++++++++++ src/semaphore.cc | 187 ------------------------------------------- src/semaphore.h | 49 ------------ test/Makefile.am | 8 +- test/semaphoretest.cc | 2 +- 12 files changed, 247 insertions(+), 248 deletions(-) create mode 100644 src/sem.cc create mode 100644 src/sem.h delete mode 100644 src/semaphore.cc delete mode 100644 src/semaphore.h diff --git a/drumgizmo/output/jackaudio.h b/drumgizmo/output/jackaudio.h index ce18f6f..52e6213 100644 --- a/drumgizmo/output/jackaudio.h +++ b/drumgizmo/output/jackaudio.h @@ -26,7 +26,7 @@ */ #pragma once #include -#include +#include #include "audiooutputengine.h" #include "../jackclient.h" diff --git a/plugin/Makefile.mingw32.in b/plugin/Makefile.mingw32.in index 610d5e9..50d6478 100644 --- a/plugin/Makefile.mingw32.in +++ b/plugin/Makefile.mingw32.in @@ -36,7 +36,7 @@ DG_SRC = \ @top_srcdir@/src/random.cc \ @top_srcdir@/src/sample.cc \ @top_srcdir@/src/sample_selection.cc \ - @top_srcdir@/src/semaphore.cc \ + @top_srcdir@/src/sem.cc \ @top_srcdir@/src/staminafilter.cc \ @top_srcdir@/src/thread.cc \ @top_srcdir@/src/velocityfilter.cc \ diff --git a/plugingui/Makefile.mingw32 b/plugingui/Makefile.mingw32 index afc43b8..df735d5 100644 --- a/plugingui/Makefile.mingw32 +++ b/plugingui/Makefile.mingw32 @@ -1,7 +1,7 @@ DG_SRC = \ ../src/configfile.cc \ ../src/thread.cc \ - ../src/semaphore.cc \ + ../src/sem.cc \ ../src/mutex.cc DG_CFLAGS = -I.. -I../src -DSSE -msse -msse2 diff --git a/src/Makefile.am b/src/Makefile.am index dec675d..b85e1d3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -38,7 +38,7 @@ nodist_libdg_la_SOURCES = \ random.cc \ sample.cc \ sample_selection.cc \ - semaphore.cc \ + sem.cc \ staminafilter.cc \ thread.cc \ velocityfilter.cc \ @@ -87,7 +87,7 @@ EXTRA_DIST = \ rangemap.h \ sample.h \ sample_selection.h \ - semaphore.h \ + sem.h \ settings.h \ staminafilter.h \ syncedsettings.h \ diff --git a/src/audiocacheeventhandler.h b/src/audiocacheeventhandler.h index e1e60a9..f80b4c8 100644 --- a/src/audiocacheeventhandler.h +++ b/src/audiocacheeventhandler.h @@ -31,7 +31,7 @@ #include #include "thread.h" -#include "semaphore.h" +#include "sem.h" #include "audiocachefile.h" #include "audiocacheidmanager.h" diff --git a/src/drumkitloader.h b/src/drumkitloader.h index e9d18c3..b6e0a8e 100644 --- a/src/drumkitloader.h +++ b/src/drumkitloader.h @@ -32,7 +32,7 @@ #include #include "thread.h" -#include "semaphore.h" +#include "sem.h" #include "drumkit.h" #include "settings.h" diff --git a/src/sem.cc b/src/sem.cc new file mode 100644 index 0000000..ad5266d --- /dev/null +++ b/src/sem.cc @@ -0,0 +1,186 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * sem.cc + * + * Sat Oct 8 17:44:13 CEST 2005 + * Copyright 2005 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "sem.h" + +#include +#include +#include +#include +#include +#include + +#include "platform.h" + +#if DG_PLATFORM != DG_PLATFORM_WINDOWS +#include +#include +#include +#include +#endif + +#if DG_PLATFORM == DG_PLATFORM_OSX +//#include +#include +#endif + +struct semaphore_private_t { +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + HANDLE semaphore; +#elif DG_PLATFORM == DG_PLATFORM_OSX + MPSemaphoreID semaphore; +#else + sem_t semaphore; +#endif +}; + +Semaphore::Semaphore(std::size_t initial_count) +{ + prv = new struct semaphore_private_t(); + +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + prv->semaphore = CreateSemaphore(nullptr, // default security attributes + initial_count, + std::numeric_limits::max(), + nullptr); // unnamed semaphore +#elif DG_PLATFORM == DG_PLATFORM_OSX + MPCreateSemaphore(std::numeric_limits::max(), + initial_count, + &prv->semaphore); +#else + const int pshared = 0; + memset(&prv->semaphore, 0, sizeof(sem_t)); + sem_init(&prv->semaphore, pshared, initial_count); +#endif +} + +Semaphore::~Semaphore() +{ +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + CloseHandle(prv->semaphore); +#elif DG_PLATFORM == DG_PLATFORM_OSX + MPDeleteSemaphore(prv->semaphore); +#else + sem_destroy(&prv->semaphore); +#endif + + delete prv; +} + +void Semaphore::post() +{ +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + ReleaseSemaphore(prv->semaphore, 1, nullptr); +#elif DG_PLATFORM == DG_PLATFORM_OSX + MPSignalSemaphore(prv->semaphore); +#else + sem_post(&prv->semaphore); +#endif +} + +bool Semaphore::wait(const std::chrono::milliseconds& timeout) +{ +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + DWORD ret = WaitForSingleObject(prv->semaphore, timeout.count()); + if(ret == WAIT_TIMEOUT) + { + return false; + } + + assert(ret == WAIT_OBJECT_0); +#elif DG_PLATFORM == DG_PLATFORM_OSX + auto ret = MPWaitOnSemaphore(prv->semaphore, + kDurationMillisecond * timeout.count()); + if(ret == kMPTimeoutErr) + { + return false; + } +#else + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + + // Add timeout + time_t seconds = (time_t)(timeout.count() / 1000); + ts.tv_sec += seconds; + ts.tv_nsec += (long)((timeout.count() % 1000) * 1000000); + + // Make sure we don't overflow the nanoseconds. + constexpr long nsec = 1000000000LL; + if(ts.tv_nsec >= nsec) + { + ts.tv_nsec -= nsec; + ts.tv_sec += 1; + } + +again: + int ret = sem_timedwait(&prv->semaphore, &ts); + if(ret < 0) + { + if(errno == EINTR) + { + // The timed wait were interrupted prematurely so we need to wait some + // more. To prevent an infinite loop-like behaviour we make a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + goto again; + } + + if(errno == ETIMEDOUT) + { + return false; + } + + perror("sem_timedwait()"); + assert(false); + } +#endif + + return true; +} + +void Semaphore::wait() +{ +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + WaitForSingleObject(prv->semaphore, INFINITE); +#elif DG_PLATFORM == DG_PLATFORM_OSX + MPWaitOnSemaphore(prv->semaphore, kDurationForever); +#else +again: + int ret = sem_wait(&prv->semaphore); + if(ret < 0) + { + if(errno == EINTR) + { + // The wait were interrupted prematurely so we need to wait again + // To prevent an infinite loop-like behaviour we make a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + goto again; + } + + perror("sem_wait()"); + assert(false); + } +#endif +} diff --git a/src/sem.h b/src/sem.h new file mode 100644 index 0000000..89f9d73 --- /dev/null +++ b/src/sem.h @@ -0,0 +1,49 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * sem.h + * + * Sat Oct 8 17:44:13 CEST 2005 + * Copyright 2005 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#pragma once + +#include + +struct semaphore_private_t; + +class Semaphore +{ +public: + Semaphore(std::size_t initial_count = 0); + ~Semaphore(); + + void post(); + + //! Lock semaphore with timeout. + //! \returns true if the semaphore was locked, false on timeout. + bool wait(const std::chrono::milliseconds& timeout); + + void wait(); + +private: + struct semaphore_private_t *prv{nullptr}; +}; diff --git a/src/semaphore.cc b/src/semaphore.cc deleted file mode 100644 index fdc33f1..0000000 --- a/src/semaphore.cc +++ /dev/null @@ -1,187 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * semaphore.cc - * - * Sat Oct 8 17:44:13 CEST 2005 - * Copyright 2005 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of DrumGizmo. - * - * DrumGizmo is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DrumGizmo 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with DrumGizmo; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "semaphore.h" - -#include -#include -#include -#include -#include -#include - -#include "platform.h" - -#if DG_PLATFORM != DG_PLATFORM_WINDOWS -// Make sure we don't include /this/ file's header... -#include <../include/semaphore.h> -#include -#include -#include -#endif - -#if DG_PLATFORM == DG_PLATFORM_OSX -//#include -#include -#endif - -struct semaphore_private_t { -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - HANDLE semaphore; -#elif DG_PLATFORM == DG_PLATFORM_OSX - MPSemaphoreID semaphore; -#else - sem_t semaphore; -#endif -}; - -Semaphore::Semaphore(std::size_t initial_count) -{ - prv = new struct semaphore_private_t(); - -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - prv->semaphore = CreateSemaphore(nullptr, // default security attributes - initial_count, - std::numeric_limits::max(), - nullptr); // unnamed semaphore -#elif DG_PLATFORM == DG_PLATFORM_OSX - MPCreateSemaphore(std::numeric_limits::max(), - initial_count, - &prv->semaphore); -#else - const int pshared = 0; - memset(&prv->semaphore, 0, sizeof(sem_t)); - sem_init(&prv->semaphore, pshared, initial_count); -#endif -} - -Semaphore::~Semaphore() -{ -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - CloseHandle(prv->semaphore); -#elif DG_PLATFORM == DG_PLATFORM_OSX - MPDeleteSemaphore(prv->semaphore); -#else - sem_destroy(&prv->semaphore); -#endif - - delete prv; -} - -void Semaphore::post() -{ -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - ReleaseSemaphore(prv->semaphore, 1, nullptr); -#elif DG_PLATFORM == DG_PLATFORM_OSX - MPSignalSemaphore(prv->semaphore); -#else - sem_post(&prv->semaphore); -#endif -} - -bool Semaphore::wait(const std::chrono::milliseconds& timeout) -{ -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - DWORD ret = WaitForSingleObject(prv->semaphore, timeout.count()); - if(ret == WAIT_TIMEOUT) - { - return false; - } - - assert(ret == WAIT_OBJECT_0); -#elif DG_PLATFORM == DG_PLATFORM_OSX - auto ret = MPWaitOnSemaphore(prv->semaphore, - kDurationMillisecond * timeout.count()); - if(ret == kMPTimeoutErr) - { - return false; - } -#else - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - - // Add timeout - time_t seconds = (time_t)(timeout.count() / 1000); - ts.tv_sec += seconds; - ts.tv_nsec += (long)((timeout.count() % 1000) * 1000000); - - // Make sure we don't overflow the nanoseconds. - constexpr long nsec = 1000000000LL; - if(ts.tv_nsec >= nsec) - { - ts.tv_nsec -= nsec; - ts.tv_sec += 1; - } - -again: - int ret = sem_timedwait(&prv->semaphore, &ts); - if(ret < 0) - { - if(errno == EINTR) - { - // The timed wait were interrupted prematurely so we need to wait some - // more. To prevent an infinite loop-like behaviour we make a short sleep. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - goto again; - } - - if(errno == ETIMEDOUT) - { - return false; - } - - perror("sem_timedwait()"); - assert(false); - } -#endif - - return true; -} - -void Semaphore::wait() -{ -#if DG_PLATFORM == DG_PLATFORM_WINDOWS - WaitForSingleObject(prv->semaphore, INFINITE); -#elif DG_PLATFORM == DG_PLATFORM_OSX - MPWaitOnSemaphore(prv->semaphore, kDurationForever); -#else -again: - int ret = sem_wait(&prv->semaphore); - if(ret < 0) - { - if(errno == EINTR) - { - // The wait were interrupted prematurely so we need to wait again - // To prevent an infinite loop-like behaviour we make a short sleep. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - goto again; - } - - perror("sem_wait()"); - assert(false); - } -#endif -} diff --git a/src/semaphore.h b/src/semaphore.h deleted file mode 100644 index d5995cb..0000000 --- a/src/semaphore.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * semaphore.h - * - * Sat Oct 8 17:44:13 CEST 2005 - * Copyright 2005 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of DrumGizmo. - * - * DrumGizmo is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DrumGizmo 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with DrumGizmo; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#pragma once - -#include - -struct semaphore_private_t; - -class Semaphore -{ -public: - Semaphore(std::size_t initial_count = 0); - ~Semaphore(); - - void post(); - - //! Lock semaphore with timeout. - //! \returns true if the semaphore was locked, false on timeout. - bool wait(const std::chrono::milliseconds& timeout); - - void wait(); - -private: - struct semaphore_private_t *prv{nullptr}; -}; diff --git a/test/Makefile.am b/test/Makefile.am index 80ff73a..056c111 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -44,7 +44,7 @@ audiocache_SOURCES = \ $(top_srcdir)/src/audiocachefile.cc \ $(top_srcdir)/src/audiocacheidmanager.cc \ $(top_srcdir)/src/thread.cc \ - $(top_srcdir)/src/semaphore.cc \ + $(top_srcdir)/src/sem.cc \ $(top_srcdir)/src/audiofile.cc \ $(top_srcdir)/src/random.cc \ dgtest.cc \ @@ -58,7 +58,7 @@ audiocachefile_LDFLAGS = $(PTHREAD_LIBS) $(SNDFILE_LIBS) audiocachefile_SOURCES = \ $(top_srcdir)/src/audiocachefile.cc \ $(top_srcdir)/src/thread.cc \ - $(top_srcdir)/src/semaphore.cc \ + $(top_srcdir)/src/sem.cc \ $(top_srcdir)/src/audiofile.cc \ $(top_srcdir)/src/random.cc \ dgtest.cc \ @@ -85,7 +85,7 @@ audiocacheeventhandler_SOURCES = \ $(top_srcdir)/src/audiocacheidmanager.cc \ $(top_srcdir)/src/audiocachefile.cc \ $(top_srcdir)/src/thread.cc \ - $(top_srcdir)/src/semaphore.cc \ + $(top_srcdir)/src/sem.cc \ dgtest.cc \ audiocacheeventhandlertest.cc @@ -165,7 +165,7 @@ semaphoretest_CXXFLAGS = -DOUTPUT=\"semaphoretest\" \ semaphoretest_LDFLAGS = $(PTHREAD_LIBS) semaphoretest_SOURCES = \ $(top_srcdir)/hugin/hugin.c \ - $(top_srcdir)/src/semaphore.cc \ + $(top_srcdir)/src/sem.cc \ semaphoretest.cc \ dgtest.cc diff --git a/test/semaphoretest.cc b/test/semaphoretest.cc index c99e9a6..470b5a0 100644 --- a/test/semaphoretest.cc +++ b/test/semaphoretest.cc @@ -31,7 +31,7 @@ #include #include -#include "../src/semaphore.h" +#include "../src/sem.h" std::chrono::nanoseconds dist(const std::chrono::duration& a, const std::chrono::duration& b) -- cgit v1.2.3