This commit is contained in:
Jérôme Leclercq 2017-06-21 18:10:51 +02:00
commit 2ca844be63
17 changed files with 247 additions and 34 deletions

View File

@ -73,7 +73,7 @@
#define NAZARA_CORE_WINDOWS_CS_SPINLOCKS 4096
// Optimize the Windows implementation with technologies of Windows NT 6.0 (and greater) (Break the compatibility with Windows XP)
#define NAZARA_CORE_WINDOWS_NT6 0
#define NAZARA_CORE_WINDOWS_NT6 1
/*

View File

@ -56,7 +56,7 @@ namespace Nz
};
// Little hack to have them in both Nz and global scope
namespace DetailFlagOperators
namespace FlagsOperators
{
template<typename E> constexpr std::enable_if_t<EnumAsFlags<E>::value, Flags<E>> operator~(E lhs);
template<typename E> constexpr std::enable_if_t<EnumAsFlags<E>::value, Flags<E>> operator|(E lhs, E rhs);
@ -64,10 +64,10 @@ namespace Nz
template<typename E> constexpr std::enable_if_t<EnumAsFlags<E>::value, Flags<E>> operator^(E lhs, E rhs);
}
using namespace DetailFlagOperators;
using namespace FlagsOperators;
}
using namespace Nz::DetailFlagOperators;
using namespace Nz::FlagsOperators;
#include <Nazara/Core/Flags.inl>

View File

@ -211,7 +211,7 @@ namespace Nz
}
namespace DetailFlagOperators
namespace FlagsOperators
{
/*!
* \brief Override binary NOT operator on enum to turns into a Flags object.

View File

@ -32,11 +32,13 @@ namespace Nz
Id GetId() const;
bool IsJoinable() const;
void Join();
void SetName(const String& name);
Thread& operator=(const Thread&) = delete;
Thread& operator=(Thread&& thread);
static unsigned int HardwareConcurrency();
static void SetCurrentThreadName(const String& name);
static void Sleep(UInt32 milliseconds);
private:

View File

@ -24,7 +24,7 @@ namespace Nz
inline void LuaInstance::SetMemoryLimit(std::size_t memoryLimit)
{
m_memoryLimit = m_memoryLimit;
m_memoryLimit = memoryLimit;
}
inline void LuaInstance::SetTimeLimit(UInt32 limit)

View File

@ -5,6 +5,7 @@
#include <Nazara/Core/Posix/FileImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <cstdio>
#include <sys/file.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
@ -72,11 +73,47 @@ namespace Nz
if (mode & OpenMode_Truncate)
flags |= O_TRUNC;
///TODO: lock
//if ((mode & OpenMode_Lock) == 0)
// shareMode |= FILE_SHARE_WRITE;
m_fileDescriptor = open64(filePath.GetConstBuffer(), flags, permissions);
static struct flock lock;
auto initialize_flock = [](struct flock& fileLock)
{
fileLock.l_type = F_WRLCK;
fileLock.l_start = 0;
fileLock.l_whence = SEEK_SET;
fileLock.l_len = 0;
fileLock.l_pid = getpid();
};
initialize_flock(lock);
if (fcntl(m_fileDescriptor, F_GETLK, &lock) == -1)
{
Close();
NazaraError("Unable to detect presence of lock on the file");
return false;
}
if (lock.l_type != F_UNLCK)
{
Close();
NazaraError("A lock is present on the file");
return false;
}
if (mode & OpenMode_Lock)
{
initialize_flock(lock);
if (fcntl(m_fileDescriptor, F_SETLK, &lock) == -1)
{
Close();
NazaraError("Unable to place a lock on the file");
return false;
}
}
return m_fileDescriptor != -1;
}
@ -128,10 +165,7 @@ namespace Nz
std::size_t FileImpl::Write(const void* buffer, std::size_t size)
{
lockf64(m_fileDescriptor, F_LOCK, size);
ssize_t written = write(m_fileDescriptor, buffer, size);
lockf64(m_fileDescriptor, F_ULOCK, size);
m_endOfFileUpdated = false;
return written;

View File

@ -29,13 +29,22 @@ namespace Nz
pthread_join(m_handle, nullptr);
}
void* ThreadImpl::ThreadProc(void* userdata)
void ThreadImpl::SetName(const Nz::String& name)
{
Functor* func = static_cast<Functor*>(userdata);
func->Run();
delete func;
#ifdef __GNUC__
pthread_setname_np(m_handle, name.GetConstBuffer());
#else
NazaraWarning("Setting thread name is not supported on this platform");
#endif
}
return nullptr;
void ThreadImpl::SetCurrentName(const Nz::String& name)
{
#ifdef __GNUC__
pthread_setname_np(pthread_self(), name.GetConstBuffer());
#else
NazaraWarning("Setting current thread name is not supported on this platform");
#endif
}
void ThreadImpl::Sleep(UInt32 time)
@ -56,4 +65,13 @@ namespace Nz
while (r == -1 && errno == EINTR);
}
}
void* ThreadImpl::ThreadProc(void* userdata)
{
Functor* func = static_cast<Functor*>(userdata);
func->Run();
delete func;
return nullptr;
}
}

View File

@ -8,6 +8,12 @@
#define NAZARA_THREADIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/String.hpp>
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <pthread.h>
namespace Nz
@ -21,7 +27,9 @@ namespace Nz
void Detach();
void Join();
void SetName(const Nz::String& name);
static void SetCurrentName(const Nz::String& name);
static void Sleep(UInt32 time);
private:

View File

@ -117,6 +117,25 @@ namespace Nz
m_impl = nullptr;
}
/*!
* \brief Changes the debugging name associated to a thread
*
* Changes the debugging name associated with a particular thread, and may helps with debugging tools.
*
* \param name The new name of the thread
*
* \remark Due to system limitations, thread name cannot exceed 15 characters (excluding null-terminator)
*
* \see SetCurrentThreadName
*/
void Thread::SetName(const String& name)
{
NazaraAssert(m_impl, "Invalid thread");
NazaraAssert(name.GetSize() < 16, "Thread name is too long");
m_impl->SetName(name);
}
/*!
* \brief Moves the other thread into this
* \return A reference to this
@ -145,18 +164,35 @@ namespace Nz
* \brief Gets the number of simulatenous threads that can run on the same cpu
* \return The number of simulatenous threads
*/
unsigned int Thread::HardwareConcurrency()
{
return HardwareInfo::GetProcessorCount();
}
/*!
* \brief Changes the debugging name associated to the calling thread
*
* Changes the debugging name associated with the calling thread, and may helps with debugging tools.
*
* \param name The new name associated with this thread
*
* \remark Due to system limitations, thread name cannot exceed 15 characters (excluding null-terminator)
*
* \see SetName
*/
void Thread::SetCurrentThreadName(const String& name)
{
NazaraAssert(name.GetSize() < 16, "Thread name is too long");
ThreadImpl::SetCurrentName(name);
}
/*!
* \brief Makes sleep this thread
*
* \param milliseconds The number of milliseconds to sleep
*/
void Thread::Sleep(UInt32 milliseconds)
{
ThreadImpl::Sleep(milliseconds);

View File

@ -191,9 +191,7 @@ namespace Nz
LARGE_INTEGER cursorPos;
cursorPos.QuadPart = GetCursorPos();
LockFile(m_handle, cursorPos.LowPart, cursorPos.HighPart, static_cast<DWORD>(size), 0);
WriteFile(m_handle, buffer, static_cast<DWORD>(size), &written, nullptr);
UnlockFile(m_handle, cursorPos.LowPart, cursorPos.HighPart, static_cast<DWORD>(size), 0);
m_endOfFileUpdated = false;

View File

@ -6,15 +6,32 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Functor.hpp>
#include <process.h>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace
{
#pragma pack(push,8)
struct THREADNAME_INFO
{
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
};
#pragma pack(pop)
}
ThreadImpl::ThreadImpl(Functor* functor)
{
m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &ThreadImpl::ThreadProc, functor, 0, nullptr));
unsigned int threadId;
m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &ThreadImpl::ThreadProc, functor, 0, &threadId));
if (!m_handle)
NazaraInternalError("Failed to create thread: " + Error::GetLastSystemError());
m_threadId = threadId;
}
void ThreadImpl::Detach()
@ -29,6 +46,44 @@ namespace Nz
CloseHandle(m_handle);
}
void ThreadImpl::SetName(const Nz::String& name)
{
SetThreadName(m_threadId, name.GetConstBuffer());
}
void ThreadImpl::SetCurrentName(const Nz::String& name)
{
SetThreadName(::GetCurrentThreadId(), name.GetConstBuffer());
}
void ThreadImpl::Sleep(UInt32 time)
{
::Sleep(time);
}
void ThreadImpl::SetThreadName(DWORD threadId, const char* threadName)
{
// https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = threadId;
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
#pragma warning(pop)
}
unsigned int __stdcall ThreadImpl::ThreadProc(void* userdata)
{
Functor* func = static_cast<Functor*>(userdata);
@ -42,9 +97,4 @@ namespace Nz
return 0;
}
void ThreadImpl::Sleep(UInt32 time)
{
::Sleep(time);
}
}

View File

@ -10,6 +10,7 @@
#define NAZARA_THREADIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/String.hpp>
#include <windows.h>
namespace Nz
@ -23,12 +24,16 @@ namespace Nz
void Detach();
void Join();
void SetName(const Nz::String& name);
static void SetCurrentName(const Nz::String& name);
static void Sleep(UInt32 time);
private:
static void SetThreadName(DWORD threadId, const char* threadName);
static unsigned int __stdcall ThreadProc(void* userdata);
DWORD m_threadId;
HANDLE m_handle;
};
}

View File

@ -326,7 +326,7 @@ namespace Nz
});
}
while (totalByteSent < size)
while (totalByteSent < size || !IsBlockingEnabled())
{
int sendSize = static_cast<int>(std::min<std::size_t>(size - totalByteSent, std::numeric_limits<int>::max())); //< Handle very large send
int sentSize;

View File

@ -135,6 +135,30 @@ namespace Nz
#if NAZARA_NETWORK_POLL_SUPPORT
activeSockets = SocketImpl::Poll(m_sockets.data(), m_sockets.size(), static_cast<int>(msTimeout), error);
m_readyToReadSockets.clear();
m_readyToWriteSockets.clear();
if (activeSockets > 0U)
{
int socketRemaining = activeSockets;
for (PollSocket& entry : m_sockets)
{
if (entry.revents != 0)
{
if (entry.revents & POLLRDNORM)
m_readyToReadSockets.insert(entry.fd);
if (entry.revents & POLLWRNORM)
m_readyToWriteSockets.insert(entry.fd);
entry.revents = 0;
if (--socketRemaining == 0)
break;
}
}
}
#else
fd_set* readSet = nullptr;
fd_set* writeSet = nullptr;

View File

@ -121,11 +121,15 @@ namespace Nz
Rectf RigidBody2D::GetAABB() const
{
cpBB bb = cpBBNew(0.f, 0.f, 0.f, 0.f);
for (cpShape* shape : m_shapes)
bb = cpBBMerge(bb, cpShapeGetBB(shape));
if (m_shapes.empty())
return Rectf::Zero();
return Rectf(Rect<cpFloat>(bb.l, bb.t, bb.r - bb.l, bb.b - bb.t));
auto it = m_shapes.begin();
cpBB bb = cpShapeGetBB(*it++);
for (; it != m_shapes.end(); ++it)
bb = cpBBMerge(bb, cpShapeGetBB(*it));
return Rectf(Rect<cpFloat>(bb.l, bb.b, bb.r - bb.l, bb.t - bb.b));
}
float RigidBody2D::GetAngularVelocity() const

View File

@ -0,0 +1,34 @@
#include <NDK/Systems/PhysicsSystem2D.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/CollisionComponent2D.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent2D.hpp>
#include <Catch/catch.hpp>
SCENARIO("PhysicsSystem2D", "[NDK][PHYSICSSYSTEM2D]")
{
GIVEN("A world and an entity")
{
Ndk::World world;
const Ndk::EntityHandle& entity = world.CreateEntity();
Ndk::NodeComponent& nodeComponent = entity->AddComponent<Ndk::NodeComponent>();
Nz::Vector2f position(2.f, 3.f);
nodeComponent.SetPosition(position);
Nz::Rectf aabb(0.f, 0.f, 16.f, 18.f);
Nz::BoxCollider2DRef collisionBox = Nz::BoxCollider2D::New(aabb);
Ndk::CollisionComponent2D& collisionComponent = entity->AddComponent<Ndk::CollisionComponent2D>(collisionBox);
Ndk::PhysicsComponent2D& physicsComponent = entity->AddComponent<Ndk::PhysicsComponent2D>();
WHEN("We update the world")
{
world.Update(1.f);
THEN("Entity should have correct bounding box")
{
REQUIRE(nodeComponent.GetPosition() == position);
aabb.Translate(position);
REQUIRE(physicsComponent.GetAABB() == aabb);
}
}
}
}

View File

@ -5,7 +5,7 @@
#include <NDK/Components/PhysicsComponent3D.hpp>
#include <Catch/catch.hpp>
SCENARIO("PhysicsSystem", "[NDK][PHYSICSSYSTEM]")
SCENARIO("PhysicsSystem3D", "[NDK][PHYSICSSYSTEM3D]")
{
GIVEN("A world and a static entity & a dynamic entity")
{