TaskSchedulerImpl for POSIX and compilation fixes
Former-commit-id: a402d40ac90cacf444b5832c49cfbdaf61f7f747
This commit is contained in:
parent
602dd561d2
commit
2cb669a558
|
|
@ -8,11 +8,12 @@
|
||||||
#define NAZARA_CONDITIONVARIABLE_HPP
|
#define NAZARA_CONDITIONVARIABLE_HPP
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
|
#include <Nazara/Core/NonCopyable.hpp>
|
||||||
|
|
||||||
class NzConditionVariableImpl;
|
class NzConditionVariableImpl;
|
||||||
class NzMutex;
|
class NzMutex;
|
||||||
|
|
||||||
class NAZARA_API NzConditionVariable
|
class NAZARA_API NzConditionVariable : NzNonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NzConditionVariable();
|
NzConditionVariable();
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@
|
||||||
#include <Nazara/Core/ThreadSafetyOff.hpp>
|
#include <Nazara/Core/ThreadSafetyOff.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
class NzFileImpl;
|
class NzFileImpl;
|
||||||
|
|
||||||
class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable
|
class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,9 @@ class NAZARA_API NzMutex : NzNonCopyable
|
||||||
~NzMutex();
|
~NzMutex();
|
||||||
|
|
||||||
void Lock();
|
void Lock();
|
||||||
|
|
||||||
bool TryLock();
|
bool TryLock();
|
||||||
|
|
||||||
void Unlock();
|
void Unlock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@ class NAZARA_API NzSemaphore : NzNonCopyable
|
||||||
~NzSemaphore();
|
~NzSemaphore();
|
||||||
|
|
||||||
unsigned int GetCount() const;
|
unsigned int GetCount() const;
|
||||||
|
|
||||||
void Post();
|
void Post();
|
||||||
|
|
||||||
void Wait();
|
void Wait();
|
||||||
bool Wait(nzUInt32 timeout);
|
bool Wait(nzUInt32 timeout);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <Nazara/Core/String.hpp>
|
#include <Nazara/Core/String.hpp>
|
||||||
#include <Nazara/Math/Config.hpp>
|
#include <Nazara/Math/Config.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <errno.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <new>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
|
||||||
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
|
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
|
||||||
{
|
{
|
||||||
NazaraUnused(parent);
|
NazaraUnused(parent);
|
||||||
|
|
@ -82,13 +85,10 @@ bool NzDirectoryImpl::Exists(const NzString& dirPath)
|
||||||
NzString NzDirectoryImpl::GetCurrent()
|
NzString NzDirectoryImpl::GetCurrent()
|
||||||
{
|
{
|
||||||
NzString currentPath;
|
NzString currentPath;
|
||||||
char* path = getcwd(nullptr, 0);
|
|
||||||
|
|
||||||
if (path)
|
char path[MAXPATHLEN];
|
||||||
{
|
if (getcwd(path, MAXPATHLEN))
|
||||||
currentPath = path;
|
currentPath = path;
|
||||||
free(path);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
NazaraError("Unable to get current directory: " + NzError::GetLastSystemError()); // Bug: initialisation -> if no path for log !
|
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/Posix/FileImpl.hpp>
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <stdio.h>
|
#include <cstdio>
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
NzFileImpl::NzFileImpl(const NzFile* parent) :
|
NzFileImpl::NzFileImpl(const NzFile* parent) :
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,16 @@
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/Debug.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
|
// Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
|
||||||
asm volatile ("cpuid" // Besoin d'être volatile ?
|
asm volatile ("cpuid" // Besoin d'être volatile ?
|
||||||
: "=a" (result[0]), "=b" (result[1]), "=c" (result[2]), "=d" (result[3]) // output
|
: "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) // output
|
||||||
: "a" (code), "c" (0)); // input
|
: "a" (functionId), "c" (subFunctionId)); // input
|
||||||
#else
|
#else
|
||||||
NazaraInternalError("Cpuid has been called although it is not supported");
|
NazaraInternalError("Cpuid has been called although it is not supported");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
||||||
|
|
@ -26,30 +26,30 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
|
||||||
|
|
||||||
bool NzHardwareInfoImpl::IsCpuidSupported()
|
bool NzHardwareInfoImpl::IsCpuidSupported()
|
||||||
{
|
{
|
||||||
#ifdef NAZARA_PLATFORM_x64
|
#ifdef NAZARA_PLATFORM_x64
|
||||||
return true; // Toujours supporté sur un processeur 64 bits
|
return true; // Toujours supporté sur un processeur 64 bits
|
||||||
#else
|
#else
|
||||||
#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)
|
||||||
int supported;
|
int supported;
|
||||||
asm volatile (" pushfl\n"
|
asm volatile (" pushfl\n"
|
||||||
" pop %%eax\n"
|
" pop %%eax\n"
|
||||||
" mov %%eax, %%ecx\n"
|
" mov %%eax, %%ecx\n"
|
||||||
" xor $0x200000, %%eax\n"
|
" xor $0x200000, %%eax\n"
|
||||||
" push %%eax\n"
|
" push %%eax\n"
|
||||||
" popfl\n"
|
" popfl\n"
|
||||||
" pushfl\n"
|
" pushfl\n"
|
||||||
" pop %%eax\n"
|
" pop %%eax\n"
|
||||||
" xor %%ecx, %%eax\n"
|
" xor %%ecx, %%eax\n"
|
||||||
" mov %%eax, %0\n"
|
" mov %%eax, %0\n"
|
||||||
" push %%ecx\n"
|
" push %%ecx\n"
|
||||||
" popfl"
|
" popfl"
|
||||||
: "=m" (supported) // output
|
: "=m" (supported) // output
|
||||||
: // input
|
: // input
|
||||||
: "eax", "ecx", "memory"); // clobbered register
|
: "eax", "ecx", "memory"); // clobbered register
|
||||||
|
|
||||||
return supported != 0;
|
return supported != 0;
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
class NzHardwareInfoImpl
|
class NzHardwareInfoImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void Cpuid(nzUInt32 code, nzUInt32 result[4]);
|
static void Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4]);
|
||||||
static unsigned int GetProcessorCount();
|
static unsigned int GetProcessorCount();
|
||||||
static bool IsCpuidSupported();
|
static bool IsCpuidSupported();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,207 @@
|
||||||
|
// 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>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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 commence par vider la queue et demander qu'ils s'arrêtent.
|
||||||
|
s_shouldFinish = true;
|
||||||
|
|
||||||
|
ClearQueue();
|
||||||
|
|
||||||
|
{
|
||||||
|
// On réveille les threads pour qu'ils sortent de la boucle et terminent.
|
||||||
|
pthread_mutex_lock(&s_mutexQueue);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NzTaskSchedulerImpl::ClearQueue()
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&s_mutexQueue);
|
||||||
|
std::queue<NzFunctor*> emptyQueue;
|
||||||
|
std::swap(s_tasks, emptyQueue);
|
||||||
|
pthread_mutex_unlock(&s_mutexQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (!s_isDone)
|
||||||
|
{
|
||||||
|
s_isWaiting = true;
|
||||||
|
pthread_mutex_lock(&s_mutexQueue);
|
||||||
|
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,48 @@
|
||||||
|
// 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 void ClearQueue();
|
||||||
|
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
|
||||||
Loading…
Reference in New Issue