Remove Mutex, replacing their uses with std::{mutex, recursive_mutex}

This commit is contained in:
yvt 2019-05-25 15:00:32 +09:00
parent 76908188ab
commit c910bd4846
No known key found for this signature in database
GPG Key ID: 48F2768FA8D07C92
18 changed files with 51 additions and 218 deletions

View File

@ -146,9 +146,7 @@
E82E677418EA7972004DBA18 /* VersionInfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E85233791839B28C00F40541 /* VersionInfo.cpp */; };
E82E677518EA7972004DBA18 /* Thread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03B0178EE300000683D4 /* Thread.cpp */; };
E82E677618EA7972004DBA18 /* Semaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03CF178EEF80000683D4 /* Semaphore.cpp */; };
E82E677718EA7972004DBA18 /* Mutex.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03D2178EF069000683D4 /* Mutex.cpp */; };
E82E677818EA7972004DBA18 /* ILockable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03D5178EF09C000683D4 /* ILockable.cpp */; };
E82E677918EA7972004DBA18 /* AutoLocker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03D8178EF165000683D4 /* AutoLocker.cpp */; };
E82E677A18EA7972004DBA18 /* IRunnable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03DE178EF4E9000683D4 /* IRunnable.cpp */; };
E82E677B18EA7972004DBA18 /* Exception.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF0403178FF776000683D4 /* Exception.cpp */; };
E82E677C18EA7972004DBA18 /* DynamicLibrary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8567E5E1792C0FF009D83E0 /* DynamicLibrary.cpp */; };
@ -793,12 +791,8 @@
E8CF03CD178EE95F000683D4 /* Math.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Math.h; sourceTree = "<group>"; };
E8CF03CF178EEF80000683D4 /* Semaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Semaphore.cpp; sourceTree = "<group>"; };
E8CF03D0178EEF80000683D4 /* Semaphore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Semaphore.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E8CF03D2178EF069000683D4 /* Mutex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Mutex.cpp; sourceTree = "<group>"; };
E8CF03D3178EF06A000683D4 /* Mutex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mutex.h; sourceTree = "<group>"; };
E8CF03D5178EF09C000683D4 /* ILockable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ILockable.cpp; sourceTree = "<group>"; };
E8CF03D6178EF09C000683D4 /* ILockable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ILockable.h; sourceTree = "<group>"; };
E8CF03D8178EF165000683D4 /* AutoLocker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AutoLocker.cpp; sourceTree = "<group>"; };
E8CF03D9178EF166000683D4 /* AutoLocker.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = AutoLocker.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E8CF03DE178EF4E9000683D4 /* IRunnable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IRunnable.cpp; sourceTree = "<group>"; };
E8CF03DF178EF4E9000683D4 /* IRunnable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IRunnable.h; sourceTree = "<group>"; };
E8CF03E1178EF57E000683D4 /* SDLRunner.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SDLRunner.cpp; sourceTree = "<group>"; };
@ -1823,12 +1817,8 @@
E8CF03B1178EE300000683D4 /* Thread.h */,
E8CF03CF178EEF80000683D4 /* Semaphore.cpp */,
E8CF03D0178EEF80000683D4 /* Semaphore.h */,
E8CF03D2178EF069000683D4 /* Mutex.cpp */,
E8CF03D3178EF06A000683D4 /* Mutex.h */,
E8CF03D5178EF09C000683D4 /* ILockable.cpp */,
E8CF03D6178EF09C000683D4 /* ILockable.h */,
E8CF03D8178EF165000683D4 /* AutoLocker.cpp */,
E8CF03D9178EF166000683D4 /* AutoLocker.h */,
E8CF03DE178EF4E9000683D4 /* IRunnable.cpp */,
E8CF03DF178EF4E9000683D4 /* IRunnable.h */,
E8CF0403178FF776000683D4 /* Exception.cpp */,
@ -2121,9 +2111,7 @@
E82E677418EA7972004DBA18 /* VersionInfo.cpp in Sources */,
E82E677518EA7972004DBA18 /* Thread.cpp in Sources */,
E82E677618EA7972004DBA18 /* Semaphore.cpp in Sources */,
E82E677718EA7972004DBA18 /* Mutex.cpp in Sources */,
E82E677818EA7972004DBA18 /* ILockable.cpp in Sources */,
E82E677918EA7972004DBA18 /* AutoLocker.cpp in Sources */,
E82E677A18EA7972004DBA18 /* IRunnable.cpp in Sources */,
E82E677B18EA7972004DBA18 /* Exception.cpp in Sources */,
E82E677C18EA7972004DBA18 /* DynamicLibrary.cpp in Sources */,

View File

@ -25,7 +25,6 @@
#include <vector>
#include "GameMap.h"
#include <Core/AutoLocker.h>
#include <Core/Debug.h>
#include <Core/Exception.h>
#include <Core/FileManager.h>
@ -51,12 +50,12 @@ namespace spades {
GameMap::~GameMap() { SPADES_MARK_FUNCTION(); }
void GameMap::AddListener(spades::client::IGameMapListener *l) {
AutoLocker guard(&listenersMutex);
std::lock_guard<std::mutex> _guard{listenersMutex};
listeners.push_back(l);
}
void GameMap::RemoveListener(spades::client::IGameMapListener *l) {
AutoLocker guard(&listenersMutex);
std::lock_guard<std::mutex> _guard{listenersMutex};
auto it = std::find(listeners.begin(), listeners.end(), l);
if (it != listeners.end()) {
listeners.erase(it);

View File

@ -24,13 +24,12 @@
#include <cstdint>
#include <functional>
#include <list>
#include <mutex>
#include <Core/Debug.h>
#include <Core/Math.h>
#include "IGameMapListener.h"
#include <Core/AutoLocker.h>
#include <Core/Mutex.h>
#include <Core/RefCountedObject.h>
namespace spades {
@ -127,11 +126,9 @@ namespace spades {
}
if (!unsafe) {
if (changed) {
{
AutoLocker guard(&listenersMutex);
for (auto *l : listeners) {
l->GameMapChanged(x, y, z, this);
}
std::lock_guard<std::mutex> guard{listenersMutex};
for (auto *l : listeners) {
l->GameMapChanged(x, y, z, this);
}
}
}
@ -163,9 +160,9 @@ namespace spades {
uint64_t solidMap[DefaultWidth][DefaultHeight];
uint32_t colorMap[DefaultWidth][DefaultHeight][DefaultDepth];
std::list<IGameMapListener *> listeners;
Mutex listenersMutex;
std::mutex listenersMutex;
bool IsSurface(int x, int y, int z);
};
}
}
} // namespace client
} // namespace spades

View File

@ -1,21 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades 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 3 of the License, or
(at your option) any later version.
OpenSpades 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 OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AutoLocker.h"

View File

@ -1,56 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades 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 3 of the License, or
(at your option) any later version.
OpenSpades 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 OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "ILockable.h"
namespace spades {
class AutoLocker {
ILockable *lockable;
public:
AutoLocker(ILockable *l) : lockable(l) {
if (lockable)
lockable->Lock();
}
AutoLocker(const AutoLocker &l) : lockable(l.lockable) {
if (lockable)
lockable->Lock();
}
~AutoLocker() {
if (lockable)
lockable->Unlock();
}
ILockable *Release() {
ILockable *l = lockable;
lockable = 0;
return l;
}
ILockable *GetLockable() { return lockable; }
void operator=(const AutoLocker &l) {
if (l.lockable)
l.lockable->Lock();
if (lockable)
lockable->Unlock();
lockable = l.lockable;
}
};
}

View File

@ -39,11 +39,9 @@
#endif
#endif
#include "AutoLocker.h"
#include "ConcurrentDispatch.h"
#include "Debug.h"
#include "Exception.h"
#include "Mutex.h"
#include "Settings.h"
#include "Thread.h"
#include <OpenSpades.h>

View File

@ -1,32 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades 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 3 of the License, or
(at your option) any later version.
OpenSpades 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 OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Mutex.h"
#include <Imports/SDL.h>
namespace spades {
Mutex::Mutex() { priv = (void *)SDL_CreateMutex(); }
Mutex::~Mutex() { SDL_DestroyMutex((SDL_mutex *)priv); }
void Mutex::Lock() { SDL_mutexP((SDL_mutex *)priv); }
void Mutex::Unlock() { SDL_mutexV((SDL_mutex *)priv); }
}

View File

@ -1,36 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades 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 3 of the License, or
(at your option) any later version.
OpenSpades 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 OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "ILockable.h"
namespace spades {
/** Recursive mutex. */
class Mutex : public ILockable {
void *priv;
public:
Mutex();
~Mutex();
void Lock() override;
void Unlock() override;
};
}

View File

@ -20,7 +20,6 @@
#include "RefCountedObject.h"
#include <ScriptBindings/ScriptManager.h>
#include "AutoLocker.h"
#include "Exception.h"
namespace spades {
@ -32,7 +31,7 @@ namespace spades {
void RefCountedObject::Release() {
#if DEBUG_REFCOUNTED_OBJECT_LAST_RELEASE
AutoLocker guard(&releaseInfoMutex);
std::lock_guard<std::mutex> guard{releaseInfoMutex};
#endif
int cnt = refCount.fetch_sub(1);
if (cnt == 1) {

View File

@ -25,10 +25,13 @@
#include <typeinfo>
#include "Debug.h"
#include "Mutex.h"
#define DEBUG_REFCOUNTED_OBJECT_LAST_RELEASE 0
#if DEBUG_REFCOUNTED_OBJECT_LAST_RELEASE
#include <mutex>
#endif
namespace spades {
class RefCountedObject {
@ -36,7 +39,7 @@ namespace spades {
#if DEBUG_REFCOUNTED_OBJECT_LAST_RELEASE
reflection::BacktraceRecord lastRelease;
reflection::BacktraceRecord secondLastRelease;
Mutex releaseInfoMutex;
std::mutex releaseInfoMutex;
#endif
protected:
virtual ~RefCountedObject();
@ -147,11 +150,9 @@ namespace spades {
}
/**
* Attempts to cast this `Handle<T>` to `Handle<S>` using `dynamic_cast`.
* Throws an exception if the cast was unsuccessful.
*/
template <class S> Handle<S> Cast() const & {
return Handle{*this}.Cast<S>();
}
* Attempts to cast this `Handle<T>` to `Handle<S>` using `dynamic_cast`.
* Throws an exception if the cast was unsuccessful.
*/
template <class S> Handle<S> Cast() const & { return Handle{*this}.Cast<S>(); }
};
}

View File

@ -18,11 +18,11 @@
*/
#include <mutex>
#include <typeinfo>
#include <Imports/SDL.h>
#include "AutoLocker.h"
#include "ConcurrentDispatch.h"
#include "Debug.h"
#include "Thread.h"
@ -39,15 +39,15 @@ namespace spades {
class ThreadCleanuper : public ConcurrentDispatch {
std::vector<SDL_Thread *> threads;
Mutex mutex;
std::recursive_mutex mutex;
public:
void Add(SDL_Thread *thread) {
AutoLocker locker(&mutex);
std::lock_guard<std::recursive_mutex> _lock{this->mutex};
threads.push_back(thread);
}
void Cleanup() {
AutoLocker locker(&mutex);
std::lock_guard<std::recursive_mutex> _lock{this->mutex};
for (size_t i = 0; i < threads.size(); i++)
SDL_WaitThread(threads[i], NULL);
threads.clear();
@ -63,7 +63,7 @@ namespace spades {
Thread::~Thread() {
SDL_Thread *th = NULL;
{
AutoLocker locker(&lock);
std::lock_guard<std::recursive_mutex> _lock{this->lock};
th = (SDL_Thread *)threadInfo;
if (!th)
return;
@ -80,7 +80,7 @@ namespace spades {
}
void Thread::Start() {
AutoLocker locker(&lock);
std::lock_guard<std::recursive_mutex> _lock{this->lock};
if (threadInfo)
return;
@ -99,7 +99,7 @@ namespace spades {
}
SDL_Thread *th = NULL;
{
AutoLocker locker(&lock);
std::lock_guard<std::recursive_mutex> _lock{this->lock};
th = (SDL_Thread *)threadInfo;
if (!th)
return;
@ -137,13 +137,13 @@ namespace spades {
}
void Thread::Quited() {
lock.Lock();
lock.lock();
threadInfo = NULL;
if (autoDelete) {
delete this;
return;
}
lock.Unlock();
lock.unlock();
}
void Thread::Run() {
@ -152,13 +152,14 @@ namespace spades {
}
void Thread::MarkForAutoDeletion() {
lock.Lock();
lock.lock();
if (threadInfo == NULL) {
// already exited
lock.unlock();
delete this;
return;
}
autoDelete = true;
lock.Unlock();
lock.unlock();
}
}
} // namespace spades

View File

@ -21,15 +21,15 @@
#pragma once
#include <Imports/SDL.h>
#include <mutex>
#include "IRunnable.h"
#include "Mutex.h"
namespace spades {
class Thread {
void *threadInfo;
Mutex lock;
std::recursive_mutex lock;
IRunnable *runnable;
bool autoDelete;
SDL_threadID threadId;

View File

@ -29,7 +29,6 @@
#include "MainScreen.h"
#include "MainScreenHelper.h"
#include <Core/AutoLocker.h>
#include <Core/FileManager.h>
#include <Core/IStream.h>
#include <Core/Settings.h>

View File

@ -24,7 +24,6 @@
#include <vector>
#include <memory>
#include <Core/Mutex.h>
#include <Core/RefCountedObject.h>
#include <Core/TMPUtils.h>
#include <ScriptBindings/ScriptManager.h>

View File

@ -21,14 +21,13 @@
#include <curl/curl.h>
#include <json/json.h>
#include <memory>
#include <mutex>
#include "PackageUpdateManager.h"
#include <Core/AutoLocker.h>
#include <Core/Debug.h>
#include <Core/Exception.h>
#include <Core/FileManager.h>
#include <Core/Mutex.h>
#include <Core/Settings.h>
#include <Core/Strings.h>
#include <Core/TMPUtils.h>
@ -39,7 +38,7 @@ DEFINE_SPADES_SETTING(cl_checkForUpdates, "0");
namespace spades {
namespace {
Mutex globalMutex;
std::recursive_mutex globalMutex;
PackageUpdateManager::VersionInfo ParseVersionInfo(const Json::Value &value) {
if (!value.isObject()) {
@ -88,21 +87,21 @@ namespace spades {
PackageUpdateManager &m_packageUpdateManager;
void ReturnUnavailable() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
SPAssert(m_packageUpdateManager.m_updateInfoReadyState == ReadyState::Loading);
m_packageUpdateManager.m_updateInfoReadyState = ReadyState::Unavailable;
SPLog("Update info is not available for the package and/or the current platform.");
}
void ReturnError(const std::string &reason) {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
SPAssert(m_packageUpdateManager.m_updateInfoReadyState == ReadyState::Loading);
m_packageUpdateManager.m_updateInfoReadyState = ReadyState::Error;
SPLog("Failed to check for update.: %s", reason.c_str());
}
void ReturnVersionInfo(const VersionInfo &info, const std::string &pageURL) {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
SPAssert(m_packageUpdateManager.m_updateInfoReadyState == ReadyState::Loading);
m_packageUpdateManager.m_updateInfoReadyState = ReadyState::Loaded;
m_packageUpdateManager.m_latestVersionInfo = info;
@ -367,12 +366,12 @@ namespace spades {
PackageUpdateManager::~PackageUpdateManager() {}
PackageUpdateManager::ReadyState PackageUpdateManager::GetUpdateInfoReadyState() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
return m_updateInfoReadyState;
}
bool PackageUpdateManager::IsUpdateAvailable() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
if (m_updateInfoReadyState != ReadyState::Loaded) {
return false;
@ -382,12 +381,12 @@ namespace spades {
}
PackageUpdateManager::VersionInfo PackageUpdateManager::GetLatestVersionInfo() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
return m_latestVersionInfo;
}
std::string PackageUpdateManager::GetLatestVersionInfoPageURL() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
if (m_updateInfoReadyState != ReadyState::Loaded) {
return std::string{};
@ -397,7 +396,7 @@ namespace spades {
}
void PackageUpdateManager::CheckForUpdate() {
AutoLocker lock{&globalMutex};
std::lock_guard<std::recursive_mutex> _lock{globalMutex};
if (m_updateInfoReadyState == ReadyState::Loading) {
return;
}

View File

@ -25,7 +25,6 @@
#include <vector>
#include <Core/Math.h>
#include <Core/Mutex.h>
#include <Core/RefCountedObject.h>
#include <ScriptBindings/ScriptManager.h>

View File

@ -23,7 +23,6 @@
#include <vector>
#include <sstream>
#include <Core/Exception.h>
#include <Core/AutoLocker.h>
#include <Core/FileManager.h>
#include <Core/IStream.h>
@ -199,8 +198,8 @@ namespace spades {
ScriptContextHandle ScriptManager::GetContext() {
SPADES_MARK_FUNCTION_DEBUG();
AutoLocker locker(&contextMutex);
if(contextFreeList.empty()){
std::lock_guard<std::recursive_mutex> _lock{contextMutex};
// no free context; create one
Context *ctx = new Context();
ctx->obj = engine->CreateContext();
@ -223,13 +222,13 @@ namespace spades {
ScriptContextHandle::ScriptContextHandle(ScriptManager::Context *ctx,
ScriptManager *manager):
manager(manager), obj(ctx){
AutoLocker locker(&manager->contextMutex);
std::lock_guard<std::recursive_mutex> _lock{manager->contextMutex};
ctx->refCount++;
}
ScriptContextHandle::ScriptContextHandle(const ScriptContextHandle& h) :
manager(h.manager), obj(h.obj){
AutoLocker locker(&manager->contextMutex);
std::lock_guard<std::recursive_mutex> _lock{manager->contextMutex};
obj->refCount++;
}
@ -239,7 +238,7 @@ namespace spades {
void ScriptContextHandle::Release() {
if(obj){
AutoLocker locker(&manager->contextMutex);
std::lock_guard<std::recursive_mutex> _lock{manager->contextMutex};
obj->refCount--;
if(obj->refCount == 0){
// this context is no longer used;
@ -258,7 +257,7 @@ namespace spades {
manager = h.manager;
obj = h.obj;
AutoLocker locker(&manager->contextMutex);
std::lock_guard<std::recursive_mutex> _lock{manager->contextMutex};
obj->refCount++;
}

View File

@ -31,8 +31,8 @@
#include <AngelScript/addons/scriptmathcomplex.h>
#include <AngelScript/addons/scriptstdstring.h>
#include <AngelScript/addons/weakref.h>
#include <Core/Mutex.h>
#include <list>
#include <mutex>
namespace spades {
@ -44,7 +44,7 @@ namespace spades {
asIScriptContext *obj;
int refCount;
};
Mutex contextMutex;
std::recursive_mutex contextMutex;
std::list<Context *> contextFreeList;
asIScriptEngine *engine;