1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-04-23 11:42:38 +00:00

Use unsigned to define number of threads

This commit is contained in:
elsid 2022-07-12 14:58:25 +02:00
parent 8c3c65fe9f
commit 22ed6d5c1e
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625
3 changed files with 13 additions and 18 deletions

View File

@ -33,9 +33,8 @@
namespace namespace
{ {
template <class Mutex> template <class Mutex>
std::optional<std::unique_lock<Mutex>> makeExclusiveLock(Mutex& mutex, int threadCount) std::optional<std::unique_lock<Mutex>> makeExclusiveLock(Mutex& mutex, unsigned threadCount)
{ {
assert(threadCount >= 0);
if (threadCount > 0) if (threadCount > 0)
return std::unique_lock(mutex); return std::unique_lock(mutex);
return {}; return {};
@ -48,7 +47,7 @@ namespace
public: public:
/// @param mutex a mutex /// @param mutex a mutex
/// @param threadCount decide wether the excluse lock will be taken /// @param threadCount decide wether the excluse lock will be taken
explicit MaybeExclusiveLock(Mutex& mutex, int threadCount) explicit MaybeExclusiveLock(Mutex& mutex, unsigned threadCount)
: mImpl(makeExclusiveLock(mutex, threadCount)) : mImpl(makeExclusiveLock(mutex, threadCount))
{} {}
@ -57,9 +56,8 @@ namespace
}; };
template <class Mutex> template <class Mutex>
std::optional<std::shared_lock<Mutex>> makeSharedLock(Mutex& mutex, int threadCount) std::optional<std::shared_lock<Mutex>> makeSharedLock(Mutex& mutex, unsigned threadCount)
{ {
assert(threadCount >= 0);
if (threadCount > 0) if (threadCount > 0)
return std::shared_lock(mutex); return std::shared_lock(mutex);
return {}; return {};
@ -72,7 +70,7 @@ namespace
public: public:
/// @param mutex a shared mutex /// @param mutex a shared mutex
/// @param threadCount decide wether the shared lock will be taken /// @param threadCount decide wether the shared lock will be taken
explicit MaybeSharedLock(Mutex& mutex, int threadCount) explicit MaybeSharedLock(Mutex& mutex, unsigned threadCount)
: mImpl(makeSharedLock(mutex, threadCount)) : mImpl(makeSharedLock(mutex, threadCount))
{} {}
@ -81,9 +79,8 @@ namespace
}; };
template <class Mutex> template <class Mutex>
std::variant<std::monostate, std::unique_lock<Mutex>, std::shared_lock<Mutex>> makeLock(Mutex& mutex, int threadCount) std::variant<std::monostate, std::unique_lock<Mutex>, std::shared_lock<Mutex>> makeLock(Mutex& mutex, unsigned threadCount)
{ {
assert(threadCount >= 0);
if (threadCount > 1) if (threadCount > 1)
return std::shared_lock(mutex); return std::shared_lock(mutex);
if (threadCount == 1) if (threadCount == 1)
@ -98,7 +95,7 @@ namespace
public: public:
/// @param mutex a shared mutex /// @param mutex a shared mutex
/// @param threadCount decide wether the lock will be shared, exclusive or inexistent /// @param threadCount decide wether the lock will be shared, exclusive or inexistent
explicit MaybeLock(Mutex& mutex, int threadCount) explicit MaybeLock(Mutex& mutex, unsigned threadCount)
: mImpl(makeLock(mutex, threadCount)) {} : mImpl(makeLock(mutex, threadCount)) {}
private: private:
@ -132,7 +129,7 @@ namespace
{ {
const Impl& mImpl; const Impl& mImpl;
std::shared_mutex& mCollisionWorldMutex; std::shared_mutex& mCollisionWorldMutex;
const int mNumJobs; const unsigned mNumThreads;
template <class Ptr, class FrameData> template <class Ptr, class FrameData>
void operator()(MWPhysics::SimulationImpl<Ptr, FrameData>& sim) const void operator()(MWPhysics::SimulationImpl<Ptr, FrameData>& sim) const
@ -144,7 +141,7 @@ namespace
// Locked shared_ptr has to be destructed after releasing mCollisionWorldMutex to avoid // Locked shared_ptr has to be destructed after releasing mCollisionWorldMutex to avoid
// possible deadlock. Ptr destructor also acquires mCollisionWorldMutex. // possible deadlock. Ptr destructor also acquires mCollisionWorldMutex.
const std::pair arg(std::move(ptr), frameData); const std::pair arg(std::move(ptr), frameData);
const Lock<std::shared_mutex> lock(mCollisionWorldMutex, mNumJobs); const Lock<std::shared_mutex> lock(mCollisionWorldMutex, mNumThreads);
mImpl(arg); mImpl(arg);
} }
}; };
@ -288,7 +285,7 @@ namespace
namespace Config namespace Config
{ {
/// @return either the number of thread as configured by the user, or 1 if Bullet doesn't support multithreading and user requested more than 1 background threads /// @return either the number of thread as configured by the user, or 1 if Bullet doesn't support multithreading and user requested more than 1 background threads
int computeNumThreads() unsigned computeNumThreads()
{ {
int wantedThread = Settings::Manager::getInt("async num threads", "Physics"); int wantedThread = Settings::Manager::getInt("async num threads", "Physics");
@ -300,7 +297,7 @@ namespace
Log(Debug::Warning) << "Bullet was not compiled with multithreading support, 1 async thread will be used"; Log(Debug::Warning) << "Bullet was not compiled with multithreading support, 1 async thread will be used";
return 1; return 1;
} }
return std::max(0, wantedThread); return static_cast<unsigned>(std::max(0, wantedThread));
} }
} }
} }
@ -335,7 +332,7 @@ namespace MWPhysics
{ {
if (mNumThreads >= 1) if (mNumThreads >= 1)
{ {
for (int i = 0; i < mNumThreads; ++i) for (unsigned i = 0; i < mNumThreads; ++i)
mThreads.emplace_back([&] { worker(); } ); mThreads.emplace_back([&] { worker(); } );
} }
else else

View File

@ -92,7 +92,7 @@ namespace MWPhysics
std::unique_ptr<Misc::Barrier> mPostStepBarrier; std::unique_ptr<Misc::Barrier> mPostStepBarrier;
std::unique_ptr<Misc::Barrier> mPostSimBarrier; std::unique_ptr<Misc::Barrier> mPostSimBarrier;
int mNumThreads; unsigned mNumThreads;
int mNumJobs; int mNumJobs;
int mRemainingSteps; int mRemainingSteps;
int mLOSCacheExpiry; int mLOSCacheExpiry;

View File

@ -1,7 +1,6 @@
#ifndef OPENMW_BARRIER_H #ifndef OPENMW_BARRIER_H
#define OPENMW_BARRIER_H #define OPENMW_BARRIER_H
#include <cassert>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
@ -12,9 +11,8 @@ namespace Misc
{ {
public: public:
/// @param count number of threads to wait on /// @param count number of threads to wait on
explicit Barrier(int count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0) explicit Barrier(unsigned count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0)
{ {
assert(count >= 0);
} }
/// @brief stop execution of threads until count distinct threads reach this point /// @brief stop execution of threads until count distinct threads reach this point