Просмотр исходного кода

core: do not include windows.h in header files

Daniele Bartolini 7 лет назад
Родитель
Сommit
85ff4b34dc

+ 60 - 22
src/core/network/socket.cpp

@@ -6,6 +6,7 @@
 #include "core/error/error.h"
 #include "core/network/ip_address.h"
 #include "core/network/socket.h"
+#include "core/platform.h"
 
 #if CROWN_PLATFORM_POSIX
 	#include <errno.h>
@@ -13,6 +14,7 @@
 	#include <netinet/in.h> // htons, htonl, ...
 	#include <sys/socket.h>
 	#include <unistd.h>     // close
+	typedef int SOCKET;
 	#define INVALID_SOCKET (-1)
 	#define SOCKET_ERROR (-1)
 	#define closesocket close
@@ -20,6 +22,8 @@
 	#define WSAECONNREFUSED ECONNREFUSED
 	#define WSAETIMEDOUT ETIMEDOUT
 	#define WSAEWOULDBLOCK EWOULDBLOCK
+#elif CROWN_PLATFORM_WINDOWS
+	#include <winsock2.h>
 #endif // CROWN_PLATFORM_POSIX
 
 namespace crown
@@ -37,6 +41,11 @@ namespace
 
 }
 
+struct Private
+{
+	SOCKET socket;
+};
+
 namespace socket_internal
 {
 	SOCKET open()
@@ -62,7 +71,8 @@ namespace socket_internal
 		}
 		else
 		{
-			c._socket = err;
+			Private* priv = (Private*)c._data;
+			priv->socket = (SOCKET)err;
 		}
 
 		return ar;
@@ -148,30 +158,36 @@ namespace socket_internal
 } // namespace socket_internal
 
 TCPSocket::TCPSocket()
-	: _socket(INVALID_SOCKET)
 {
+	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(SOCKET));
+	Private* priv = (Private*)_data;
+	priv->socket = INVALID_SOCKET;
 }
 
 void TCPSocket::close()
 {
-	if (_socket != INVALID_SOCKET)
+	Private* priv = (Private*)_data;
+
+	if (priv->socket != INVALID_SOCKET)
 	{
-		::closesocket(_socket);
-		_socket = INVALID_SOCKET;
+		::closesocket(priv->socket);
+		priv->socket = INVALID_SOCKET;
 	}
 }
 
 ConnectResult TCPSocket::connect(const IPAddress& ip, u16 port)
 {
+	Private* priv = (Private*)_data;
+
 	close();
-	_socket = socket_internal::open();
+	priv->socket = socket_internal::open();
 
 	sockaddr_in addr_in;
 	addr_in.sin_family = AF_INET;
 	addr_in.sin_addr.s_addr = htonl(ip.address());
 	addr_in.sin_port = htons(port);
 
-	int err = ::connect(_socket, (const sockaddr*)&addr_in, sizeof(sockaddr_in));
+	int err = ::connect(priv->socket, (const sockaddr*)&addr_in, sizeof(sockaddr_in));
 
 	ConnectResult cr;
 	cr.error = ConnectResult::SUCCESS;
@@ -191,8 +207,10 @@ ConnectResult TCPSocket::connect(const IPAddress& ip, u16 port)
 
 BindResult TCPSocket::bind(u16 port)
 {
+	Private* priv = (Private*)_data;
+
 	close();
-	_socket = socket_internal::open();
+	priv->socket = socket_internal::open();
 	set_reuse_address(true);
 
 	sockaddr_in address;
@@ -200,7 +218,7 @@ BindResult TCPSocket::bind(u16 port)
 	address.sin_addr.s_addr = htonl(INADDR_ANY);
 	address.sin_port = htons(port);
 
-	int err = ::bind(_socket, (const sockaddr*)&address, sizeof(sockaddr_in));
+	int err = ::bind(priv->socket, (const sockaddr*)&address, sizeof(sockaddr_in));
 
 	BindResult br;
 	br.error = BindResult::SUCCESS;
@@ -218,55 +236,71 @@ BindResult TCPSocket::bind(u16 port)
 
 void TCPSocket::listen(u32 max)
 {
-	int err = ::listen(_socket, max);
+	Private* priv = (Private*)_data;
+
+	int err = ::listen(priv->socket, max);
 	CE_ASSERT(err == 0, "listen: last_error() = %d", last_error());
 	CE_UNUSED(err);
 }
 
 AcceptResult TCPSocket::accept(TCPSocket& c)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(true);
-	return socket_internal::accept(_socket, c);
+	return socket_internal::accept(priv->socket, c);
 }
 
 AcceptResult TCPSocket::accept_nonblock(TCPSocket& c)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(false);
-	return socket_internal::accept(_socket, c);
+	return socket_internal::accept(priv->socket, c);
 }
 
 ReadResult TCPSocket::read(void* data, u32 size)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(true);
-	return socket_internal::read(_socket, data, size);
+	return socket_internal::read(priv->socket, data, size);
 }
 
 ReadResult TCPSocket::read_nonblock(void* data, u32 size)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(false);
-	return socket_internal::read(_socket, data, size);
+	return socket_internal::read(priv->socket, data, size);
 }
 
 WriteResult TCPSocket::write(const void* data, u32 size)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(true);
-	return socket_internal::write(_socket, data, size);
+	return socket_internal::write(priv->socket, data, size);
 }
 
 WriteResult TCPSocket::write_nonblock(const void* data, u32 size)
 {
+	Private* priv = (Private*)_data;
+
 	set_blocking(false);
-	return socket_internal::write(_socket, data, size);
+	return socket_internal::write(priv->socket, data, size);
 }
 
 void TCPSocket::set_blocking(bool blocking)
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int flags = fcntl(_socket, F_GETFL, 0);
-	fcntl(_socket, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : O_NONBLOCK);
+	int flags = fcntl(priv->socket, F_GETFL, 0);
+	fcntl(priv->socket, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : O_NONBLOCK);
 #elif CROWN_PLATFORM_WINDOWS
 	u_long non_blocking = blocking ? 0 : 1;
-	int err = ioctlsocket(_socket, FIONBIO, &non_blocking);
+	int err = ioctlsocket(priv->socket, FIONBIO, &non_blocking);
 	CE_ASSERT(err == 0, "ioctlsocket: last_error() = %d", last_error());
 	CE_UNUSED(err);
 #endif
@@ -274,21 +308,25 @@ void TCPSocket::set_blocking(bool blocking)
 
 void TCPSocket::set_reuse_address(bool reuse)
 {
+	Private* priv = (Private*)_data;
+
 	int optval = (int)reuse;
-	int err = setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval));
+	int err = setsockopt(priv->socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval));
 	CE_ASSERT(err == 0, "setsockopt: last_error() = %d", last_error());
 	CE_UNUSED(err);
 }
 
 void TCPSocket::set_timeout(u32 seconds)
 {
+	Private* priv = (Private*)_data;
+
 	struct timeval timeout;
 	timeout.tv_sec = seconds;
 	timeout.tv_usec = 0;
 
-	int err = setsockopt(_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
+	int err = setsockopt(priv->socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
 	CE_ASSERT(err == 0, "setsockopt: last_error(): %d", last_error());
-	err = setsockopt(_socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
+	err = setsockopt(priv->socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
 	CE_ASSERT(err == 0, "setsockopt: last_error(): %d", last_error());
 	CE_UNUSED(err);
 }

+ 2 - 8
src/core/network/socket.h

@@ -6,15 +6,8 @@
 #pragma once
 
 #include "core/network/types.h"
-#include "core/platform.h"
 #include "core/types.h"
 
-#if CROWN_PLATFORM_POSIX
-	typedef int SOCKET;
-#elif CROWN_PLATFORM_WINDOWS
-	#include <winsock2.h>
-#endif
-
 namespace crown
 {
 struct ConnectResult
@@ -79,8 +72,9 @@ struct WriteResult
 /// @ingroup Network
 struct TCPSocket
 {
-	SOCKET _socket;
+	CE_ALIGN_DECL(16, u8 _data[8]);
 
+	///
 	TCPSocket();
 
 	/// Closes the socket.

+ 3 - 3
src/core/platform.h

@@ -49,9 +49,9 @@
 	#undef CROWN_PLATFORM_WINDOWS
 // http://msdn.microsoft.com/en-us/library/6sehtctf.aspx
 	#if !defined(WINVER) && !defined(_WIN32_WINNT)
-// Windows Server 2003 with SP1, Windows XP with SP2 and above
-		#define WINVER 0x0501
-		#define _WIN32_WINNT 0x0501
+// Windows 7 and above
+		#define WINVER 0x0601
+		#define _WIN32_WINNT 0x0601
 	#endif // !defined(WINVER) && !defined(_WIN32_WINNT)
 	#define CROWN_PLATFORM_WINDOWS 1
 #elif defined(__ANDROID__)

+ 0 - 3
src/core/thread/atomic_int.h

@@ -9,9 +9,6 @@
 
 #if CROWN_PLATFORM_WINDOWS
 	#include "core/types.h"
-	#ifndef WIN32_LEAN_AND_MEAN
-	#define WIN32_LEAN_AND_MEAN
-	#endif
 	#include <windows.h>
 #endif
 

+ 31 - 9
src/core/thread/condition_variable.cpp

@@ -3,27 +3,45 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
-#define WINVER 0x0601
-#define _WIN32_WINNT 0x0601
 #include "core/thread/condition_variable.h"
 
+#if CROWN_PLATFORM_POSIX
+	#include <pthread.h>
+#elif CROWN_PLATFORM_WINDOWS
+	#include <windows.h>
+#endif
+
 namespace crown
 {
+struct Private
+{
+#if CROWN_PLATFORM_POSIX
+	pthread_cond_t cond;
+#elif CROWN_PLATFORM_WINDOWS
+	CONDITION_VARIABLE cv;
+#endif
+};
+
 ConditionVariable::ConditionVariable()
 {
+	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(Private));
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_init(&_cond, NULL);
+	int err = pthread_cond_init(&priv->cond, NULL);
 	CE_ASSERT(err == 0, "pthread_cond_init: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	InitializeConditionVariable(&_cv);
+	InitializeConditionVariable(&priv->cv);
 #endif
 }
 
 ConditionVariable::~ConditionVariable()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_destroy(&_cond);
+	int err = pthread_cond_destroy(&priv->cond);
 	CE_ASSERT(err == 0, "pthread_cond_destroy: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
@@ -33,23 +51,27 @@ ConditionVariable::~ConditionVariable()
 
 void ConditionVariable::wait(Mutex& mutex)
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_wait(&_cond, &mutex._mutex);
+	int err = pthread_cond_wait(&priv->cond, (pthread_mutex_t*)mutex.native_handle());
 	CE_ASSERT(err == 0, "pthread_cond_wait: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	SleepConditionVariableCS(&_cv, &mutex._cs, INFINITE);
+	SleepConditionVariableCS(&priv->cv, (CRITICAL_SECTION*)mutex.native_handle(), INFINITE);
 #endif
 }
 
 void ConditionVariable::signal()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_signal(&_cond);
+	int err = pthread_cond_signal(&priv->cond);
 	CE_ASSERT(err == 0, "pthread_cond_signal: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	WakeConditionVariable(&_cv);
+	WakeConditionVariable(&priv->cv);
 #endif
 }
 

+ 1 - 5
src/core/thread/condition_variable.h

@@ -14,11 +14,7 @@ namespace crown
 /// @ingroup Thread
 struct ConditionVariable
 {
-#if CROWN_PLATFORM_POSIX
-	pthread_cond_t _cond;
-#elif CROWN_PLATFORM_WINDOWS
-	CONDITION_VARIABLE _cv;
-#endif
+	CE_ALIGN_DECL(16, u8 _data[64]);
 
 	///
 	ConditionVariable();

+ 43 - 8
src/core/thread/mutex.cpp

@@ -5,56 +5,91 @@
 
 #include "core/thread/mutex.h"
 
+#if CROWN_PLATFORM_POSIX
+	#include <pthread.h>
+#elif CROWN_PLATFORM_WINDOWS
+	#include <windows.h>
+#endif
+
 namespace crown
 {
+struct Private
+{
+#if CROWN_PLATFORM_POSIX
+	pthread_mutex_t mutex;
+#elif CROWN_PLATFORM_WINDOWS
+	CRITICAL_SECTION cs;
+#endif
+};
+
 Mutex::Mutex()
 {
+	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(Private));
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
 	pthread_mutexattr_t attr;
 	int err = pthread_mutexattr_init(&attr);
 	CE_ASSERT(err == 0, "pthread_mutexattr_init: errno = %d", err);
 	err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
 	CE_ASSERT(err == 0, "pthread_mutexattr_settype: errno = %d", err);
-	err = pthread_mutex_init(&_mutex, &attr);
+	err = pthread_mutex_init(&priv->mutex, &attr);
 	CE_ASSERT(err == 0, "pthread_mutex_init: errno = %d", err);
 	err = pthread_mutexattr_destroy(&attr);
 	CE_ASSERT(err == 0, "pthread_mutexattr_destroy: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	InitializeCriticalSection(&_cs);
+	InitializeCriticalSection(&priv->cs);
 #endif
 }
 
 Mutex::~Mutex()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_mutex_destroy(&_mutex);
+	int err = pthread_mutex_destroy(&priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_destroy: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	DeleteCriticalSection(&_cs);
+	DeleteCriticalSection(&priv->cs);
 #endif
 }
 
 void Mutex::lock()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_mutex_lock(&_mutex);
+	int err = pthread_mutex_lock(&priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_lock: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	EnterCriticalSection(&_cs);
+	EnterCriticalSection(&priv->cs);
 #endif
 }
 
 void Mutex::unlock()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_mutex_unlock(&_mutex);
+	int err = pthread_mutex_unlock(&priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_unlock: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	LeaveCriticalSection(&_cs);
+	LeaveCriticalSection(&priv->cs);
+#endif
+}
+
+void* Mutex::native_handle()
+{
+	Private* priv = (Private*)_data;
+
+#if CROWN_PLATFORM_POSIX
+	return &priv->mutex;
+#elif CROWN_PLATFORM_WINDOWS
+	return &priv->cs;
 #endif
 }
 

+ 4 - 14
src/core/thread/mutex.h

@@ -9,15 +9,6 @@
 #include "core/platform.h"
 #include "core/types.h"
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
-	#ifndef WIN32_LEAN_AND_MEAN
-	#define WIN32_LEAN_AND_MEAN
-	#endif
-	#include <windows.h>
-#endif
-
 namespace crown
 {
 /// Mutex.
@@ -25,11 +16,7 @@ namespace crown
 /// @ingroup Thread
 struct Mutex
 {
-#if CROWN_PLATFORM_POSIX
-	pthread_mutex_t _mutex;
-#elif CROWN_PLATFORM_WINDOWS
-	CRITICAL_SECTION _cs;
-#endif
+	CE_ALIGN_DECL(16, u8 _data[64]);
 
 	///
 	Mutex();
@@ -48,6 +35,9 @@ struct Mutex
 
 	/// Unlocks the mutex.
 	void unlock();
+
+	/// Returns the native mutex handle.
+	void* native_handle();
 };
 
 /// Automatically locks a mutex when created and unlocks when destroyed.

+ 46 - 18
src/core/thread/semaphore.cpp

@@ -6,34 +6,58 @@
 #include "core/error/error.h"
 #include "core/thread/semaphore.h"
 
+#if CROWN_PLATFORM_POSIX
+	#include <pthread.h>
+#elif CROWN_PLATFORM_WINDOWS
+	#include <windows.h>
+	#include <limits.h> // LONG_MAX
+#endif
+
 namespace crown
 {
-Semaphore::Semaphore()
+struct Private
+{
 #if CROWN_PLATFORM_POSIX
-	: _count(0)
+	Mutex mutex;
+	pthread_cond_t cond;
+	s32 count;
 #elif CROWN_PLATFORM_WINDOWS
-	: _handle(INVALID_HANDLE_VALUE)
+	HANDLE handle;
 #endif
+};
+
+Semaphore::Semaphore()
 {
+	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(Private));
+	Private* priv = (Private*)_data;
+
+#if CROWN_PLATFORM_POSIX
+	priv->count = 0;
+#elif CROWN_PLATFORM_WINDOWS
+	priv->handle = INVALID_HANDLE_VALUE;
+#endif
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_init(&_cond, NULL);
+	int err = pthread_cond_init(&priv->cond, NULL);
 	CE_ASSERT(err == 0, "pthread_cond_init: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
-	CE_ASSERT(_handle != NULL, "CreateSemaphore: GetLastError = %d", GetLastError());
-	CE_UNUSED(_handle);
+	priv->handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+	CE_ASSERT(priv->handle != NULL, "CreateSemaphore: GetLastError = %d", GetLastError());
+	CE_UNUSED(priv->handle);
 #endif
 }
 
 Semaphore::~Semaphore()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_destroy(&_cond);
+	int err = pthread_cond_destroy(&priv->cond);
 	CE_ASSERT(err == 0, "pthread_cond_destroy: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	BOOL err = CloseHandle(_handle);
+	BOOL err = CloseHandle(priv->handle);
 	CE_ASSERT(err != 0, "CloseHandle: GetLastError = %d", GetLastError());
 	CE_UNUSED(err);
 #endif
@@ -41,19 +65,21 @@ Semaphore::~Semaphore()
 
 void Semaphore::post(u32 count)
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	ScopedMutex sm(_mutex);
+	ScopedMutex sm(priv->mutex);
 
 	for (u32 i = 0; i < count; ++i)
 	{
-		int err = pthread_cond_signal(&_cond);
+		int err = pthread_cond_signal(&priv->cond);
 		CE_ASSERT(err == 0, "pthread_cond_signal: errno = %d", err);
 		CE_UNUSED(err);
 	}
 
-	_count += count;
+	priv->count += count;
 #elif CROWN_PLATFORM_WINDOWS
-	BOOL err = ReleaseSemaphore(_handle, count, NULL);
+	BOOL err = ReleaseSemaphore(priv->handle, count, NULL);
 	CE_ASSERT(err != 0, "ReleaseSemaphore: GetLastError = %d", GetLastError());
 	CE_UNUSED(err);
 #endif
@@ -61,19 +87,21 @@ void Semaphore::post(u32 count)
 
 void Semaphore::wait()
 {
+	Private* priv = (Private*)_data;
+
 #if CROWN_PLATFORM_POSIX
-	ScopedMutex sm(_mutex);
+	ScopedMutex sm(priv->mutex);
 
-	while (_count <= 0)
+	while (priv->count <= 0)
 	{
-		int err = pthread_cond_wait(&_cond, &(_mutex._mutex));
+		int err = pthread_cond_wait(&priv->cond, (pthread_mutex_t*)priv->mutex.native_handle());
 		CE_ASSERT(err == 0, "pthread_cond_wait: errno = %d", err);
 		CE_UNUSED(err);
 	}
 
-	_count--;
+	priv->count--;
 #elif CROWN_PLATFORM_WINDOWS
-	DWORD err = WaitForSingleObject(_handle, INFINITE);
+	DWORD err = WaitForSingleObject(priv->handle, INFINITE);
 	CE_ASSERT(err == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());
 	CE_UNUSED(err);
 #endif

+ 1 - 17
src/core/thread/semaphore.h

@@ -8,16 +8,6 @@
 #include "core/platform.h"
 #include "core/thread/mutex.h"
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
-	#ifndef WIN32_LEAN_AND_MEAN
-	#define WIN32_LEAN_AND_MEAN
-	#endif
-	#include <windows.h>
-	#include <limits.h>
-#endif
-
 namespace crown
 {
 /// Semaphore.
@@ -25,13 +15,7 @@ namespace crown
 /// @ingroup Thread.
 struct Semaphore
 {
-#if CROWN_PLATFORM_POSIX
-	Mutex _mutex;
-	pthread_cond_t _cond;
-	s32 _count;
-#elif CROWN_PLATFORM_WINDOWS
-	HANDLE _handle;
-#endif
+	CE_ALIGN_DECL(16, u8 _data[128]);
 
 	///
 	Semaphore();

+ 33 - 11
src/core/thread/thread.cpp

@@ -6,8 +6,24 @@
 #include "core/error/error.h"
 #include "core/thread/thread.h"
 
+#if CROWN_PLATFORM_POSIX
+	#include <pthread.h>
+#elif CROWN_PLATFORM_WINDOWS
+	#include <windows.h>
+	#include <process.h>
+#endif
+
 namespace crown
 {
+struct Private
+{
+#if CROWN_PLATFORM_POSIX
+	pthread_t handle;
+#elif CROWN_PLATFORM_WINDOWS
+	HANDLE handle;
+#endif
+};
+
 #if CROWN_PLATFORM_POSIX
 static void* thread_proc(void* arg)
 {
@@ -31,12 +47,14 @@ Thread::Thread()
 	: _function(NULL)
 	, _user_data(NULL)
 	, _is_running(false)
+{
+	Private* priv = (Private*)_data;
+	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(Private));
 #if CROWN_PLATFORM_POSIX
-	, _handle(0)
+	priv->handle = 0;
 #elif CROWN_PLATFORM_WINDOWS
-	, _handle(INVALID_HANDLE_VALUE)
+	priv->handle = INVALID_HANDLE_VALUE;
 #endif
-{
 }
 
 Thread::~Thread()
@@ -47,6 +65,8 @@ Thread::~Thread()
 
 void Thread::start(ThreadFunction func, void* user_data, u32 stack_size)
 {
+	Private* priv = (Private*)_data;
+
 	CE_ASSERT(!_is_running, "Thread is already running");
 	CE_ASSERT(func != NULL, "Function must be != NULL");
 	_function = func;
@@ -64,15 +84,15 @@ void Thread::start(ThreadFunction func, void* user_data, u32 stack_size)
 		CE_ASSERT(err == 0, "pthread_attr_setstacksize: errno = %d", err);
 	}
 
-	err = pthread_create(&_handle, &attr, thread_proc, this);
+	err = pthread_create(&priv->handle, &attr, thread_proc, this);
 	CE_ASSERT(err == 0, "pthread_create: errno = %d", err);
 
 	err = pthread_attr_destroy(&attr);
 	CE_ASSERT(err == 0, "pthread_attr_destroy: errno = %d", err);
 	CE_UNUSED(err);
 #elif CROWN_PLATFORM_WINDOWS
-	_handle = CreateThread(NULL, stack_size, thread_proc, this, 0, NULL);
-	CE_ASSERT(_handle != NULL, "CreateThread: GetLastError = %d", GetLastError());
+	priv->handle = CreateThread(NULL, stack_size, thread_proc, this, 0, NULL);
+	CE_ASSERT(priv->handle != NULL, "CreateThread: GetLastError = %d", GetLastError());
 #endif
 
 	_is_running = true;
@@ -81,18 +101,20 @@ void Thread::start(ThreadFunction func, void* user_data, u32 stack_size)
 
 void Thread::stop()
 {
+	Private* priv = (Private*)_data;
+
 	CE_ASSERT(_is_running, "Thread is not running");
 
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_join(_handle, NULL);
+	int err = pthread_join(priv->handle, NULL);
 	CE_ASSERT(err == 0, "pthread_join: errno = %d", err);
 	CE_UNUSED(err);
-	_handle = 0;
+	priv->handle = 0;
 #elif CROWN_PLATFORM_WINDOWS
-	WaitForSingleObject(_handle, INFINITE);
+	WaitForSingleObject(priv->handle, INFINITE);
 	// GetExitCodeThread(_handle, &m_exit_code);
-	CloseHandle(_handle);
-	_handle = INVALID_HANDLE_VALUE;
+	CloseHandle(priv->handle);
+	priv->handle = INVALID_HANDLE_VALUE;
 #endif
 
 	_is_running = false;

+ 1 - 15
src/core/thread/thread.h

@@ -9,16 +9,6 @@
 #include "core/thread/semaphore.h"
 #include "core/types.h"
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
-	#ifndef WIN32_LEAN_AND_MEAN
-	#define WIN32_LEAN_AND_MEAN
-	#endif
-	#include <windows.h>
-	#include <process.h>
-#endif
-
 /// @defgroup Thread Thread
 /// @ingroup Core
 namespace crown
@@ -34,11 +24,7 @@ struct Thread
 	void* _user_data;
 	Semaphore _sem;
 	bool _is_running;
-#if CROWN_PLATFORM_POSIX
-	pthread_t _handle;
-#elif CROWN_PLATFORM_WINDOWS
-	HANDLE _handle;
-#endif
+	CE_ALIGN_DECL(16, u8 _data[32]);
 
 	///
 	Thread();

+ 7 - 0
src/core/types.h

@@ -5,6 +5,7 @@
 
 #pragma once
 
+#include "core/platform.h"
 #include <stdint.h>
 
 #ifndef CROWN_DEBUG
@@ -71,3 +72,9 @@ typedef double   f64;
 #else
 	#error "Compiler not supported"
 #endif
+
+#if CROWN_PLATFORM_LINUX
+	#define CE_ALIGN_DECL(align, decl) decl __attribute__ ((aligned (align)))
+#elif CROWN_PLATFORM_WINDOWS
+	#define CE_ALIGN_DECL(align_, decl) __declspec (align(align_)) decl
+#endif