Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced POSIX threads under Windows with stdlib implementations since it is under LGPL license #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions oxygine/src/ThreadLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@

namespace oxygine
{
#if OX_CPP11THREADS
ThreadLoader::ThreadLoader(): _thread(_staticThreadFunc, this), _threadDone(false)
#else
ThreadLoader::ThreadLoader(): _thread(pthread_self()), _threadDone(false)
#endif
{
}

ThreadLoader::~ThreadLoader()
{
#if OX_CPP11THREADS
if(_thread.get_id() != std::this_thread::get_id())
_thread.join();
#else
if (!pthread_equal(_thread, pthread_self()))
pthread_join(_thread, 0);
#endif
}


Expand Down Expand Up @@ -122,11 +131,12 @@ namespace oxygine
_load();
getStage()->addTween(TweenDummy(), 100)->addDoneCallback(CLOSURE(this, &ThreadLoader::loaded));
#else

#if !OX_CPP11THREADS
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&_thread, &attr, _staticThreadFunc, this);
#endif
#endif
}
}
}
15 changes: 13 additions & 2 deletions oxygine/src/ThreadLoader.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once
#include "oxygine-include.h"
#include "EventDispatcher.h"
#include "pthread.h"

#if OX_CPP11THREADS
#include <thread>
#else
#include "pthread.h"
#endif

#include "core/ThreadDispatcher.h"
#include <functional>
#include "Event.h"
Expand Down Expand Up @@ -44,7 +50,12 @@ namespace oxygine
void loaded(Event*);
void _load();

#if OX_CPP11THREADS
std::thread _thread;
#else
pthread_t _thread;
#endif

volatile bool _threadDone;

typedef std::list<Resources*> resources;
Expand All @@ -58,4 +69,4 @@ namespace oxygine
funcs _funcs;
#endif
};
}
}
71 changes: 56 additions & 15 deletions oxygine/src/core/Mutex.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,79 @@
#include "Mutex.h"
#include "ox_debug.h"
#include "pthread.h"

#if OX_CPP11THREADS
#include <mutex>
#else
#include "pthread.h"
#endif

namespace oxygine
{
Mutex::Mutex(bool recursive)
Mutex::Mutex()
{
if (recursive)
{
pthread_mutexattr_t mta;
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);

pthread_mutex_init(&_handle, &mta);
}
else
{
pthread_mutex_init(&_handle, 0);
}
#if !OX_CPP11THREADS
pthread_mutex_init(&_handle, 0);
#endif
}

Mutex::~Mutex()
{
#if !OX_CPP11THREADS
pthread_mutex_destroy(&_handle);
#endif
}

void Mutex::lock()
{
#if OX_CPP11THREADS
_mutex.lock();
#else
pthread_mutex_lock(&_handle);
#endif
}

void Mutex::unlock()
{
#if OX_CPP11THREADS
_mutex.unlock();
#else
pthread_mutex_unlock(&_handle);
#endif
}

MutexRecursive::MutexRecursive()
{
#if !OX_CPP11THREADS
pthread_mutexattr_t mta;
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);

pthread_mutex_init(&_handle, &mta);
#endif
}

MutexRecursive::~MutexRecursive()
{
#if !OX_CPP11THREADS
pthread_mutex_destroy(&_handle);
#endif
}

void MutexRecursive::lock()
{
#if OX_CPP11THREADS
_mutex.lock();
#else
pthread_mutex_lock(&_handle);
#endif
}

void MutexRecursive::unlock()
{
#if OX_CPP11THREADS
_mutex.unlock();
#else
pthread_mutex_unlock(&_handle);
#endif
}
}
}
43 changes: 42 additions & 1 deletion oxygine/src/core/Mutex.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
#pragma once
#include "oxygine-include.h"

#if OX_CPP11THREADS
#include <mutex>
#else

#if defined(_WIN32) && !defined(__MINGW32__)
typedef struct pthread_mutex_t_* pthread_mutex_t;
#else
# include "pthread.h"
#endif

#endif

namespace oxygine
{
class Mutex
{
public:
Mutex(bool recursive = false);
Mutex();
~Mutex();

void lock();
Expand All @@ -22,8 +28,33 @@ namespace oxygine
Mutex(const Mutex&) {}
void operator = (const Mutex&) {}

#if OX_CPP11THREADS
std::mutex _mutex;
#else
pthread_mutex_t _handle;
//void *_handle;
#endif
};

class MutexRecursive
{
public:
MutexRecursive();
~MutexRecursive();

void lock();
void unlock();

private:
MutexRecursive(const MutexRecursive&) {}
void operator = (const MutexRecursive&) {}

#if OX_CPP11THREADS
std::recursive_mutex _mutex;
#else
pthread_mutex_t _handle;
//void *_handle;
#endif
};

class MutexAutoLock
Expand All @@ -35,4 +66,14 @@ namespace oxygine
private:
Mutex& _m;
};

class MutexRecursiveAutoLock
{
public:
MutexRecursiveAutoLock(MutexRecursive& m): _m(m) {_m.lock();}
~MutexRecursiveAutoLock() {_m.unlock();}

private:
MutexRecursive& _m;
};
}
Loading