Made NzThread interface mimic std::thread one

Hopefully fixed the threaded window bug


Former-commit-id: 6dc3ca2a8bee1da591a9b97d202d4b73b10be8eb
This commit is contained in:
Lynix 2012-11-29 16:51:01 +01:00
parent a2eb55e74a
commit cbc98ce3f0
7 changed files with 155 additions and 140 deletions

View File

@ -2,8 +2,6 @@
// This file is part of the "Nazara Engine - Core module" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
// Inspiré du code de la SFML par Laurent Gomila
#pragma once #pragma once
#ifndef NAZARA_THREAD_HPP #ifndef NAZARA_THREAD_HPP
@ -12,53 +10,56 @@
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Functor.hpp> #include <Nazara/Core/Functor.hpp>
#include <Nazara/Core/NonCopyable.hpp> #include <Nazara/Core/NonCopyable.hpp>
#include <ostream>
class NzThreadImpl; class NzThreadImpl;
class NAZARA_API NzThread : NzNonCopyable class NAZARA_API NzThread : NzNonCopyable
{ {
friend NzThreadImpl;
public: public:
class NAZARA_API Id class NAZARA_API Id
{ {
friend NzThread; friend NzThread;
friend NzThreadImpl;
public: public:
Id() = default; Id() = default;
Id(Id&& rhs) = default;
~Id();
Id& operator=(Id&& rhs) = default; NAZARA_API friend bool operator==(const Id& lhs, const Id& rhs);
bool operator==(const Id& rhs) const; NAZARA_API friend bool operator!=(const Id& lhs, const Id& rhs);
bool operator!=(const Id& rhs) const; NAZARA_API friend bool operator<(const Id& lhs, const Id& rhs);
NAZARA_API friend bool operator<=(const Id& lhs, const Id& rhs);
NAZARA_API friend bool operator>(const Id& lhs, const Id& rhs);
NAZARA_API friend bool operator>=(const Id& lhs, const Id& rhs);
NAZARA_API friend bool operator<<(std::ostream& o, const Id& id);
private: private:
Id(void* handle) : m_handle(handle) {} Id(NzThreadImpl* thread);
Id(const NzThreadImpl* thread);
void* m_handle = nullptr; NzThreadImpl* m_id = nullptr;
}; };
NzThread() = default;
NzThread(NzThread&& other);
template<typename F> NzThread(F function); template<typename F> NzThread(F function);
template<typename F, typename... Args> NzThread(F function, Args... args); template<typename F, typename... Args> NzThread(F function, Args... args);
template<typename C> NzThread(void (C::*function)(), C* object); template<typename C> NzThread(void (C::*function)(), C* object);
~NzThread(); ~NzThread();
void Detach();
Id GetId() const; Id GetId() const;
bool IsCurrent() const; bool IsJoinable() const;
void Launch(bool independent = false);
void Join(); void Join();
void Terminate();
static Id GetCurrentId(); NzThread& operator=(NzThread&& thread);
static void Sleep(nzUInt32 time);
static unsigned int HardwareConcurrency();
static void Sleep(nzUInt32 milliseconds);
private: private:
NzFunctor* m_func; void CreateImpl(NzFunctor* functor);
NzThreadImpl* m_impl = nullptr; NzThreadImpl* m_impl = nullptr;
bool m_independent;
}; };
#include <Nazara/Core/Thread.inl> #include <Nazara/Core/Thread.inl>

View File

@ -5,21 +5,21 @@
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
template<typename F> template<typename F>
NzThread::NzThread(F function) : NzThread::NzThread(F function)
m_func(new NzFunctorWithoutArgs<F>(function))
{ {
CreateImpl(new NzFunctorWithoutArgs<F>(function));
} }
template<typename F, typename... Args> template<typename F, typename... Args>
NzThread::NzThread(F function, Args... args) : NzThread::NzThread(F function, Args... args)
m_func(new NzFunctorWithArgs<F, Args...>(function, args...))
{ {
CreateImpl(new NzFunctorWithArgs<F, Args...>(function, args...));
} }
template<typename C> template<typename C>
NzThread::NzThread(void (C::*function)(), C* object) : NzThread::NzThread(void (C::*function)(), C* object)
m_func(new NzMemberWithoutArgs<C>(function, object))
{ {
CreateImpl(new NzMemberWithoutArgs<C>(function, object));
} }
#include <Nazara/Core/DebugOff.hpp> #include <Nazara/Core/DebugOff.hpp>

View File

@ -42,7 +42,7 @@
#define NAZARA_UTILITY_STRICT_RESOURCE_PARSING 1 #define NAZARA_UTILITY_STRICT_RESOURCE_PARSING 1
// Fait tourner chaque fenêtre dans un thread séparé si le système le supporte // Fait tourner chaque fenêtre dans un thread séparé si le système le supporte
#define NAZARA_UTILITY_THREADED_WINDOW 0 ///FIXME: Buggé depuis GCC 4.7 avec certains ordinateurs #define NAZARA_UTILITY_THREADED_WINDOW 0
// Protège les classes des accès concurrentiels // Protège les classes des accès concurrentiels
#define NAZARA_UTILITY_THREADSAFE 1 #define NAZARA_UTILITY_THREADSAFE 1

View File

@ -7,6 +7,8 @@
#include <Nazara/Core/Thread.hpp> #include <Nazara/Core/Thread.hpp>
#include <Nazara/Core/Config.hpp> #include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/HardwareInfo.hpp>
#include <stdexcept>
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/ThreadImpl.hpp> #include <Nazara/Core/Win32/ThreadImpl.hpp>
@ -18,62 +20,127 @@
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
///********************************NzThread::Id********************************
bool operator==(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id == rhs.m_id;
}
bool operator!=(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id != rhs.m_id;
}
bool operator<(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id < rhs.m_id;
}
bool operator<=(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id <= rhs.m_id;
}
bool operator>(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id > rhs.m_id;
}
bool operator>=(const NzThread::Id& lhs, const NzThread::Id& rhs)
{
return lhs.m_id >= rhs.m_id;
}
bool operator<<(std::ostream& o, const NzThread::Id& id)
{
o << id.m_id;
return o;
}
NzThread::Id::Id(NzThreadImpl* thread) :
m_id(thread)
{
}
///**********************************NzThread**********************************
NzThread::NzThread(NzThread&& other) :
m_impl(other.m_impl)
{
other.m_impl = nullptr;
}
NzThread::~NzThread() NzThread::~NzThread()
{
if (!m_independent)
Join();
else
delete m_impl;
}
NzThread::Id NzThread::GetId() const
{
if (m_impl)
return m_impl->GetId();
else
return NzThread::Id();
}
bool NzThread::IsCurrent() const
{
if (m_impl)
return m_impl->IsCurrent();
else
return false;
}
void NzThread::Launch(bool independent)
{
Join();
m_independent = independent;
m_impl = new NzThreadImpl(this);
}
void NzThread::Join()
{ {
if (m_impl) if (m_impl)
{ {
#if NAZARA_CORE_SAFE
if (m_impl->IsCurrent())
{
NazaraError("A thread cannot join itself");
return;
}
#endif
m_impl->Join(); m_impl->Join();
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
} }
} }
void NzThread::Terminate() void NzThread::Detach()
{ {
if (m_impl) if (m_impl)
{ {
m_impl->Terminate(); m_impl->Detach();
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
} }
} }
NzThread::Id NzThread::GetId() const
{
return NzThread::Id(m_impl);
}
bool NzThread::IsJoinable() const
{
return m_impl != nullptr;
}
void NzThread::Join()
{
#if NAZARA_CORE_SAFE
if (!m_impl)
{
NazaraError("This thread is not joinable");
return;
}
#endif
m_impl->Join();
delete m_impl;
m_impl = nullptr;
}
NzThread& NzThread::operator=(NzThread&& thread)
{
#if NAZARA_CORE_SAFE
if (m_impl)
{
NazaraError("This thread cannot be joined");
std::terminate();
}
#endif
std::swap(m_impl, thread.m_impl);
return *this;
}
unsigned int NzThread::HardwareConcurrency()
{
return NzHardwareInfo::GetProcessorCount();
}
void NzThread::Sleep(nzUInt32 milliseconds)
{
NzThreadImpl::Sleep(milliseconds);
}
void NzThread::CreateImpl(NzFunctor* functor)
{
m_impl = new NzThreadImpl(functor);
}

View File

@ -2,93 +2,46 @@
// This file is part of the "Nazara Engine - Core module" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
// Inspiré du code de la SFML par Laurent Gomila
#include <Nazara/Core/Win32/ThreadImpl.hpp> #include <Nazara/Core/Win32/ThreadImpl.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Functor.hpp> #include <Nazara/Core/Functor.hpp>
#include <process.h> #include <process.h>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzThread::Id::Id(const NzThreadImpl* thread) NzThreadImpl::NzThreadImpl(NzFunctor* functor)
{ {
if (thread->m_thread) m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &NzThreadImpl::ThreadProc, functor, 0, nullptr));
m_handle = reinterpret_cast<void*>(thread->m_threadId); // Un entier transformé en pointeur : Hacky if (!m_handle)
else NazaraInternalError("Failed to create thread: " + NzGetLastSystemError());
m_handle = nullptr;
} }
NzThread::Id::~Id() void NzThreadImpl::Detach()
{ {
} // http://stackoverflow.com/questions/418742/is-it-reasonable-to-call-closehandle-on-a-thread-before-it-terminates
CloseHandle(m_handle);
bool NzThread::Id::operator==(const Id& rhs) const
{
return m_handle == rhs.m_handle;
}
bool NzThread::Id::operator!=(const Id& rhs) const
{
return m_handle != rhs.m_handle;
}
NzThreadImpl::NzThreadImpl(NzThread* thread)
{
m_thread = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &NzThreadImpl::ThreadProc, thread, 0, &m_threadId));
if (!m_thread)
NazaraError("Failed to create thread: " + NzGetLastSystemError());
}
NzThreadImpl::~NzThreadImpl()
{
}
NzThread::Id NzThreadImpl::GetId() const
{
return NzThread::Id(reinterpret_cast<void*>(m_threadId)); // Hacky
}
bool NzThreadImpl::IsCurrent() const
{
return m_threadId == GetCurrentThreadId();
} }
void NzThreadImpl::Join() void NzThreadImpl::Join()
{ {
if (m_thread) WaitForSingleObject(m_handle, INFINITE);
WaitForSingleObject(m_thread, INFINITE); CloseHandle(m_handle);
}
void NzThreadImpl::Terminate()
{
if (m_thread)
TerminateThread(m_thread, 0);
} }
unsigned int __stdcall NzThreadImpl::ThreadProc(void* userdata) unsigned int __stdcall NzThreadImpl::ThreadProc(void* userdata)
{ {
NzThread* owner = reinterpret_cast<NzThread*>(userdata); NzFunctor* func = static_cast<NzFunctor*>(userdata);
NzFunctor* func = owner->m_func;
HANDLE myHandle = owner->m_impl->m_thread;
func->Run(); func->Run();
delete func; delete func;
// http://stackoverflow.com/questions/418742/is-it-reasonable-to-call-closehandle-on-a-thread-before-it-terminates
CloseHandle(myHandle);
/* /*
En C++, il vaut mieux retourner depuis la fonction que de quitter le thread explicitement En C++, il vaut mieux retourner depuis la fonction que de quitter le thread explicitement
Source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682659(v=vs.85).aspx Source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682659(v=vs.85).aspx
*/ */
return 0; return 0;
} }
NzThread::Id NzThread::GetCurrentId() void NzThreadImpl::Sleep(nzUInt32 time)
{
return NzThread::Id(reinterpret_cast<void*>(GetCurrentThreadId())); // Hacky
}
void NzThread::Sleep(nzUInt32 time)
{ {
::Sleep(time); ::Sleep(time);
} }

View File

@ -10,29 +10,24 @@
#define NAZARA_THREADIMPL_HPP #define NAZARA_THREADIMPL_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Thread.hpp>
#include <windows.h> #include <windows.h>
class NzThread; struct NzFunctor;
class NzThreadImpl class NzThreadImpl
{ {
friend class NzThread::Id;
public: public:
NzThreadImpl(NzThread* threadFunc); NzThreadImpl(NzFunctor* threadFunc);
~NzThreadImpl();
NzThread::Id GetId() const; void Detach();
bool IsCurrent() const;
void Join(); void Join();
void Terminate();
static void Sleep(nzUInt32 time);
private: private:
static unsigned int __stdcall ThreadProc(void* userdata); static unsigned int __stdcall ThreadProc(void* userdata);
HANDLE m_thread; HANDLE m_handle;
unsigned int m_threadId;
}; };
#endif // NAZARA_THREADIMPL_HPP #endif // NAZARA_THREADIMPL_HPP

View File

@ -123,12 +123,11 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
#if NAZARA_UTILITY_THREADED_WINDOW #if NAZARA_UTILITY_THREADED_WINDOW
NzMutex mutex; NzMutex mutex;
NzConditionVariable condition; NzConditionVariable condition;
m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle, win32Style, x, y, width, height, this, &mutex, &condition);
m_threadActive = true; m_threadActive = true;
// On attend que la fenêtre soit créée // On attend que la fenêtre soit créée
mutex.Lock(); mutex.Lock();
m_thread->Launch(); m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle, win32Style, x, y, width, height, this, &mutex, &condition);
condition.Wait(&mutex); condition.Wait(&mutex);
mutex.Unlock(); mutex.Unlock();
#else #else