diff options
author | Lukas Rusak <lorusak@gmail.com> | 2022-03-26 10:28:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-26 10:28:47 -0700 |
commit | 1858297881e105f04bf975aa4e901105662791bc (patch) | |
tree | 5ca626091c66c3c49a606e7c57225297bebdfdf6 | |
parent | f2c8d1e2077b4338871b11a45d2de4e7f8bf18ea (diff) | |
parent | 6335b3f757783a277b1a36006240b2d1df9dc83f (diff) |
Merge pull request #21180 from lrusak/atomicspinlock
threads: replace CAtomicSpinLock usage with std::lock_guard
-rw-r--r-- | xbmc/network/Zeroconf.cpp | 13 | ||||
-rw-r--r-- | xbmc/network/Zeroconf.h | 3 | ||||
-rw-r--r-- | xbmc/network/ZeroconfBrowser.cpp | 31 | ||||
-rw-r--r-- | xbmc/network/ZeroconfBrowser.h | 3 | ||||
-rw-r--r-- | xbmc/platform/darwin/osx/XBMCHelper.cpp | 12 | ||||
-rw-r--r-- | xbmc/platform/darwin/tvos/TVOSSettingsHandler.mm | 13 | ||||
-rw-r--r-- | xbmc/platform/posix/XTimeUtils.cpp | 9 | ||||
-rw-r--r-- | xbmc/threads/Atomics.cpp | 22 | ||||
-rw-r--r-- | xbmc/threads/Atomics.h | 21 | ||||
-rw-r--r-- | xbmc/threads/CMakeLists.txt | 6 |
10 files changed, 50 insertions, 83 deletions
diff --git a/xbmc/network/Zeroconf.cpp b/xbmc/network/Zeroconf.cpp index cf850202ab..311ecfb089 100644 --- a/xbmc/network/Zeroconf.cpp +++ b/xbmc/network/Zeroconf.cpp @@ -15,7 +15,6 @@ #endif #include "settings/Settings.h" #include "settings/SettingsComponent.h" -#include "threads/Atomics.h" #include "threads/CriticalSection.h" #include "utils/JobManager.h" @@ -31,6 +30,13 @@ #include <cassert> #include <utility> +namespace +{ + +std::mutex singletonMutex; + +} + #ifndef HAS_ZEROCONF //dummy implementation used if no zeroconf is present //should be optimized away @@ -47,7 +53,6 @@ class CZeroconfDummy : public CZeroconf }; #endif -std::atomic_flag CZeroconf::sm_singleton_guard = ATOMIC_FLAG_INIT; CZeroconf* CZeroconf::smp_instance = 0; CZeroconf::CZeroconf():mp_crit_sec(new CCriticalSection) @@ -134,7 +139,7 @@ void CZeroconf::Stop() CZeroconf* CZeroconf::GetInstance() { - CAtomicSpinLock lock(sm_singleton_guard); + std::lock_guard<std::mutex> lock(singletonMutex); if(!smp_instance) { #ifndef HAS_ZEROCONF @@ -157,7 +162,7 @@ CZeroconf* CZeroconf::GetInstance() void CZeroconf::ReleaseInstance() { - CAtomicSpinLock lock(sm_singleton_guard); + std::lock_guard<std::mutex> lock(singletonMutex); delete smp_instance; smp_instance = 0; } diff --git a/xbmc/network/Zeroconf.h b/xbmc/network/Zeroconf.h index f91f0fc3c3..bed66c936b 100644 --- a/xbmc/network/Zeroconf.h +++ b/xbmc/network/Zeroconf.h @@ -10,7 +10,6 @@ #include "utils/Job.h" -#include <atomic> #include <map> #include <string> #include <utility> @@ -123,8 +122,6 @@ private: tServiceMap m_service_map; bool m_started = false; - //protects singleton creation/destruction - static std::atomic_flag sm_singleton_guard; static CZeroconf* smp_instance; class CPublish : public CJob diff --git a/xbmc/network/ZeroconfBrowser.cpp b/xbmc/network/ZeroconfBrowser.cpp index c08dbac250..2d1b8c6686 100644 --- a/xbmc/network/ZeroconfBrowser.cpp +++ b/xbmc/network/ZeroconfBrowser.cpp @@ -25,7 +25,13 @@ #endif #include "threads/CriticalSection.h" -#include "threads/Atomics.h" + +namespace +{ + +std::mutex singletonMutex; + +} #if !defined(HAS_ZEROCONF) //dummy implementation used if no zeroconf is present @@ -39,7 +45,6 @@ class CZeroconfBrowserDummy : public CZeroconfBrowser }; #endif -std::atomic_flag CZeroconfBrowser::sm_singleton_guard = ATOMIC_FLAG_INIT; CZeroconfBrowser* CZeroconfBrowser::smp_instance = 0; CZeroconfBrowser::CZeroconfBrowser():mp_crit_sec(new CCriticalSection) @@ -131,27 +136,24 @@ bool CZeroconfBrowser::ResolveService(ZeroconfService& fr_service, double f_time CZeroconfBrowser* CZeroconfBrowser::GetInstance() { + std::lock_guard<std::mutex> lock(singletonMutex); + if(!smp_instance) { - //use double checked locking - CAtomicSpinLock lock(sm_singleton_guard); - if(!smp_instance) - { #if !defined(HAS_ZEROCONF) - smp_instance = new CZeroconfBrowserDummy; + smp_instance = new CZeroconfBrowserDummy; #else #if defined(TARGET_DARWIN) - smp_instance = new CZeroconfBrowserDarwin; + smp_instance = new CZeroconfBrowserDarwin; #elif defined(HAS_AVAHI) - smp_instance = new CZeroconfBrowserAvahi; + smp_instance = new CZeroconfBrowserAvahi; #elif defined(TARGET_ANDROID) - // WIP - smp_instance = new CZeroconfBrowserAndroid; + // WIP + smp_instance = new CZeroconfBrowserAndroid; #elif defined(HAS_MDNS) - smp_instance = new CZeroconfBrowserMDNS; + smp_instance = new CZeroconfBrowserMDNS; #endif #endif - } } assert(smp_instance); return smp_instance; @@ -159,7 +161,8 @@ CZeroconfBrowser* CZeroconfBrowser::GetInstance() void CZeroconfBrowser::ReleaseInstance() { - CAtomicSpinLock lock(sm_singleton_guard); + std::lock_guard<std::mutex> lock(singletonMutex); + delete smp_instance; smp_instance = 0; } diff --git a/xbmc/network/ZeroconfBrowser.h b/xbmc/network/ZeroconfBrowser.h index 5979e34186..76a4439a11 100644 --- a/xbmc/network/ZeroconfBrowser.h +++ b/xbmc/network/ZeroconfBrowser.h @@ -8,7 +8,6 @@ #pragma once -#include <atomic> #include <string> #include <set> #include <vector> @@ -153,8 +152,6 @@ private: tServices m_services; bool m_started = false; - //protects singleton creation/destruction - static std::atomic_flag sm_singleton_guard; static CZeroconfBrowser* smp_instance; }; #include <iostream> diff --git a/xbmc/platform/darwin/osx/XBMCHelper.cpp b/xbmc/platform/darwin/osx/XBMCHelper.cpp index 52bb6acfdb..396c67a969 100644 --- a/xbmc/platform/darwin/osx/XBMCHelper.cpp +++ b/xbmc/platform/darwin/osx/XBMCHelper.cpp @@ -19,12 +19,12 @@ #include "settings/Settings.h" #include "settings/SettingsComponent.h" #include "settings/lib/Setting.h" -#include "threads/Atomics.h" #include "utils/SystemInfo.h" #include "utils/TimeUtils.h" #include "utils/log.h" #include <fstream> +#include <mutex> #include <signal.h> #include <sstream> @@ -32,7 +32,13 @@ #include "PlatformDefs.h" -static std::atomic_flag sg_singleton_lock_variable = ATOMIC_FLAG_INIT; +namespace +{ + +std::mutex singletonMutex; + +} + XBMCHelper* XBMCHelper::smp_instance = 0; #define XBMC_HELPER_PROGRAM "XBMCHelper" @@ -44,7 +50,7 @@ static int GetBSDProcessList(kinfo_proc **procList, size_t *procCount); XBMCHelper& XBMCHelper::GetInstance() { - CAtomicSpinLock lock(sg_singleton_lock_variable); + std::lock_guard<std::mutex> lock(singletonMutex); if( ! smp_instance ) { smp_instance = new XBMCHelper(); diff --git a/xbmc/platform/darwin/tvos/TVOSSettingsHandler.mm b/xbmc/platform/darwin/tvos/TVOSSettingsHandler.mm index a1a19530f9..5e3df2c8aa 100644 --- a/xbmc/platform/darwin/tvos/TVOSSettingsHandler.mm +++ b/xbmc/platform/darwin/tvos/TVOSSettingsHandler.mm @@ -12,18 +12,25 @@ #include "settings/Settings.h" #include "settings/SettingsComponent.h" #include "settings/lib/Setting.h" -#include "threads/Atomics.h" #import "platform/darwin/tvos/XBMCController.h" #import "platform/darwin/tvos/input/LibInputHandler.h" #import "platform/darwin/tvos/input/LibInputSettings.h" -static std::atomic_flag sg_singleton_lock_variable = ATOMIC_FLAG_INIT; +#include <mutex> + +namespace +{ + +std::mutex singletonMutex; + +} + CTVOSInputSettings* CTVOSInputSettings::m_instance = nullptr; CTVOSInputSettings& CTVOSInputSettings::GetInstance() { - CAtomicSpinLock lock(sg_singleton_lock_variable); + std::lock_guard<std::mutex> lock(singletonMutex); if (!m_instance) m_instance = new CTVOSInputSettings(); diff --git a/xbmc/platform/posix/XTimeUtils.cpp b/xbmc/platform/posix/XTimeUtils.cpp index bbec474e86..e78e5cff48 100644 --- a/xbmc/platform/posix/XTimeUtils.cpp +++ b/xbmc/platform/posix/XTimeUtils.cpp @@ -11,14 +11,11 @@ #include "PosixTimezone.h" #include <errno.h> +#include <mutex> #include <time.h> #include <sys/times.h> -#if defined(TARGET_DARWIN) -#include "threads/Atomics.h" -#endif - #if defined(TARGET_ANDROID) && !defined(__LP64__) #include <time64.h> #endif @@ -92,7 +89,7 @@ int SystemTimeToFileTime(const SystemTime* systemTime, FileTime* fileTime) { static const int dayoffset[12] = {0, 31, 59, 90, 120, 151, 182, 212, 243, 273, 304, 334}; #if defined(TARGET_DARWIN) - static std::atomic_flag timegm_lock = ATOMIC_FLAG_INIT; + static std::mutex timegm_lock; #endif struct tm sysTime = {}; @@ -111,7 +108,7 @@ int SystemTimeToFileTime(const SystemTime* systemTime, FileTime* fileTime) sysTime.tm_yday++; #if defined(TARGET_DARWIN) - CAtomicSpinLock lock(timegm_lock); + std::lock_guard<std::mutex> lock(timegm_lock); #endif #if defined(TARGET_ANDROID) && !defined(__LP64__) diff --git a/xbmc/threads/Atomics.cpp b/xbmc/threads/Atomics.cpp deleted file mode 100644 index 28d5d4c33f..0000000000 --- a/xbmc/threads/Atomics.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2005-2018 Team Kodi - * This file is part of Kodi - https://kodi.tv - * - * SPDX-License-Identifier: GPL-2.0-or-later - * See LICENSES/README.md for more information. - */ - -#include "Atomics.h" - -/////////////////////////////////////////////////////////////////////////// -// Fast spinlock implementation. No backoff when busy -/////////////////////////////////////////////////////////////////////////// -CAtomicSpinLock::CAtomicSpinLock(std::atomic_flag& lock) : m_Lock(lock) -{ - while (atomic_flag_test_and_set(&m_Lock)) {} // Lock -} - -CAtomicSpinLock::~CAtomicSpinLock() -{ - std::atomic_flag_clear(&m_Lock); // Unlock -} diff --git a/xbmc/threads/Atomics.h b/xbmc/threads/Atomics.h deleted file mode 100644 index 9a403fae65..0000000000 --- a/xbmc/threads/Atomics.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2005-2018 Team Kodi - * This file is part of Kodi - https://kodi.tv - * - * SPDX-License-Identifier: GPL-2.0-or-later - * See LICENSES/README.md for more information. - */ - -#pragma once - -#include <atomic> - -class CAtomicSpinLock -{ -public: - explicit CAtomicSpinLock(std::atomic_flag& lock); - ~CAtomicSpinLock(); -private: - std::atomic_flag& m_Lock; -}; - diff --git a/xbmc/threads/CMakeLists.txt b/xbmc/threads/CMakeLists.txt index 0202ca4514..de9007d684 100644 --- a/xbmc/threads/CMakeLists.txt +++ b/xbmc/threads/CMakeLists.txt @@ -1,10 +1,8 @@ -set(SOURCES Atomics.cpp - Event.cpp +set(SOURCES Event.cpp Thread.cpp Timer.cpp) -set(HEADERS Atomics.h - Condition.h +set(HEADERS Condition.h CriticalSection.h Event.h Lockables.h |