diff --git a/configure.ac b/configure.ac index d4fc0745..b328d629 100644 --- a/configure.ac +++ b/configure.ac @@ -82,7 +82,7 @@ dnl Code qui fait des warnings == code de porc == deux baffes dans ta gueule CXXFLAGS="${CXXFLAGS} -Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wsign-compare" AC_CHECK_LIB(m, sin, MATH_LIBS="${MATH_LIBS} -lm") - +AC_CHECK_LIB(pthread, main, LIBS="$LIBS -lpthread") dnl Are we on the PS3? AC_CHECK_LIB(sysmodule_stub, cellSysmoduleLoadModule, diff --git a/src/core.h b/src/core.h index 45fdce5d..4db4d973 100644 --- a/src/core.h +++ b/src/core.h @@ -68,6 +68,7 @@ static inline int isnan(float f) #include "matrix.h" #include "numeric.h" #include "timer.h" +#include "thread/thread.h" // Static classes #include "log.h" diff --git a/src/thread/thread.h b/src/thread/thread.h new file mode 100644 index 00000000..64713e3d --- /dev/null +++ b/src/thread/thread.h @@ -0,0 +1,50 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The Threading classes +// --------------------- +// + +#if !defined __LOL_THREAD_H__ +#define __LOL_THREAD_H__ + +#if defined __CELLOS_LV2__ +# include "platform/ps3/threadbase.h" +#else +# include "threadbase.h" +#endif + +namespace lol +{ + +class Mutex : public MutexBase +{ +public: + Mutex() : MutexBase() {} +}; + +class Condition : public ConditionBase +{ +public: + Condition() : ConditionBase() {} +}; + +class Thread : ThreadBase +{ +public: + Thread(void *(*fn)(void *), void *data) : ThreadBase(fn, data) {} + virtual ~Thread() {} +}; + +} /* namespace lol */ + +#endif // __LOL_THREAD_H__ + diff --git a/src/thread/threadbase.h b/src/thread/threadbase.h new file mode 100644 index 00000000..27cbd665 --- /dev/null +++ b/src/thread/threadbase.h @@ -0,0 +1,145 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The ThreadBase class +// -------------------- +// + +#if !defined __LOL_THREADBASE_H__ +#define __LOL_THREADBASE_H__ + +#if defined __linux__ || defined __native_client__ +# include +#else +# error No threading support yet :( +#endif + +namespace lol +{ + +class MutexBase +{ +public: + MutexBase() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_init(&m_mutex, NULL); +#endif + } + + ~MutexBase() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_destroy(&m_mutex); +#endif + } + + void Lock() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_lock(&m_mutex); +#endif + } + + void Unlock() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_unlock(&m_mutex); +#endif + } + +private: +#if defined __linux__ || defined __native_client__ + pthread_mutex_t m_mutex; +#endif +}; + +class ConditionBase +{ +public: + ConditionBase() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_init(&m_mutex, NULL); + pthread_cond_init(&m_cond, NULL); +#endif + } + + ~ConditionBase() + { +#if defined __linux__ || defined __native_client__ + pthread_cond_destroy(&m_cond); + pthread_mutex_destroy(&m_mutex); +#endif + } + + void Acquire() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_lock(&m_mutex); +#endif + } + + void Release() + { +#if defined __linux__ || defined __native_client__ + pthread_mutex_unlock(&m_mutex); +#endif + } + + void Wait() + { +#if defined __linux__ || defined __native_client__ + pthread_cond_wait(&m_cond, &m_mutex); +#endif + } + + void Notify() + { +#if defined __linux__ || defined __native_client__ + pthread_cond_signal(&m_cond); +#endif + } + +private: +#if defined __linux__ || defined __native_client__ + pthread_mutex_t m_mutex; + pthread_cond_t m_cond; +#endif +}; + +class ThreadBase +{ +public: + ThreadBase(void *(*fn)(void *), void *data) + { +#if defined __linux__ || defined __native_client__ + pthread_create(&m_thread, NULL, fn, data); +#endif + } + + virtual ~ThreadBase() + { +#if defined __linux__ || defined __native_client__ + pthread_join(m_thread, NULL); +#endif + } + +private: +#if defined __linux__ || defined __native_client__ + pthread_t m_thread; +#endif +}; + +} /* namespace lol */ + +#endif // __LOL_THREADBASE_H__ +