X::Initialize no longer takes arguments

Former-commit-id: a8233235e89112630f2d31c637723443bd0829a4
This commit is contained in:
Lynix
2013-04-03 16:41:57 +02:00
parent d8c5f4a0fd
commit 8694f71c2a
8 changed files with 47 additions and 51 deletions

View File

@@ -10,24 +10,9 @@
#include <Nazara/Core/TaskScheduler.hpp>
#include <Nazara/Core/Debug.hpp>
bool NzCore::Initialize(bool initializeHardwareInfo, bool initializeTaskScheduler)
bool NzCore::Initialize()
{
s_moduleReferenceCounter++;
// Initialisation du module
if (initializeHardwareInfo && !NzHardwareInfo::Initialize())
NazaraWarning("Failed to initialize hardware info"); // Non-critique
if (initializeTaskScheduler && !NzTaskScheduler::Initialize())
{
NazaraError("Failed to initialize task scheduler");
Uninitialize();
return false;
}
// Vérification après l'initialisation des sous-modules
if (s_moduleReferenceCounter != 1)
if (s_moduleReferenceCounter++ != 0)
return true; // Déjà initialisé
NazaraNotice("Initialized: Core");

View File

@@ -80,6 +80,7 @@ NzString NzHardwareInfo::GetProcessorBrandString()
unsigned int NzHardwareInfo::GetProcessorCount()
{
///DOC: Ne nécessite pas l'initialisation de HardwareInfo pour fonctionner
static unsigned int processorCount = std::max(NzHardwareInfoImpl::GetProcessorCount(), 1U);
return processorCount;
}

View File

@@ -5,6 +5,7 @@
#include <Nazara/Core/TaskScheduler.hpp>
#include <Nazara/Core/ConditionVariable.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/HardwareInfo.hpp>
#include <Nazara/Core/LockGuard.hpp>
#include <Nazara/Core/Mutex.hpp>
#include <Nazara/Core/Thread.hpp>
@@ -19,7 +20,7 @@ namespace
struct TaskSchedulerImpl
{
std::queue<NzFunctor*> tasks;
std::vector<NzThread*> workers;
std::vector<NzThread> workers;
NzConditionVariable waiterConditionVariable;
NzConditionVariable workerConditionVariable;
NzMutex taskMutex;
@@ -31,6 +32,7 @@ namespace
};
TaskSchedulerImpl* s_impl = nullptr;
unsigned int s_workerCount = 0;
void WorkerFunc()
{
@@ -84,31 +86,41 @@ namespace
unsigned int NzTaskScheduler::GetWorkerCount()
{
#ifdef NAZARA_CORE_SAFE
if (!s_impl)
{
NazaraError("Task scheduler is not initialized");
return 0;
}
#endif
return s_impl->workers.size();
return (s_workerCount > 0) ? s_workerCount : NzHardwareInfo::GetProcessorCount();
}
bool NzTaskScheduler::Initialize(unsigned int workerCount)
bool NzTaskScheduler::Initialize()
{
if (s_impl)
return true; // Déjà initialisé
s_impl = new TaskSchedulerImpl;
s_impl->workers.reserve(workerCount);
unsigned int workerCount = GetWorkerCount();
s_impl->workers.resize(workerCount);
for (unsigned int i = 0; i < workerCount; ++i)
s_impl->workers.push_back(new NzThread(WorkerFunc));
s_impl->workers[i] = NzThread(WorkerFunc);
return true;
}
void NzTaskScheduler::SetWorkerCount(unsigned int workerCount)
{
s_workerCount = workerCount;
if (s_impl)
{
unsigned int newWorkerCount = GetWorkerCount();
unsigned int oldWorkerCount = s_impl->workers.size();
s_impl->workers.resize(newWorkerCount);
if (newWorkerCount > oldWorkerCount)
{
for (unsigned int i = oldWorkerCount-1; i < newWorkerCount; ++i)
s_impl->workers[i] = NzThread(WorkerFunc);
}
}
}
void NzTaskScheduler::Uninitialize()
{
if (s_impl)
@@ -130,11 +142,8 @@ void NzTaskScheduler::Uninitialize()
s_impl->workerConditionVariable.SignalAll();
s_impl->workerConditionVariableMutex.Unlock();
for (NzThread* thread : s_impl->workers)
{
thread->Join();
delete thread;
}
for (NzThread& thread : s_impl->workers)
thread.Join();
delete s_impl;
s_impl = nullptr;