From 6cd3d2414ac40a94713c84a0703d3e021bf9a570 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 27 May 2023 08:49:48 -0700 Subject: Use [[maybe_unused]] instead of std::ignore --- common/threads.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'common/threads.cpp') diff --git a/common/threads.cpp b/common/threads.cpp index 19a6bbf0..136c4813 100644 --- a/common/threads.cpp +++ b/common/threads.cpp @@ -97,7 +97,6 @@ bool semaphore::try_wait() noexcept #ifdef HAVE_PTHREAD_NP_H #include #endif -#include namespace { @@ -106,33 +105,27 @@ using setname_t2 = int(*)(pthread_t, const char*); using setname_t3 = void(*)(pthread_t, const char*); using setname_t4 = int(*)(pthread_t, const char*, void*); -void setname_caller(setname_t1 func, const char *name) +[[maybe_unused]] void setname_caller(setname_t1 func, const char *name) { func(name); } -void setname_caller(setname_t2 func, const char *name) +[[maybe_unused]] void setname_caller(setname_t2 func, const char *name) { func(pthread_self(), name); } -void setname_caller(setname_t3 func, const char *name) +[[maybe_unused]] void setname_caller(setname_t3 func, const char *name) { func(pthread_self(), name); } -void setname_caller(setname_t4 func, const char *name) +[[maybe_unused]] void setname_caller(setname_t4 func, const char *name) { func(pthread_self(), "%s", static_cast(const_cast(name))); } } // namespace -void althrd_setname(const char *name) +void althrd_setname(const char *name [[maybe_unused]]) { #if defined(HAVE_PTHREAD_SET_NAME_NP) setname_caller(pthread_set_name_np, name); #elif defined(HAVE_PTHREAD_SETNAME_NP) setname_caller(pthread_setname_np, name); #endif - /* Avoid unused function/parameter warnings. */ - std::ignore = name; - std::ignore = static_cast(&setname_caller); - std::ignore = static_cast(&setname_caller); - std::ignore = static_cast(&setname_caller); - std::ignore = static_cast(&setname_caller); } #ifdef __APPLE__ -- cgit v1.2.3 From cd781b1511d437816aac65f89646bd80dbf7c040 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Sun, 28 May 2023 14:28:00 +0800 Subject: threads: do not use libdispatch where it is not present (#851) Fixes: https://github.com/kcat/openal-soft/issues/850 --- common/threads.cpp | 3 ++- common/threads.h | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'common/threads.cpp') diff --git a/common/threads.cpp b/common/threads.cpp index 136c4813..76a13d9d 100644 --- a/common/threads.cpp +++ b/common/threads.cpp @@ -128,7 +128,8 @@ void althrd_setname(const char *name [[maybe_unused]]) #endif } -#ifdef __APPLE__ +/* Do not try using libdispatch on systems where it is absent. */ +#if defined(__APPLE__) && ((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) namespace al { diff --git a/common/threads.h b/common/threads.h index 59fccd12..2592e5b0 100644 --- a/common/threads.h +++ b/common/threads.h @@ -15,7 +15,12 @@ #endif #if defined(__APPLE__) +#include +#if (MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__) #include +#else +#include /* Fallback option for Apple without a working libdispatch */ +#endif #elif !defined(_WIN32) #include #endif @@ -27,7 +32,7 @@ namespace al { class semaphore { #ifdef _WIN32 using native_type = void*; -#elif defined(__APPLE__) +#elif defined(__APPLE__) && ((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) using native_type = dispatch_semaphore_t; #else using native_type = sem_t; -- cgit v1.2.3 From a91ac01004332d59370c5d6210e1605cb8f708c6 Mon Sep 17 00:00:00 2001 From: "Deal(一线灵)" Date: Fri, 2 Jun 2023 07:31:37 +0800 Subject: Fix ios crash at sem_init fail with errno=78(function not implemented) (#855) --- common/threads.cpp | 2 +- common/threads.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'common/threads.cpp') diff --git a/common/threads.cpp b/common/threads.cpp index 76a13d9d..c1d9e8b0 100644 --- a/common/threads.cpp +++ b/common/threads.cpp @@ -129,7 +129,7 @@ void althrd_setname(const char *name [[maybe_unused]]) } /* Do not try using libdispatch on systems where it is absent. */ -#if defined(__APPLE__) && ((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) +#if defined(AL_APPLE_HAVE_DISPATCH) namespace al { diff --git a/common/threads.h b/common/threads.h index 2592e5b0..62d80828 100644 --- a/common/threads.h +++ b/common/threads.h @@ -16,8 +16,10 @@ #if defined(__APPLE__) #include -#if (MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__) +#include +#if (((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) || TARGET_OS_IOS || TARGET_OS_TV) #include +#define AL_APPLE_HAVE_DISPATCH 1 #else #include /* Fallback option for Apple without a working libdispatch */ #endif @@ -32,7 +34,7 @@ namespace al { class semaphore { #ifdef _WIN32 using native_type = void*; -#elif defined(__APPLE__) && ((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) +#elif defined(AL_APPLE_HAVE_DISPATCH) using native_type = dispatch_semaphore_t; #else using native_type = sem_t; -- cgit v1.2.3 From c200eb73a7cf47dbd1a4a3785be471adfb3513d8 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 1 Jun 2023 17:31:31 -0700 Subject: Move althrd_setname to its own source --- CMakeLists.txt | 2 ++ alc/backends/alsa.cpp | 2 +- alc/backends/dsound.cpp | 2 +- alc/backends/jack.cpp | 1 + alc/backends/null.cpp | 4 +-- alc/backends/opensl.cpp | 1 + alc/backends/oss.cpp | 2 +- alc/backends/sndio.cpp | 2 +- alc/backends/solaris.cpp | 2 +- alc/backends/wasapi.cpp | 2 +- alc/backends/wave.cpp | 2 +- alc/backends/winmm.cpp | 1 + common/althrd_setname.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++ common/althrd_setname.h | 6 ++++ common/threads.cpp | 66 ---------------------------------------- common/threads.h | 2 -- 16 files changed, 96 insertions(+), 77 deletions(-) create mode 100644 common/althrd_setname.cpp create mode 100644 common/althrd_setname.h (limited to 'common/threads.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index e4d3776d..63a213c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -621,6 +621,8 @@ set(COMMON_OBJS common/alspan.h common/alstring.cpp common/alstring.h + common/althrd_setname.cpp + common/althrd_setname.h common/altraits.h common/atomic.h common/comptr.h diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp index ce368f5e..83eef183 100644 --- a/alc/backends/alsa.cpp +++ b/alc/backends/alsa.cpp @@ -41,12 +41,12 @@ #include "alc/alconfig.h" #include "almalloc.h" #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "ringbuffer.h" -#include "threads.h" #include diff --git a/alc/backends/dsound.cpp b/alc/backends/dsound.cpp index 5fc8a1c7..54fac898 100644 --- a/alc/backends/dsound.cpp +++ b/alc/backends/dsound.cpp @@ -47,6 +47,7 @@ #include "albit.h" #include "alnumeric.h" #include "alspan.h" +#include "althrd_setname.h" #include "comptr.h" #include "core/device.h" #include "core/helpers.h" @@ -54,7 +55,6 @@ #include "dynload.h" #include "ringbuffer.h" #include "strutils.h" -#include "threads.h" /* MinGW-w64 needs this for some unknown reason now. */ using LPCWAVEFORMATEX = const WAVEFORMATEX*; diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp index 9e023e21..b8325b05 100644 --- a/alc/backends/jack.cpp +++ b/alc/backends/jack.cpp @@ -35,6 +35,7 @@ #include "albit.h" #include "alc/alconfig.h" #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" diff --git a/alc/backends/null.cpp b/alc/backends/null.cpp index 5a8fc255..73420ad3 100644 --- a/alc/backends/null.cpp +++ b/alc/backends/null.cpp @@ -30,10 +30,10 @@ #include #include -#include "core/device.h" +#include "althrd_setname.h" #include "almalloc.h" +#include "core/device.h" #include "core/helpers.h" -#include "threads.h" namespace { diff --git a/alc/backends/opensl.cpp b/alc/backends/opensl.cpp index c9053816..a3b70791 100644 --- a/alc/backends/opensl.cpp +++ b/alc/backends/opensl.cpp @@ -35,6 +35,7 @@ #include "albit.h" #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" diff --git a/alc/backends/oss.cpp b/alc/backends/oss.cpp index dc18c4c3..554ccc24 100644 --- a/alc/backends/oss.cpp +++ b/alc/backends/oss.cpp @@ -43,11 +43,11 @@ #include "alc/alconfig.h" #include "almalloc.h" #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" -#include "threads.h" #include diff --git a/alc/backends/sndio.cpp b/alc/backends/sndio.cpp index 2040dd3a..84c54c62 100644 --- a/alc/backends/sndio.cpp +++ b/alc/backends/sndio.cpp @@ -32,11 +32,11 @@ #include #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" -#include "threads.h" #include diff --git a/alc/backends/solaris.cpp b/alc/backends/solaris.cpp index ae87e7eb..15dcc98f 100644 --- a/alc/backends/solaris.cpp +++ b/alc/backends/solaris.cpp @@ -41,10 +41,10 @@ #include #include "alc/alconfig.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" -#include "threads.h" #include diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp index 32a4334d..2b88aa96 100644 --- a/alc/backends/wasapi.cpp +++ b/alc/backends/wasapi.cpp @@ -61,6 +61,7 @@ #include "alc/events.h" #include "alnumeric.h" #include "alspan.h" +#include "althrd_setname.h" #include "comptr.h" #include "core/converter.h" #include "core/device.h" @@ -68,7 +69,6 @@ #include "core/logging.h" #include "ringbuffer.h" #include "strutils.h" -#include "threads.h" #if defined(ALSOFT_UWP) #include diff --git a/alc/backends/wave.cpp b/alc/backends/wave.cpp index 1ee2fe51..40592ee7 100644 --- a/alc/backends/wave.cpp +++ b/alc/backends/wave.cpp @@ -38,12 +38,12 @@ #include "alc/alconfig.h" #include "almalloc.h" #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "opthelpers.h" #include "strutils.h" -#include "threads.h" namespace { diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index 0345fe10..fa5bf22a 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -39,6 +39,7 @@ #include #include "alnumeric.h" +#include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" diff --git a/common/althrd_setname.cpp b/common/althrd_setname.cpp new file mode 100644 index 00000000..22d33092 --- /dev/null +++ b/common/althrd_setname.cpp @@ -0,0 +1,76 @@ + +#include "config.h" + +#include "althrd_setname.h" + + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include + +void althrd_setname(const char *name [[maybe_unused]]) +{ +#if defined(_MSC_VER) && !defined(_M_ARM) + +#define MS_VC_EXCEPTION 0x406D1388 +#pragma pack(push,8) + struct { + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. + } info; +#pragma pack(pop) + info.dwType = 0x1000; + info.szName = name; + info.dwThreadID = ~DWORD{0}; + info.dwFlags = 0; + + /* FIXME: How to do this on MinGW? */ + __try { + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except(EXCEPTION_CONTINUE_EXECUTION) { + } +#undef MS_VC_EXCEPTION +#endif +} + +#else + +#include +#ifdef HAVE_PTHREAD_NP_H +#include +#endif + +namespace { + +using setname_t1 = int(*)(const char*); +using setname_t2 = int(*)(pthread_t, const char*); +using setname_t3 = void(*)(pthread_t, const char*); +using setname_t4 = int(*)(pthread_t, const char*, void*); + +[[maybe_unused]] void setname_caller(setname_t1 func, const char *name) +{ func(name); } + +[[maybe_unused]] void setname_caller(setname_t2 func, const char *name) +{ func(pthread_self(), name); } + +[[maybe_unused]] void setname_caller(setname_t3 func, const char *name) +{ func(pthread_self(), name); } + +[[maybe_unused]] void setname_caller(setname_t4 func, const char *name) +{ func(pthread_self(), "%s", static_cast(const_cast(name))); } + +} // namespace + +void althrd_setname(const char *name [[maybe_unused]]) +{ +#if defined(HAVE_PTHREAD_SET_NAME_NP) + setname_caller(pthread_set_name_np, name); +#elif defined(HAVE_PTHREAD_SETNAME_NP) + setname_caller(pthread_setname_np, name); +#endif +} + +#endif diff --git a/common/althrd_setname.h b/common/althrd_setname.h new file mode 100644 index 00000000..0e22c0a9 --- /dev/null +++ b/common/althrd_setname.h @@ -0,0 +1,6 @@ +#ifndef COMMON_ALTHRD_SETNAME_H +#define COMMON_ALTHRD_SETNAME_H + +void althrd_setname(const char *name); + +#endif /* COMMON_ALTHRD_SETNAME_H */ diff --git a/common/threads.cpp b/common/threads.cpp index c1d9e8b0..c176f5af 100644 --- a/common/threads.cpp +++ b/common/threads.cpp @@ -32,37 +32,6 @@ #include -void althrd_setname(const char *name) -{ -#if defined(_MSC_VER) && !defined(_M_ARM) - -#define MS_VC_EXCEPTION 0x406D1388 -#pragma pack(push,8) - struct { - DWORD dwType; // Must be 0x1000. - LPCSTR szName; // Pointer to name (in user addr space). - DWORD dwThreadID; // Thread ID (-1=caller thread). - DWORD dwFlags; // Reserved for future use, must be zero. - } info; -#pragma pack(pop) - info.dwType = 0x1000; - info.szName = name; - info.dwThreadID = ~DWORD{0}; - info.dwFlags = 0; - - __try { - RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); - } - __except(EXCEPTION_CONTINUE_EXECUTION) { - } -#undef MS_VC_EXCEPTION - -#else - - (void)name; -#endif -} - namespace al { semaphore::semaphore(unsigned int initial) @@ -93,41 +62,6 @@ bool semaphore::try_wait() noexcept #else -#include -#ifdef HAVE_PTHREAD_NP_H -#include -#endif - -namespace { - -using setname_t1 = int(*)(const char*); -using setname_t2 = int(*)(pthread_t, const char*); -using setname_t3 = void(*)(pthread_t, const char*); -using setname_t4 = int(*)(pthread_t, const char*, void*); - -[[maybe_unused]] void setname_caller(setname_t1 func, const char *name) -{ func(name); } - -[[maybe_unused]] void setname_caller(setname_t2 func, const char *name) -{ func(pthread_self(), name); } - -[[maybe_unused]] void setname_caller(setname_t3 func, const char *name) -{ func(pthread_self(), name); } - -[[maybe_unused]] void setname_caller(setname_t4 func, const char *name) -{ func(pthread_self(), "%s", static_cast(const_cast(name))); } - -} // namespace - -void althrd_setname(const char *name [[maybe_unused]]) -{ -#if defined(HAVE_PTHREAD_SET_NAME_NP) - setname_caller(pthread_set_name_np, name); -#elif defined(HAVE_PTHREAD_SETNAME_NP) - setname_caller(pthread_setname_np, name); -#endif -} - /* Do not try using libdispatch on systems where it is absent. */ #if defined(AL_APPLE_HAVE_DISPATCH) diff --git a/common/threads.h b/common/threads.h index 1ef037bb..703d50d4 100644 --- a/common/threads.h +++ b/common/threads.h @@ -14,8 +14,6 @@ #include #endif -void althrd_setname(const char *name); - namespace al { class semaphore { -- cgit v1.2.3 From 2b7ab0b75086f3d73a7ffe9bc05a80e5d9c625f5 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 1 Jun 2023 18:16:17 -0700 Subject: Rename threads.cpp/h to alsem.cpp/h --- CMakeLists.txt | 4 +- al/debug.cpp | 1 - al/event.cpp | 1 - al/source.cpp | 1 - alc/alc.cpp | 1 - alc/alu.cpp | 1 - alc/backends/jack.cpp | 2 +- alc/backends/opensl.cpp | 2 +- alc/backends/winmm.cpp | 2 +- alc/context.cpp | 1 - alc/events.cpp | 1 - common/alsem.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ common/alsem.h | 43 +++++++++++++++++ common/threads.cpp | 125 ----------------------------------------------- common/threads.h | 43 ----------------- core/context.h | 2 +- 16 files changed, 175 insertions(+), 181 deletions(-) create mode 100644 common/alsem.cpp create mode 100644 common/alsem.h delete mode 100644 common/threads.cpp delete mode 100644 common/threads.h (limited to 'common/threads.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 63a213c9..c73dee55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -618,6 +618,8 @@ set(COMMON_OBJS common/almalloc.h common/alnumbers.h common/alnumeric.h + common/alsem.cpp + common/alsem.h common/alspan.h common/alstring.cpp common/alstring.h @@ -638,8 +640,6 @@ set(COMMON_OBJS common/ringbuffer.h common/strutils.cpp common/strutils.h - common/threads.cpp - common/threads.h common/vecmat.h common/vector.h) diff --git a/al/debug.cpp b/al/debug.cpp index 56705c65..af67859a 100644 --- a/al/debug.cpp +++ b/al/debug.cpp @@ -20,7 +20,6 @@ #include "core/logging.h" #include "direct_defs.h" #include "opthelpers.h" -#include "threads.h" namespace { diff --git a/al/event.cpp b/al/event.cpp index a5b5fdc5..ef58f86d 100644 --- a/al/event.cpp +++ b/al/event.cpp @@ -30,7 +30,6 @@ #include "direct_defs.h" #include "opthelpers.h" #include "ringbuffer.h" -#include "threads.h" namespace { diff --git a/al/source.cpp b/al/source.cpp index 8dbbbcd8..8f4d4d48 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -73,7 +73,6 @@ #include "filter.h" #include "opthelpers.h" #include "ringbuffer.h" -#include "threads.h" #ifdef ALSOFT_EAX #include diff --git a/alc/alc.cpp b/alc/alc.cpp index 128b3d7e..48fa19ec 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -103,7 +103,6 @@ #include "intrusive_ptr.h" #include "opthelpers.h" #include "strutils.h" -#include "threads.h" #include "backends/base.h" #include "backends/null.h" diff --git a/alc/alu.cpp b/alc/alu.cpp index c43da639..0130f280 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -77,7 +77,6 @@ #include "opthelpers.h" #include "ringbuffer.h" #include "strutils.h" -#include "threads.h" #include "vecmat.h" #include "vector.h" diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp index b8325b05..66fc0877 100644 --- a/alc/backends/jack.cpp +++ b/alc/backends/jack.cpp @@ -35,13 +35,13 @@ #include "albit.h" #include "alc/alconfig.h" #include "alnumeric.h" +#include "alsem.h" #include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "ringbuffer.h" -#include "threads.h" #include #include diff --git a/alc/backends/opensl.cpp b/alc/backends/opensl.cpp index a3b70791..2a161056 100644 --- a/alc/backends/opensl.cpp +++ b/alc/backends/opensl.cpp @@ -35,13 +35,13 @@ #include "albit.h" #include "alnumeric.h" +#include "alsem.h" #include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "opthelpers.h" #include "ringbuffer.h" -#include "threads.h" #include #include diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index fa5bf22a..661585cd 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -39,13 +39,13 @@ #include #include "alnumeric.h" +#include "alsem.h" #include "althrd_setname.h" #include "core/device.h" #include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" #include "strutils.h" -#include "threads.h" #ifndef WAVE_FORMAT_IEEE_FLOAT #define WAVE_FORMAT_IEEE_FLOAT 0x0003 diff --git a/alc/context.cpp b/alc/context.cpp index bcd72f92..142ad50c 100644 --- a/alc/context.cpp +++ b/alc/context.cpp @@ -33,7 +33,6 @@ #include "core/voice_change.h" #include "device.h" #include "ringbuffer.h" -#include "threads.h" #include "vecmat.h" #ifdef ALSOFT_EAX diff --git a/alc/events.cpp b/alc/events.cpp index b5b65cb1..b14b1a8d 100644 --- a/alc/events.cpp +++ b/alc/events.cpp @@ -6,7 +6,6 @@ #include #include "alspan.h" -#include "common/threads.h" #include "core/logging.h" #include "device.h" diff --git a/common/alsem.cpp b/common/alsem.cpp new file mode 100644 index 00000000..6a92b35c --- /dev/null +++ b/common/alsem.cpp @@ -0,0 +1,126 @@ +/** + * OpenAL cross platform audio library + * Copyright (C) 1999-2007 by authors. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * Or go to http://www.gnu.org/copyleft/lgpl.html + */ + +#include "config.h" + +#include "alsem.h" + +#include + +#include "opthelpers.h" + + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include + +#include + +namespace al { + +semaphore::semaphore(unsigned int initial) +{ + if(initial > static_cast(std::numeric_limits::max())) + throw std::system_error(std::make_error_code(std::errc::value_too_large)); + mSem = CreateSemaphore(nullptr, initial, std::numeric_limits::max(), nullptr); + if(mSem == nullptr) + throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); +} + +semaphore::~semaphore() +{ CloseHandle(mSem); } + +void semaphore::post() +{ + if(!ReleaseSemaphore(static_cast(mSem), 1, nullptr)) + throw std::system_error(std::make_error_code(std::errc::value_too_large)); +} + +void semaphore::wait() noexcept +{ WaitForSingleObject(static_cast(mSem), INFINITE); } + +bool semaphore::try_wait() noexcept +{ return WaitForSingleObject(static_cast(mSem), 0) == WAIT_OBJECT_0; } + +} // namespace al + +#else + +/* Do not try using libdispatch on systems where it is absent. */ +#if defined(AL_APPLE_HAVE_DISPATCH) + +namespace al { + +semaphore::semaphore(unsigned int initial) +{ + mSem = dispatch_semaphore_create(initial); + if(!mSem) + throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); +} + +semaphore::~semaphore() +{ dispatch_release(mSem); } + +void semaphore::post() +{ dispatch_semaphore_signal(mSem); } + +void semaphore::wait() noexcept +{ dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); } + +bool semaphore::try_wait() noexcept +{ return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; } + +} // namespace al + +#else /* !__APPLE__ */ + +#include + +namespace al { + +semaphore::semaphore(unsigned int initial) +{ + if(sem_init(&mSem, 0, initial) != 0) + throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); +} + +semaphore::~semaphore() +{ sem_destroy(&mSem); } + +void semaphore::post() +{ + if(sem_post(&mSem) != 0) + throw std::system_error(std::make_error_code(std::errc::value_too_large)); +} + +void semaphore::wait() noexcept +{ + while(sem_wait(&mSem) == -1 && errno == EINTR) { + } +} + +bool semaphore::try_wait() noexcept +{ return sem_trywait(&mSem) == 0; } + +} // namespace al + +#endif /* __APPLE__ */ + +#endif /* _WIN32 */ diff --git a/common/alsem.h b/common/alsem.h new file mode 100644 index 00000000..9f72d1c6 --- /dev/null +++ b/common/alsem.h @@ -0,0 +1,43 @@ +#ifndef COMMON_ALSEM_H +#define COMMON_ALSEM_H + +#if defined(__APPLE__) +#include +#include +#if (((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) || TARGET_OS_IOS || TARGET_OS_TV) +#include +#define AL_APPLE_HAVE_DISPATCH 1 +#else +#include /* Fallback option for Apple without a working libdispatch */ +#endif +#elif !defined(_WIN32) +#include +#endif + +namespace al { + +class semaphore { +#ifdef _WIN32 + using native_type = void*; +#elif defined(AL_APPLE_HAVE_DISPATCH) + using native_type = dispatch_semaphore_t; +#else + using native_type = sem_t; +#endif + native_type mSem; + +public: + semaphore(unsigned int initial=0); + semaphore(const semaphore&) = delete; + ~semaphore(); + + semaphore& operator=(const semaphore&) = delete; + + void post(); + void wait() noexcept; + bool try_wait() noexcept; +}; + +} // namespace al + +#endif /* COMMON_ALSEM_H */ diff --git a/common/threads.cpp b/common/threads.cpp deleted file mode 100644 index c176f5af..00000000 --- a/common/threads.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/** - * OpenAL cross platform audio library - * Copyright (C) 1999-2007 by authors. - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * Or go to http://www.gnu.org/copyleft/lgpl.html - */ - -#include "config.h" - -#include "opthelpers.h" -#include "threads.h" - -#include - - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include - -#include - -namespace al { - -semaphore::semaphore(unsigned int initial) -{ - if(initial > static_cast(std::numeric_limits::max())) - throw std::system_error(std::make_error_code(std::errc::value_too_large)); - mSem = CreateSemaphore(nullptr, initial, std::numeric_limits::max(), nullptr); - if(mSem == nullptr) - throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); -} - -semaphore::~semaphore() -{ CloseHandle(mSem); } - -void semaphore::post() -{ - if(!ReleaseSemaphore(static_cast(mSem), 1, nullptr)) - throw std::system_error(std::make_error_code(std::errc::value_too_large)); -} - -void semaphore::wait() noexcept -{ WaitForSingleObject(static_cast(mSem), INFINITE); } - -bool semaphore::try_wait() noexcept -{ return WaitForSingleObject(static_cast(mSem), 0) == WAIT_OBJECT_0; } - -} // namespace al - -#else - -/* Do not try using libdispatch on systems where it is absent. */ -#if defined(AL_APPLE_HAVE_DISPATCH) - -namespace al { - -semaphore::semaphore(unsigned int initial) -{ - mSem = dispatch_semaphore_create(initial); - if(!mSem) - throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); -} - -semaphore::~semaphore() -{ dispatch_release(mSem); } - -void semaphore::post() -{ dispatch_semaphore_signal(mSem); } - -void semaphore::wait() noexcept -{ dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); } - -bool semaphore::try_wait() noexcept -{ return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; } - -} // namespace al - -#else /* !__APPLE__ */ - -#include - -namespace al { - -semaphore::semaphore(unsigned int initial) -{ - if(sem_init(&mSem, 0, initial) != 0) - throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); -} - -semaphore::~semaphore() -{ sem_destroy(&mSem); } - -void semaphore::post() -{ - if(sem_post(&mSem) != 0) - throw std::system_error(std::make_error_code(std::errc::value_too_large)); -} - -void semaphore::wait() noexcept -{ - while(sem_wait(&mSem) == -1 && errno == EINTR) { - } -} - -bool semaphore::try_wait() noexcept -{ return sem_trywait(&mSem) == 0; } - -} // namespace al - -#endif /* __APPLE__ */ - -#endif /* _WIN32 */ diff --git a/common/threads.h b/common/threads.h deleted file mode 100644 index 703d50d4..00000000 --- a/common/threads.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef AL_THREADS_H -#define AL_THREADS_H - -#if defined(__APPLE__) -#include -#include -#if (((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) || TARGET_OS_IOS || TARGET_OS_TV) -#include -#define AL_APPLE_HAVE_DISPATCH 1 -#else -#include /* Fallback option for Apple without a working libdispatch */ -#endif -#elif !defined(_WIN32) -#include -#endif - -namespace al { - -class semaphore { -#ifdef _WIN32 - using native_type = void*; -#elif defined(AL_APPLE_HAVE_DISPATCH) - using native_type = dispatch_semaphore_t; -#else - using native_type = sem_t; -#endif - native_type mSem; - -public: - semaphore(unsigned int initial=0); - semaphore(const semaphore&) = delete; - ~semaphore(); - - semaphore& operator=(const semaphore&) = delete; - - void post(); - void wait() noexcept; - bool try_wait() noexcept; -}; - -} // namespace al - -#endif /* AL_THREADS_H */ diff --git a/core/context.h b/core/context.h index 629e67a5..ccb7dd3b 100644 --- a/core/context.h +++ b/core/context.h @@ -10,11 +10,11 @@ #include #include "almalloc.h" +#include "alsem.h" #include "alspan.h" #include "async_event.h" #include "atomic.h" #include "opthelpers.h" -#include "threads.h" #include "vecmat.h" struct DeviceBase; -- cgit v1.2.3