U-godlike-vm\godlike 11 vuotta sitten
vanhempi
sitoutus
1f16b55bb3

+ 7 - 4
src/util/FilesystemWindows.cpp

@@ -5,14 +5,18 @@
 
 
 #include "anki/util/Filesystem.h"
 #include "anki/util/Filesystem.h"
 #include "anki/util/Assert.h"
 #include "anki/util/Assert.h"
+#include "anki/util/Logger.h"
 #include <windows.h>
 #include <windows.h>
 
 
+// Someone pollutes the global namespace
+#undef ERROR
+
 namespace anki {
 namespace anki {
 
 
 //==============================================================================
 //==============================================================================
 Bool fileExists(const CString& filename)
 Bool fileExists(const CString& filename)
 {
 {
-	DWORD dwAttrib = GetFileAttributes(filename.get());
+	DWORD dwAttrib = GetFileAttributes(&filename[0]);
 
 
 	return dwAttrib != INVALID_FILE_ATTRIBUTES 
 	return dwAttrib != INVALID_FILE_ATTRIBUTES 
 		&& !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
 		&& !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
@@ -60,7 +64,7 @@ Error createDirectory(const CString& dir)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-Error getHomeDirectory(HeapAllocator<U8>& alloc, String& out);
+Error getHomeDirectory(HeapAllocator<U8>& alloc, String& out)
 {
 {
 	const char* homed = getenv("HOMEDRIVE");
 	const char* homed = getenv("HOMEDRIVE");
 	const char* homep = getenv("HOMEPATH");
 	const char* homep = getenv("HOMEPATH");
@@ -71,8 +75,7 @@ Error getHomeDirectory(HeapAllocator<U8>& alloc, String& out);
 		return ErrorCode::FUNCTION_FAILED;
 		return ErrorCode::FUNCTION_FAILED;
 	}
 	}
 
 
-	String out;
-	Error err = out.sprintf("%s/%s", homed, homep);
+	Error err = out.sprintf(alloc, "%s/%s", homed, homep);
 	if(!err)
 	if(!err)
 	{
 	{
 		// Convert to Unix path
 		// Convert to Unix path

+ 31 - 26
src/util/ThreadWindows.cpp

@@ -3,8 +3,15 @@
 // Code licensed under the BSD License.
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
+// Add support for condition variables
+#define _WIN32_WINNT _WIN32_WINNT_VISTA
+
 #include "anki/util/Thread.h"
 #include "anki/util/Thread.h"
-#include <windows.h>
+#include "anki/util/Functions.h"
+#include "anki/util/Logger.h"
+#include <cstring>
+#include <Windows.h>
+#include <WinBase.h>
 
 
 namespace anki {
 namespace anki {
 
 
@@ -29,9 +36,9 @@ static DWORD WINAPI threadCallback(LPVOID ud)
 	info.m_userData = thread->_getUserData();
 	info.m_userData = thread->_getUserData();
 	info.m_threadName = thread->_getName();
 	info.m_threadName = thread->_getName();
 
 
-	I err = thread->_getCallback()(info);
+	Error err = thread->_getCallback()(info);
 
 
-	return err;
+	return err._getCodeInt();
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -41,7 +48,7 @@ Thread::Thread(const char* name)
 	if(name)
 	if(name)
 	{
 	{
 		U len = std::strlen(name);
 		U len = std::strlen(name);
-		len = std::min<U>(len, sizeof(m_name) - 1);
+		len = min<U>(len, sizeof(m_name) - 1);
 		std::memcpy(&m_name[0], &name[0], len);
 		std::memcpy(&m_name[0], &name[0], len);
 		m_name[len] = '\0';
 		m_name[len] = '\0';
 	}
 	}
@@ -55,7 +62,7 @@ Thread::Thread(const char* name)
 Thread::~Thread()
 Thread::~Thread()
 {
 {
 	ANKI_ASSERT(!m_started && "Thread probably not joined");
 	ANKI_ASSERT(!m_started && "Thread probably not joined");
-	free(m_impl);
+	ANKI_ASSERT(m_impl == nullptr);
 	m_impl = nullptr;
 	m_impl = nullptr;
 }
 }
 
 
@@ -71,7 +78,7 @@ void Thread::start(void* userData, Callback callback)
 	m_impl = CreateThread(nullptr, 0, threadCallback, this, 0, nullptr);
 	m_impl = CreateThread(nullptr, 0, threadCallback, this, 0, nullptr);
 	if(m_impl == nullptr)
 	if(m_impl == nullptr)
 	{
 	{
-		throw ANKI_EXCEPTION("CreateThread() failed");
+		ANKI_LOGF("CreateThread() failed");
 	}
 	}
 	else
 	else
 	{
 	{
@@ -82,7 +89,7 @@ void Thread::start(void* userData, Callback callback)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-I Thread::join()
+Error Thread::join()
 {
 {
 	ANKI_ASSERT(m_started);
 	ANKI_ASSERT(m_started);
 
 
@@ -91,27 +98,33 @@ I Thread::join()
 	
 	
 	// Get return code
 	// Get return code
 	DWORD exitCode = 0;
 	DWORD exitCode = 0;
-	ok = GetExitCodeThread(m_impl, &exitCode);
+	BOOL ok = GetExitCodeThread(m_impl, &exitCode);
 	if(!ok)
 	if(!ok)
 	{
 	{
-		throw ANKI_EXCEPTION("GetExitCodeThread() failed");
+		ANKI_LOGF("GetExitCodeThread() failed");
 	}
 	}
 
 
 	// Delete handle
 	// Delete handle
-	BOOL ok = CloseHandle(m_impl);
+	ok = CloseHandle(m_impl);
 	if(!ok)
 	if(!ok)
 	{
 	{
-		throw ANKI_EXCEPTION("CloseHandle() failed");
+		ANKI_LOGF("CloseHandle() failed");
 	}
 	}
 
 
-	return exitCode;
+	m_impl = nullptr;
+
+#if ANKI_ASSERTIONS
+	m_started = false;
+#endif
+
+	return static_cast<ErrorCode>(exitCode);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
 Thread::Id Thread::getCurrentThreadId()
 Thread::Id Thread::getCurrentThreadId()
 {
 {
 	HANDLE x = GetCurrentThread();
 	HANDLE x = GetCurrentThread();
-	return x;
+	return reinterpret_cast<PtrSize>(x);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -125,18 +138,12 @@ Mutex::Mutex()
 		reinterpret_cast<CRITICAL_SECTION*>(malloc(sizeof(CRITICAL_SECTION)));
 		reinterpret_cast<CRITICAL_SECTION*>(malloc(sizeof(CRITICAL_SECTION)));
 	if(mtx == nullptr)
 	if(mtx == nullptr)
 	{
 	{
-		throw ANKI_EXCEPTION("Out of memory");
+		ANKI_LOGF("Out of memory");
 	}
 	}
 
 
 	m_impl = mtx;
 	m_impl = mtx;
 
 
-	BOOL ok = InitializeCriticalSectionAndSpinCount(mtx, 0x400);
-	if(!ok)
-	{
-		free(m_impl);
-		m_impl = nullptr;
-		throw ANKI_EXCEPTION("InitializeCriticalSectionAndSpinCount() failed");
-	}
+	InitializeCriticalSection(mtx);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -160,8 +167,7 @@ void Mutex::lock()
 Bool Mutex::tryLock()
 Bool Mutex::tryLock()
 {
 {
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
-
-	BOOL enter = EnterCriticalSection(mtx);
+	BOOL enter = TryEnterCriticalSection(mtx);
 	return enter;
 	return enter;
 }
 }
 
 
@@ -169,7 +175,6 @@ Bool Mutex::tryLock()
 void Mutex::unlock()
 void Mutex::unlock()
 {
 {
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
-
 	LeaveCriticalSection(mtx);
 	LeaveCriticalSection(mtx);
 }
 }
 
 
@@ -184,7 +189,7 @@ ConditionVariable::ConditionVariable()
 		malloc(sizeof(CONDITION_VARIABLE)));
 		malloc(sizeof(CONDITION_VARIABLE)));
 	if(cond == nullptr)
 	if(cond == nullptr)
 	{
 	{
-		throw ANKI_EXCEPTION("Out of memory");
+		ANKI_LOGF("Out of memory");
 	}
 	}
 
 
 	m_impl = cond;
 	m_impl = cond;
@@ -219,7 +224,7 @@ void ConditionVariable::wait(Mutex& amtx)
 	CONDITION_VARIABLE* cond = reinterpret_cast<CONDITION_VARIABLE*>(m_impl);
 	CONDITION_VARIABLE* cond = reinterpret_cast<CONDITION_VARIABLE*>(m_impl);
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
 	CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
 
 
-	SleepConditionVariableCS(cond, INFINITE);
+	SleepConditionVariableCS(cond, mtx, INFINITE);
 }
 }
 
 
 } // end namespace anki
 } // end namespace anki

+ 6 - 0
tests/framework/Framework.cpp

@@ -25,8 +25,13 @@ void Test::run()
 	std::cout << "========\nRunning " << suite->name << " " << name
 	std::cout << "========\nRunning " << suite->name << " " << name
 		<< "\n========" << std::endl;
 		<< "\n========" << std::endl;
 
 
+#if ANKI_OS == ANKI_OS_LINUX
 	struct mallinfo a = mallinfo();
 	struct mallinfo a = mallinfo();
+#endif
+
 	callback(*this);
 	callback(*this);
+
+#if ANKI_OS == ANKI_OS_LINUX
 	struct mallinfo b = mallinfo();
 	struct mallinfo b = mallinfo();
 
 
 	int diff = b.uordblks - a.uordblks;
 	int diff = b.uordblks - a.uordblks;
@@ -34,6 +39,7 @@ void Test::run()
 	{
 	{
 		std::cerr << "Test leaks memory: " << diff << std::endl;
 		std::cerr << "Test leaks memory: " << diff << std::endl;
 	}
 	}
+#endif
 
 
 	std::cout << std::endl;
 	std::cout << std::endl;
 }
 }

+ 1 - 1
tests/util/Object.cpp → tests/util/Hierarchy.cpp

@@ -4,7 +4,7 @@
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
 #include "tests/framework/Framework.h"
 #include "tests/framework/Framework.h"
-#include "anki/util/Object.h"
+#include "anki/util/Hierarchy.h"
 
 
 using namespace anki;
 using namespace anki;
 
 

+ 2 - 1
tests/util/Thread.cpp

@@ -39,7 +39,7 @@ ANKI_TEST(Util, Thread)
 		return (check != true) ? ErrorCode::FUNCTION_FAILED : ErrorCode::NONE;
 		return (check != true) ? ErrorCode::FUNCTION_FAILED : ErrorCode::NONE;
 	});
 	});
 
 
-
+	ANKI_TEST_EXPECT_EQ(u, NUMBER); // It should be the old value
 	Error err = t.join();
 	Error err = t.join();
 	ANKI_TEST_EXPECT_EQ(err, 0);
 	ANKI_TEST_EXPECT_EQ(err, 0);
 	ANKI_TEST_EXPECT_EQ(u, 0xF00);
 	ANKI_TEST_EXPECT_EQ(u, 0xF00);
@@ -87,6 +87,7 @@ ANKI_TEST(Util, Mutex)
 
 
 		for(U i = 0; i < ITERATIONS; i++)
 		for(U i = 0; i < ITERATIONS; i++)
 		{
 		{
+
 			mtx.lock();
 			mtx.lock();
 			--num;
 			--num;
 			mtx.unlock();
 			mtx.unlock();