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

@ -16,7 +16,7 @@ class NAZARA_API NzCore
NzCore() = delete; NzCore() = delete;
~NzCore() = delete; ~NzCore() = delete;
static bool Initialize(bool initializeHardwareInfo = false, bool initializeTaskScheduler = false); static bool Initialize();
static bool IsInitialized(); static bool IsInitialized();

View File

@ -21,7 +21,8 @@ class NAZARA_API NzTaskScheduler
template<typename F, typename... Args> static void AddTask(F function, Args... args); template<typename F, typename... Args> static void AddTask(F function, Args... args);
template<typename C> static void AddTask(void (C::*function)(), C* object); template<typename C> static void AddTask(void (C::*function)(), C* object);
static unsigned int GetWorkerCount(); static unsigned int GetWorkerCount();
static bool Initialize(unsigned int workerCount = NzThread::HardwareConcurrency()); static bool Initialize();
static void SetWorkerCount(unsigned int workerCount);
static void Uninitialize(); static void Uninitialize();
static void WaitForTasks(); static void WaitForTasks();

View File

@ -59,7 +59,7 @@ class NAZARA_API NzRenderer
static bool HasCapability(nzRendererCap capability); static bool HasCapability(nzRendererCap capability);
static bool Initialize(bool initializeDebugDrawer = false); static bool Initialize();
static bool IsEnabled(nzRendererParameter parameter); static bool IsEnabled(nzRendererParameter parameter);
static bool IsInitialized(); static bool IsInitialized();

View File

@ -10,24 +10,9 @@
#include <Nazara/Core/TaskScheduler.hpp> #include <Nazara/Core/TaskScheduler.hpp>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
bool NzCore::Initialize(bool initializeHardwareInfo, bool initializeTaskScheduler) bool NzCore::Initialize()
{ {
s_moduleReferenceCounter++; if (s_moduleReferenceCounter++ != 0)
// 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)
return true; // Déjà initialisé return true; // Déjà initialisé
NazaraNotice("Initialized: Core"); NazaraNotice("Initialized: Core");

View File

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

View File

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

View File

@ -616,15 +616,10 @@ bool NzRenderer::HasCapability(nzRendererCap capability)
return s_capabilities[capability]; return s_capabilities[capability];
} }
bool NzRenderer::Initialize(bool initializeDebugDrawer) bool NzRenderer::Initialize()
{ {
if (s_moduleReferenceCounter++ != 0) if (s_moduleReferenceCounter++ != 0)
{
if (initializeDebugDrawer && !NzDebugDrawer::Initialize())
NazaraWarning("Failed to initialize debug drawer"); // Non-critique
return true; // Déjà initialisé return true; // Déjà initialisé
}
// Initialisation des dépendances // Initialisation des dépendances
if (!NzUtility::Initialize()) if (!NzUtility::Initialize())
@ -761,9 +756,6 @@ bool NzRenderer::Initialize(bool initializeDebugDrawer)
s_quadBuffer = new NzVertexBuffer(declaration.get(), 4, nzBufferStorage_Hardware, nzBufferUsage_Dynamic); s_quadBuffer = new NzVertexBuffer(declaration.get(), 4, nzBufferStorage_Hardware, nzBufferUsage_Dynamic);
declaration.release(); declaration.release();
if (initializeDebugDrawer && !NzDebugDrawer::Initialize())
NazaraWarning("Failed to initialize debug drawer"); // Non-critique
if (!NzShaderBuilder::Initialize()) if (!NzShaderBuilder::Initialize())
{ {
NazaraError("Failed to initialize shader builder"); NazaraError("Failed to initialize shader builder");

View File

@ -5,7 +5,9 @@
#include <Nazara/Utility/Utility.hpp> #include <Nazara/Utility/Utility.hpp>
#include <Nazara/Core/Core.hpp> #include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/HardwareInfo.hpp>
#include <Nazara/Core/Log.hpp> #include <Nazara/Core/Log.hpp>
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Utility/Buffer.hpp> #include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Loaders/MD2.hpp> #include <Nazara/Utility/Loaders/MD2.hpp>
@ -23,11 +25,7 @@ bool NzUtility::Initialize()
return true; // Déjà initialisé return true; // Déjà initialisé
// Initialisation des dépendances // Initialisation des dépendances
#if NAZARA_UTILITY_MULTITHREADED_SKINNING
if (!NzCore::Initialize(false, true))
#else
if (!NzCore::Initialize()) if (!NzCore::Initialize())
#endif
{ {
NazaraError("Failed to initialize core module"); NazaraError("Failed to initialize core module");
Uninitialize(); Uninitialize();
@ -36,6 +34,16 @@ bool NzUtility::Initialize()
} }
// Initialisation du module // Initialisation du module
#if NAZARA_UTILITY_MULTITHREADED_SKINNING
if (!NzTaskScheduler::Initialize())
{
NazaraError("Failed to initialize task scheduler");
Uninitialize();
return false;
}
#endif
if (!NzBuffer::Initialize()) if (!NzBuffer::Initialize())
{ {
NazaraError("Failed to initialize buffers"); NazaraError("Failed to initialize buffers");