commit x64 compilation from lulu cause the other branch dont seems to compile properly at home
This commit is contained in:
102
include/platform/thread/atomic_value.cpp
Normal file
102
include/platform/thread/atomic_value.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "thread/atomic_value.h"
|
||||
#include "assert/nassert.h"
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int Atomic32::Inc()
|
||||
{ return (int)InterlockedIncrement((LONG volatile *)v); }
|
||||
int Atomic32::Dec()
|
||||
{ return (int)InterlockedDecrement((LONG volatile *)v); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int Atomic32::Get() const
|
||||
{ return *((int volatile *)v); }
|
||||
int Atomic32::Set(int _v)
|
||||
{ return InterlockedExchange((LONG volatile *)v, _v); }
|
||||
int Atomic32::Cas(int cmp, int _v)
|
||||
{ return InterlockedCompareExchange((LONG volatile *)v, _v, cmp); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Atomic32::Atomic32(int _v)
|
||||
{
|
||||
v = _aligned_malloc(sizeof(LONG), 4);
|
||||
__ASSERT__(v != NULL);
|
||||
Set(_v);
|
||||
}
|
||||
Atomic32::~Atomic32()
|
||||
{ _aligned_free(v); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#elif (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 1)) // use >GCC4.1 builtins
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int Atomic32::Inc()
|
||||
{ return __sync_fetch_and_add((long volatile *)v, 1) + 1; }
|
||||
int Atomic32::Dec()
|
||||
{ return __sync_fetch_and_add((long volatile *)v, -1) - 1; }
|
||||
int Atomic32::Get() const
|
||||
{ return *((int volatile *)v); }
|
||||
int Atomic32::Set(int _v)
|
||||
{ return __sync_lock_test_and_set((long volatile *)v, _v); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int Atomic32::Cas(int cmp, int _v)
|
||||
{ return __sync_val_compare_and_swap((long volatile *)v, cmp, _v); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Atomic32::Atomic32(int _v)
|
||||
{
|
||||
v = memalign(4, sizeof(long));
|
||||
__ASSERT__(v != NULL);
|
||||
*(long *)v = _v;
|
||||
}
|
||||
Atomic32::~Atomic32()
|
||||
{ free(v); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int Atomic32::Get() const
|
||||
{ __ASSERT(true); }
|
||||
int Atomic32::Set(int _v)
|
||||
{ __ASSERT(true); }
|
||||
int Atomic32::Cas(int cmp, int _v)
|
||||
{ __ASSERT(true); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
90
include/platform/thread/mutex.cpp
Normal file
90
include/platform/thread/mutex.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "thread/mutex.h"
|
||||
#include "alloc/ialloc.h"
|
||||
|
||||
using namespace GS::Threading;
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool Mutex::TryLock()
|
||||
{ return asbool(TryEnterCriticalSection((LPCRITICAL_SECTION)mutex)); }
|
||||
void Mutex::Lock()
|
||||
{
|
||||
if (mutex)
|
||||
EnterCriticalSection((LPCRITICAL_SECTION)mutex);
|
||||
}
|
||||
void Mutex::Unlock()
|
||||
{
|
||||
if (mutex)
|
||||
LeaveCriticalSection((LPCRITICAL_SECTION)mutex);
|
||||
}
|
||||
Mutex::Mutex()
|
||||
{
|
||||
mutex = new CRITICAL_SECTION;
|
||||
InitializeCriticalSection((LPCRITICAL_SECTION)mutex);
|
||||
}
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
DeleteCriticalSection((LPCRITICAL_SECTION)mutex);
|
||||
_safe_delete(mutex);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_POSIX__
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool Mutex::TryLock()
|
||||
{ return pthread_mutex_trylock(&mutex) != EBUSY ? true : false; }
|
||||
void Mutex::Lock()
|
||||
{ pthread_mutex_lock(&mutex); }
|
||||
void Mutex::Unlock()
|
||||
{ pthread_mutex_unlock(&mutex); }
|
||||
Mutex::Mutex()
|
||||
{ pthread_mutex_init(&mutex, NULL); }
|
||||
Mutex::~Mutex()
|
||||
{ pthread_mutex_destroy(&mutex); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_NINTENDO_WII__
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool Mutex::TryLock()
|
||||
{ return OSTryLockMutex(&mutex); }
|
||||
void Mutex::Lock()
|
||||
{ OSLockMutex(&mutex); }
|
||||
void Mutex::Unlock()
|
||||
{ OSUnlockMutex(&mutex); }
|
||||
Mutex::Mutex()
|
||||
{ OSInitMutex(&mutex); }
|
||||
Mutex::~Mutex()
|
||||
{}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#else
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool Mutex::TryLock()
|
||||
{ return true; }
|
||||
void Mutex::Lock()
|
||||
{}
|
||||
void Mutex::Unlock()
|
||||
{}
|
||||
Mutex::Mutex()
|
||||
{}
|
||||
Mutex::~Mutex()
|
||||
{}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
230
include/platform/thread/thread.cpp
Normal file
230
include/platform/thread/thread.cpp
Normal file
@ -0,0 +1,230 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
#elif __PLATFORM_POSIX__
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include "thread/thread.h"
|
||||
#include "assert/nassert.h"
|
||||
#include "log/log.h"
|
||||
#include <processthreadsapi.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static unsigned __stdcall win32_thread_entrypoint(void *parm)
|
||||
{
|
||||
Thread *t = (Thread *)parm;
|
||||
__ASSERT__(t != NULL);
|
||||
t->Execute();
|
||||
return 1;
|
||||
}
|
||||
void Thread::Join()
|
||||
{
|
||||
if (handle != 0)
|
||||
{
|
||||
WaitForSingleObject((HANDLE)handle, INFINITE);
|
||||
|
||||
CloseHandle((HANDLE)handle);
|
||||
handle = 0;
|
||||
}
|
||||
}
|
||||
bool Thread::Start()
|
||||
{
|
||||
handle = (void *)_beginthreadex(NULL, 0, &win32_thread_entrypoint, (void *)this, 0, NULL);
|
||||
return handle != 0;
|
||||
}
|
||||
bool Thread::SetPriority(int)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
void Thread::Kill()
|
||||
{
|
||||
if (handle != 0)
|
||||
{
|
||||
CloseHandle((HANDLE)handle);
|
||||
handle = 0;
|
||||
}
|
||||
}
|
||||
void Thread::SetName(const char *name)
|
||||
{
|
||||
if (name == nullptr)
|
||||
return;
|
||||
|
||||
// Conversion UTF-8 (ou ANSI selon votre projet) vers UTF-16
|
||||
int length = MultiByteToWideChar(CP_UTF8, 0, name, -1, nullptr, 0);
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
std::wstring wname(length, L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, name, -1, &wname[0], length);
|
||||
|
||||
SetThreadDescription(GetCurrentThread(), wname.c_str());
|
||||
}
|
||||
void Thread::Switch()
|
||||
{ SwitchToThread(); }
|
||||
|
||||
Thread::Thread()
|
||||
{ handle = 0; }
|
||||
Thread::~Thread()
|
||||
{ Kill(); }
|
||||
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_EMSCRIPTEN__
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void *pthread_execute(void *parm)
|
||||
{ return NULL; }
|
||||
void Thread::Join()
|
||||
{}
|
||||
bool Thread::Start()
|
||||
{ return false; }
|
||||
bool Thread::SetPriority(int priority)
|
||||
{ return false; }
|
||||
void Thread::Kill()
|
||||
{}
|
||||
void Thread::SetName(const char *)
|
||||
{}
|
||||
void Thread::Switch()
|
||||
{}
|
||||
Thread::Thread()
|
||||
{}
|
||||
Thread::~Thread()
|
||||
{}
|
||||
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_POSIX__
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void *pthread_execute(void *parm)
|
||||
{
|
||||
Thread *t = (Thread * const)parm;
|
||||
t->Execute();
|
||||
return NULL;
|
||||
}
|
||||
void Thread::Join()
|
||||
{
|
||||
pthread_join(*((pthread_t *)handle), NULL);
|
||||
}
|
||||
bool Thread::Start()
|
||||
{
|
||||
if (!(handle = (void *)new pthread_t))
|
||||
return false;
|
||||
return pthread_create((pthread_t *)handle, NULL, pthread_execute, this) == 0;
|
||||
}
|
||||
bool Thread::SetPriority(int priority)
|
||||
{
|
||||
#ifdef EMSCRIPTEN
|
||||
return false;
|
||||
#else
|
||||
if (!handle)
|
||||
return false;
|
||||
|
||||
sched_param param;
|
||||
memset(¶m, 0, sizeof(param));
|
||||
param.sched_priority = priority;
|
||||
|
||||
return asbool(pthread_setschedparam(*((pthread_t *)handle), SCHED_OTHER, ¶m) == 0);
|
||||
#endif
|
||||
}
|
||||
void Thread::Kill()
|
||||
{
|
||||
#if __PLATFORM_ANDROID_NDK__
|
||||
__ASSERT_MSG__(true, "Android NDK cannot kill a thread.");
|
||||
#else
|
||||
if (handle)
|
||||
pthread_cancel(*((pthread_t *)handle));
|
||||
|
||||
delete (pthread_t *)handle;
|
||||
handle = NULL;
|
||||
#endif
|
||||
}
|
||||
void Thread::SetName(const char *)
|
||||
{}
|
||||
void Thread::Switch()
|
||||
{ sched_yield(); }
|
||||
Thread::Thread()
|
||||
{ handle = NULL; }
|
||||
Thread::~Thread()
|
||||
{ delete (pthread_t *)handle; }
|
||||
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_NINTENDO_WII__
|
||||
|
||||
namespace GS {
|
||||
namespace Threading {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void *__OSthread_wii_execute(void *parm)
|
||||
{
|
||||
nThread *t = (nThread * const)parm;
|
||||
t->alive.Set(1);
|
||||
return (void *)(t->Execute() ? 1 : 0);
|
||||
}
|
||||
void Thread::Join()
|
||||
{
|
||||
void *rv;
|
||||
if (alive.Get())
|
||||
OSJoinThread(&thread, &rv);
|
||||
alive.Set(0);
|
||||
}
|
||||
void Thread::Resume()
|
||||
{
|
||||
if (alive.Get())
|
||||
OSResumeThread(&thread);
|
||||
}
|
||||
void Thread::Suspend()
|
||||
{
|
||||
if (alive.Get())
|
||||
OSSuspendThread(&thread);
|
||||
}
|
||||
bool Thread::Start()
|
||||
{ return OSCreateThread(&thread, &__OSthread_wii_execute, this, thread_stack + 32768, 32768, 20, 0); }
|
||||
bool Thread::SetPriority(int priority)
|
||||
{ return alive.Get() ? OSSetThreadPriority(&thread, priority) : false; }
|
||||
void Thread::Kill()
|
||||
{
|
||||
if (alive.Get())
|
||||
OSCancelThread(&thread);
|
||||
alive.Set(0);
|
||||
}
|
||||
void Thread::SetName(const char *)
|
||||
{}
|
||||
Thread::Thread()
|
||||
{}
|
||||
Thread::~Thread()
|
||||
{ Kill(); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
120
include/platform/thread/thread_event.cpp
Normal file
120
include/platform/thread/thread_event.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "thread/thread_event.h"
|
||||
#include "time/ntime.h"
|
||||
|
||||
using namespace GS;
|
||||
using namespace GS::Threading;
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void Event::Wait(Time *t)
|
||||
{ WaitForSingleObject((HANDLE)event, t ? DWORD(t->toMs()) : INFINITE); }
|
||||
void Event::Trigger()
|
||||
{ SetEvent((HANDLE)event); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Event::Event()
|
||||
{ event = (void *)CreateEvent(NULL, false, false, NULL); }
|
||||
Event::~Event()
|
||||
{ if (event) CloseHandle(event); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_EMSCRIPTEN__
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void Event::Wait(Time *t)
|
||||
{}
|
||||
void Event::Trigger()
|
||||
{}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Event::Event()
|
||||
{}
|
||||
Event::~Event()
|
||||
{}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#elif __PLATFORM_POSIX__
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
struct pthread_event
|
||||
{
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
bool triggered;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void Event::Wait(Time *t)
|
||||
{
|
||||
pthread_event *ev = (pthread_event *)event;
|
||||
|
||||
timespec time;
|
||||
|
||||
if (t)
|
||||
{
|
||||
timeval ctime;
|
||||
gettimeofday(&ctime, NULL);
|
||||
time.tv_sec = t->getSec() + ctime.tv_sec;
|
||||
time.tv_nsec = t->getNanoSec() + ctime.tv_usec * 1000;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&ev->mutex);
|
||||
while (!ev->triggered)
|
||||
{
|
||||
if (!t)
|
||||
pthread_cond_wait(&ev->cond, &ev->mutex);
|
||||
else
|
||||
if (pthread_cond_timedwait(&ev->cond, &ev->mutex, &time) == ETIMEDOUT)
|
||||
break;
|
||||
}
|
||||
ev->triggered = false;
|
||||
pthread_mutex_unlock(&ev->mutex);
|
||||
}
|
||||
void Event::Trigger()
|
||||
{
|
||||
pthread_event *ev = (pthread_event *)event;
|
||||
|
||||
pthread_mutex_lock(&ev->mutex);
|
||||
ev->triggered = true;
|
||||
pthread_cond_signal(&ev->cond);
|
||||
pthread_mutex_unlock(&ev->mutex);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Event::Event()
|
||||
{
|
||||
pthread_event *ev = new pthread_event;
|
||||
event = (void *)ev;
|
||||
|
||||
pthread_mutex_init(&ev->mutex, 0);
|
||||
pthread_cond_init(&ev->cond, 0);
|
||||
ev->triggered = false;
|
||||
}
|
||||
Event::~Event()
|
||||
{
|
||||
pthread_event *ev = (pthread_event *)event;
|
||||
pthread_mutex_destroy(&ev->mutex);
|
||||
pthread_cond_destroy(&ev->cond);
|
||||
delete ev;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user