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

View File

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

View File

@ -42,7 +42,7 @@
#define NAZARA_UTILITY_STRICT_RESOURCE_PARSING 1
// 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
#define NAZARA_UTILITY_THREADSAFE 1

View File

@ -7,6 +7,8 @@
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/HardwareInfo.hpp>
#include <stdexcept>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/ThreadImpl.hpp>
@ -18,62 +20,127 @@
#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()
{
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 NAZARA_CORE_SAFE
if (m_impl->IsCurrent())
{
NazaraError("A thread cannot join itself");
return;
}
#endif
m_impl->Join();
delete m_impl;
m_impl = nullptr;
}
}
void NzThread::Terminate()
void NzThread::Detach()
{
if (m_impl)
{
m_impl->Terminate();
m_impl->Detach();
delete m_impl;
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"
// 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/Error.hpp>
#include <Nazara/Core/Functor.hpp>
#include <process.h>
#include <Nazara/Core/Debug.hpp>
NzThread::Id::Id(const NzThreadImpl* thread)
NzThreadImpl::NzThreadImpl(NzFunctor* functor)
{
if (thread->m_thread)
m_handle = reinterpret_cast<void*>(thread->m_threadId); // Un entier transformé en pointeur : Hacky
else
m_handle = nullptr;
m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &NzThreadImpl::ThreadProc, functor, 0, nullptr));
if (!m_handle)
NazaraInternalError("Failed to create thread: " + NzGetLastSystemError());
}
NzThread::Id::~Id()
void NzThreadImpl::Detach()
{
}
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();
// http://stackoverflow.com/questions/418742/is-it-reasonable-to-call-closehandle-on-a-thread-before-it-terminates
CloseHandle(m_handle);
}
void NzThreadImpl::Join()
{
if (m_thread)
WaitForSingleObject(m_thread, INFINITE);
}
void NzThreadImpl::Terminate()
{
if (m_thread)
TerminateThread(m_thread, 0);
WaitForSingleObject(m_handle, INFINITE);
CloseHandle(m_handle);
}
unsigned int __stdcall NzThreadImpl::ThreadProc(void* userdata)
{
NzThread* owner = reinterpret_cast<NzThread*>(userdata);
NzFunctor* func = owner->m_func;
HANDLE myHandle = owner->m_impl->m_thread;
NzFunctor* func = static_cast<NzFunctor*>(userdata);
func->Run();
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
Source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682659(v=vs.85).aspx
*/
return 0;
}
NzThread::Id NzThread::GetCurrentId()
{
return NzThread::Id(reinterpret_cast<void*>(GetCurrentThreadId())); // Hacky
}
void NzThread::Sleep(nzUInt32 time)
void NzThreadImpl::Sleep(nzUInt32 time)
{
::Sleep(time);
}

View File

@ -10,29 +10,24 @@
#define NAZARA_THREADIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Thread.hpp>
#include <windows.h>
class NzThread;
struct NzFunctor;
class NzThreadImpl
{
friend class NzThread::Id;
public:
NzThreadImpl(NzThread* threadFunc);
~NzThreadImpl();
NzThreadImpl(NzFunctor* threadFunc);
NzThread::Id GetId() const;
bool IsCurrent() const;
void Detach();
void Join();
void Terminate();
static void Sleep(nzUInt32 time);
private:
static unsigned int __stdcall ThreadProc(void* userdata);
HANDLE m_thread;
unsigned int m_threadId;
HANDLE m_handle;
};
#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
NzMutex mutex;
NzConditionVariable condition;
m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle, win32Style, x, y, width, height, this, &mutex, &condition);
m_threadActive = true;
// On attend que la fenêtre soit créée
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);
mutex.Unlock();
#else