Merge branch 'master' into NDK
Conflicts: include/Nazara/Core/ConditionVariable.hpp Former-commit-id: 84320f0a55ae9d8f80ff5211d30d99184c7b4213
This commit is contained in:
commit
dea48a289d
|
|
@ -128,7 +128,7 @@ return {
|
|||
{
|
||||
Title = "TaskScheduler",
|
||||
Description = "Classe permettant de répartir X tâches sur Y workers de façon efficace.",
|
||||
Portability = Feature.Windows
|
||||
Portability = Feature.Windows + Feature.POSIX
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -196,8 +196,7 @@ return {
|
|||
{
|
||||
Title = "Récupération d'informations sur le hardware",
|
||||
Description = "Classe permettant, si la plateforme le supporte, de récupérer des informations utiles sur le matériel: nom et fabricant du processeur, nombre de coeurs, support d'un set d'extension (exemple: SSE), quantité de mémoire vive, exécution de l'instruction CPUID.",
|
||||
Note = "Il manque de quoi récupérer la quantité de mémoire vive",
|
||||
Progress = 90,
|
||||
Progress = 100,
|
||||
Portability = Feature.Windows + Feature.POSIX
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@
|
|||
#define NAZARA_CONDITIONVARIABLE_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/NonCopyable.hpp>
|
||||
|
||||
class NzConditionVariableImpl;
|
||||
class NzMutex;
|
||||
|
||||
class NAZARA_CORE_API NzConditionVariable
|
||||
class NAZARA_CORE_API NzConditionVariable : NzNonCopyable
|
||||
{
|
||||
public:
|
||||
NzConditionVariable();
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
#include <Nazara/Core/ThreadSafetyOff.hpp>
|
||||
#endif
|
||||
|
||||
#include <ctime>
|
||||
|
||||
class NzFileImpl;
|
||||
|
||||
class NAZARA_CORE_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class NAZARA_CORE_API NzHardwareInfo
|
|||
static unsigned int GetProcessorCount();
|
||||
static nzProcessorVendor GetProcessorVendor();
|
||||
static NzString GetProcessorVendorName();
|
||||
static nzUInt64 GetTotalMemory();
|
||||
|
||||
static bool HasCapability(nzProcessorCap capability);
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ class NAZARA_CORE_API NzMutex : NzNonCopyable
|
|||
~NzMutex();
|
||||
|
||||
void Lock();
|
||||
|
||||
bool TryLock();
|
||||
|
||||
void Unlock();
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@ class NAZARA_CORE_API NzSemaphore : NzNonCopyable
|
|||
~NzSemaphore();
|
||||
|
||||
unsigned int GetCount() const;
|
||||
|
||||
void Post();
|
||||
|
||||
void Wait();
|
||||
bool Wait(nzUInt32 timeout);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Math/Config.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include <windows.h>
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
#include <cstring>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
|
|
|||
|
|
@ -116,6 +116,13 @@ NzString NzHardwareInfo::GetProcessorVendorName()
|
|||
return vendorNames[s_vendorEnum+1];
|
||||
}
|
||||
|
||||
nzUInt64 NzHardwareInfo::GetTotalMemory()
|
||||
{
|
||||
///DOC: Ne nécessite pas l'initialisation de HardwareInfo pour fonctionner
|
||||
static nzUInt64 totalMemory = NzHardwareInfoImpl::GetTotalMemory();
|
||||
return totalMemory;
|
||||
}
|
||||
|
||||
bool NzHardwareInfo::HasCapability(nzProcessorCap capability)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@
|
|||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
|
||||
{
|
||||
NazaraUnused(parent);
|
||||
|
|
@ -82,13 +85,10 @@ bool NzDirectoryImpl::Exists(const NzString& dirPath)
|
|||
NzString NzDirectoryImpl::GetCurrent()
|
||||
{
|
||||
NzString currentPath;
|
||||
char* path = getcwd(nullptr, 0);
|
||||
|
||||
if (path)
|
||||
{
|
||||
char path[MAXPATHLEN];
|
||||
if (getcwd(path, MAXPATHLEN))
|
||||
currentPath = path;
|
||||
free(path);
|
||||
}
|
||||
else
|
||||
NazaraError("Unable to get current directory: " + NzError::GetLastSystemError()); // Bug: initialisation -> if no path for log !
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@
|
|||
|
||||
#include <Nazara/Core/Posix/FileImpl.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <cstdio>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
NzFileImpl::NzFileImpl(const NzFile* parent) :
|
||||
|
|
|
|||
|
|
@ -6,16 +6,16 @@
|
|||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
void NzHardwareInfoImpl::Cpuid(nzUInt32 code, nzUInt32 result[4])
|
||||
void NzHardwareInfoImpl::Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4])
|
||||
{
|
||||
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
|
||||
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
|
||||
// Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
|
||||
asm volatile ("cpuid" // Besoin d'être volatile ?
|
||||
: "=a" (result[0]), "=b" (result[1]), "=c" (result[2]), "=d" (result[3]) // output
|
||||
: "a" (code), "c" (0)); // input
|
||||
#else
|
||||
: "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) // output
|
||||
: "a" (functionId), "c" (subFunctionId)); // input
|
||||
#else
|
||||
NazaraInternalError("Cpuid has been called although it is not supported");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
||||
|
|
@ -24,32 +24,40 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
|||
return sysconf(_SC_NPROCESSORS_CONF);
|
||||
}
|
||||
|
||||
nzUInt64 NzHardwareInfoImpl::GetTotalMemory()
|
||||
{
|
||||
nzUInt64 pages = sysconf(_SC_PHYS_PAGES);
|
||||
nzUInt64 page_size = sysconf(_SC_PAGE_SIZE);
|
||||
|
||||
return pages * page_size;
|
||||
}
|
||||
|
||||
bool NzHardwareInfoImpl::IsCpuidSupported()
|
||||
{
|
||||
#ifdef NAZARA_PLATFORM_x64
|
||||
#ifdef NAZARA_PLATFORM_x64
|
||||
return true; // Toujours supporté sur un processeur 64 bits
|
||||
#else
|
||||
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
|
||||
int supported;
|
||||
asm volatile (" pushfl\n"
|
||||
" pop %%eax\n"
|
||||
" mov %%eax, %%ecx\n"
|
||||
" xor $0x200000, %%eax\n"
|
||||
" push %%eax\n"
|
||||
" popfl\n"
|
||||
" pushfl\n"
|
||||
" pop %%eax\n"
|
||||
" xor %%ecx, %%eax\n"
|
||||
" mov %%eax, %0\n"
|
||||
" push %%ecx\n"
|
||||
" popfl"
|
||||
: "=m" (supported) // output
|
||||
: // input
|
||||
: "eax", "ecx", "memory"); // clobbered register
|
||||
#else
|
||||
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
|
||||
int supported;
|
||||
asm volatile (" pushfl\n"
|
||||
" pop %%eax\n"
|
||||
" mov %%eax, %%ecx\n"
|
||||
" xor $0x200000, %%eax\n"
|
||||
" push %%eax\n"
|
||||
" popfl\n"
|
||||
" pushfl\n"
|
||||
" pop %%eax\n"
|
||||
" xor %%ecx, %%eax\n"
|
||||
" mov %%eax, %0\n"
|
||||
" push %%ecx\n"
|
||||
" popfl"
|
||||
: "=m" (supported) // output
|
||||
: // input
|
||||
: "eax", "ecx", "memory"); // clobbered register
|
||||
|
||||
return supported != 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
return supported != 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,9 @@
|
|||
class NzHardwareInfoImpl
|
||||
{
|
||||
public:
|
||||
static void Cpuid(nzUInt32 code, nzUInt32 result[4]);
|
||||
static void Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4]);
|
||||
static unsigned int GetProcessorCount();
|
||||
static nzUInt64 GetTotalMemory();
|
||||
static bool IsCpuidSupported();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,192 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Posix/TaskSchedulerImpl.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
bool NzTaskSchedulerImpl::Initialize(unsigned int workerCount)
|
||||
{
|
||||
if (IsInitialized())
|
||||
return true; // Déjà initialisé
|
||||
|
||||
#if NAZARA_CORE_SAFE
|
||||
if (workerCount == 0)
|
||||
{
|
||||
NazaraError("Invalid worker count ! (0)");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
s_workerCount = workerCount;
|
||||
s_isDone = false;
|
||||
s_isWaiting = false;
|
||||
s_shouldFinish = false;
|
||||
|
||||
s_threads.reset(new pthread_t[workerCount]);
|
||||
|
||||
// On initialise les conditions variables, mutex et barrière.
|
||||
pthread_cond_init(&s_cvEmpty, nullptr);
|
||||
pthread_cond_init(&s_cvNotEmpty, nullptr);
|
||||
pthread_mutex_init(&s_mutexQueue, nullptr);
|
||||
pthread_barrier_init(&s_barrier, nullptr, workerCount + 1);
|
||||
|
||||
for (unsigned int i = 0; i < s_workerCount; ++i)
|
||||
{
|
||||
// Le thread va se lancer, attendre que tous se créent et attendre d'être réveillé.
|
||||
pthread_create(&s_threads[i], nullptr, WorkerProc, nullptr);
|
||||
}
|
||||
|
||||
pthread_barrier_wait(&s_barrier); // On attend que les enfants soient bien créés.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzTaskSchedulerImpl::IsInitialized()
|
||||
{
|
||||
return s_workerCount > 0;
|
||||
}
|
||||
|
||||
void NzTaskSchedulerImpl::Run(NzFunctor** tasks, unsigned int count)
|
||||
{
|
||||
// On s'assure que des tâches ne sont pas déjà en cours
|
||||
Wait();
|
||||
|
||||
pthread_mutex_lock(&s_mutexQueue);
|
||||
s_isDone = false;
|
||||
|
||||
while (count--)
|
||||
s_tasks.push(*tasks++);
|
||||
|
||||
pthread_cond_signal(&s_cvNotEmpty);
|
||||
pthread_mutex_unlock(&s_mutexQueue);
|
||||
}
|
||||
|
||||
void NzTaskSchedulerImpl::Uninitialize()
|
||||
{
|
||||
#ifdef NAZARA_CORE_SAFE
|
||||
if (s_workerCount == 0)
|
||||
{
|
||||
NazaraError("Task scheduler is not initialized");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// On réveille les threads pour qu'ils sortent de la boucle et terminent.
|
||||
pthread_mutex_lock(&s_mutexQueue);
|
||||
// On commence par vider la queue et demander qu'ils s'arrêtent.
|
||||
std::queue<NzFunctor*> emptyQueue;
|
||||
std::swap(s_tasks, emptyQueue);
|
||||
s_shouldFinish = true;
|
||||
pthread_cond_broadcast(&s_cvNotEmpty);
|
||||
pthread_mutex_unlock(&s_mutexQueue);
|
||||
|
||||
// On attend que chaque thread se termine
|
||||
for (unsigned int i = 0; i < s_workerCount; ++i)
|
||||
pthread_join(s_threads[i], nullptr);
|
||||
|
||||
// Et on libère les ressources
|
||||
pthread_barrier_destroy(&s_barrier);
|
||||
pthread_cond_destroy(&s_cvEmpty);
|
||||
pthread_cond_destroy(&s_cvNotEmpty);
|
||||
pthread_mutex_destroy(&s_mutexQueue);
|
||||
|
||||
s_workerCount = 0;
|
||||
}
|
||||
|
||||
void NzTaskSchedulerImpl::WaitForTasks()
|
||||
{
|
||||
#ifdef NAZARA_CORE_SAFE
|
||||
if (s_workerCount == 0)
|
||||
{
|
||||
NazaraError("Task scheduler is not initialized");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Wait();
|
||||
}
|
||||
|
||||
NzFunctor* NzTaskSchedulerImpl::PopQueue()
|
||||
{
|
||||
NzFunctor* task = nullptr;
|
||||
|
||||
pthread_mutex_lock(&s_mutexQueue);
|
||||
|
||||
if (!s_tasks.empty())
|
||||
{
|
||||
task = s_tasks.front();
|
||||
s_tasks.pop();
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&s_mutexQueue);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
void NzTaskSchedulerImpl::Wait()
|
||||
{
|
||||
if (s_isDone)
|
||||
return;
|
||||
|
||||
pthread_mutex_lock(&s_mutexQueue);
|
||||
s_isWaiting = true;
|
||||
pthread_cond_broadcast(&s_cvNotEmpty);
|
||||
pthread_cond_wait(&s_cvEmpty, &s_mutexQueue);
|
||||
pthread_mutex_unlock(&s_mutexQueue);
|
||||
|
||||
s_isDone = true;
|
||||
}
|
||||
|
||||
void* NzTaskSchedulerImpl::WorkerProc(void* /*userdata*/)
|
||||
{
|
||||
// On s'assure que tous les threads soient correctement lancés.
|
||||
pthread_barrier_wait(&s_barrier);
|
||||
|
||||
// On quitte s'il doit terminer.
|
||||
while (!s_shouldFinish)
|
||||
{
|
||||
NzFunctor* task = PopQueue();
|
||||
|
||||
if (task)
|
||||
{
|
||||
// On exécute la tâche avant de la supprimer
|
||||
task->Run();
|
||||
delete task;
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_mutex_lock(&s_mutexQueue);
|
||||
if (s_tasks.empty())
|
||||
s_isDone = true;
|
||||
|
||||
while (!(!s_tasks.empty() || s_isWaiting || s_shouldFinish))
|
||||
pthread_cond_wait(&s_cvNotEmpty, &s_mutexQueue);
|
||||
|
||||
if (s_tasks.empty() && s_isWaiting)
|
||||
{
|
||||
// On prévient le thread qui attend que les tâches soient effectuées.
|
||||
s_isWaiting = false;
|
||||
pthread_cond_signal(&s_cvEmpty);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&s_mutexQueue);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::queue<NzFunctor*> NzTaskSchedulerImpl::s_tasks;
|
||||
std::unique_ptr<pthread_t[]> NzTaskSchedulerImpl::s_threads;
|
||||
std::atomic<bool> NzTaskSchedulerImpl::s_isDone;
|
||||
std::atomic<bool> NzTaskSchedulerImpl::s_isWaiting;
|
||||
std::atomic<bool> NzTaskSchedulerImpl::s_shouldFinish;
|
||||
unsigned int NzTaskSchedulerImpl::s_workerCount;
|
||||
|
||||
pthread_mutex_t NzTaskSchedulerImpl::s_mutexQueue;
|
||||
pthread_cond_t NzTaskSchedulerImpl::s_cvEmpty;
|
||||
pthread_cond_t NzTaskSchedulerImpl::s_cvNotEmpty;
|
||||
pthread_barrier_t NzTaskSchedulerImpl::s_barrier;
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_TASKSCHEDULERIMPL_HPP
|
||||
#define NAZARA_TASKSCHEDULERIMPL_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Functor.hpp>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <pthread.h>
|
||||
#include <queue>
|
||||
|
||||
class NzTaskSchedulerImpl
|
||||
{
|
||||
public:
|
||||
NzTaskSchedulerImpl() = delete;
|
||||
~NzTaskSchedulerImpl() = delete;
|
||||
|
||||
static bool Initialize(unsigned int workerCount);
|
||||
static bool IsInitialized();
|
||||
static void Run(NzFunctor** tasks, unsigned int count);
|
||||
static void Uninitialize();
|
||||
static void WaitForTasks();
|
||||
|
||||
private:
|
||||
static NzFunctor* PopQueue();
|
||||
static void Wait();
|
||||
static void* WorkerProc(void* userdata);
|
||||
|
||||
static std::queue<NzFunctor*> s_tasks;
|
||||
static std::unique_ptr<pthread_t[]> s_threads;
|
||||
static std::atomic<bool> s_isDone;
|
||||
static std::atomic<bool> s_isWaiting;
|
||||
static std::atomic<bool> s_shouldFinish;
|
||||
static unsigned int s_workerCount;
|
||||
|
||||
static pthread_mutex_t s_mutexQueue;
|
||||
static pthread_cond_t s_cvEmpty;
|
||||
static pthread_cond_t s_cvNotEmpty;
|
||||
static pthread_barrier_t s_barrier;
|
||||
};
|
||||
|
||||
#endif // NAZARA_TASKSCHEDULERIMPL_HPP
|
||||
|
|
@ -33,11 +33,20 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
|||
{
|
||||
// Plus simple (et plus portable) que de passer par le CPUID
|
||||
SYSTEM_INFO infos;
|
||||
GetSystemInfo(&infos);
|
||||
GetNativeSystemInfo(&infos);
|
||||
|
||||
return infos.dwNumberOfProcessors;
|
||||
}
|
||||
|
||||
nzUInt64 NzHardwareInfoImpl::GetTotalMemory()
|
||||
{
|
||||
MEMORYSTATUSEX memStatus;
|
||||
memStatus.dwLength = sizeof(memStatus);
|
||||
GlobalMemoryStatusEx(&memStatus);
|
||||
|
||||
return memStatus.ullTotalPhys;
|
||||
}
|
||||
|
||||
bool NzHardwareInfoImpl::IsCpuidSupported()
|
||||
{
|
||||
#ifdef NAZARA_PLATFORM_x64
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ class NzHardwareInfoImpl
|
|||
public:
|
||||
static void Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4]);
|
||||
static unsigned int GetProcessorCount();
|
||||
static nzUInt64 GetTotalMemory();
|
||||
static bool IsCpuidSupported();
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue