From 4ee1c4f7e16619afad167d8f4292e7179db332f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 20 Oct 2013 14:53:53 +0200 Subject: [PATCH] Threading now uses QThread instead of glib threads --- src/rendering/render.c | 1 + src/rendering/renderer.c | 1 + src/rendering/system.h | 19 --------- src/rendering/tools/parallel.c | 1 + src/system/PictureFile.cpp | 2 + src/system/PictureFile.h | 3 +- src/system/Thread.cpp | 45 +++++++++++++++++++++ src/system/Thread.h | 71 ++++++++++++++++++++++++++++++++++ src/system/system.pro | 6 ++- src/system/system_global.h | 2 +- 10 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 src/system/Thread.cpp create mode 100644 src/system/Thread.h diff --git a/src/rendering/render.c b/src/rendering/render.c index 5dcdaf6..72bf829 100644 --- a/src/rendering/render.c +++ b/src/rendering/render.c @@ -8,6 +8,7 @@ #include "camera.h" #include "system.h" #include "PictureFile.h" +#include "Thread.h" typedef struct { diff --git a/src/rendering/renderer.c b/src/rendering/renderer.c index 60bf678..704e195 100644 --- a/src/rendering/renderer.c +++ b/src/rendering/renderer.c @@ -5,6 +5,7 @@ #include "render.h" #include "scenery.h" #include "tools.h" +#include "Thread.h" static RayCastingResult _RAYCASTING_NULL = {0}; diff --git a/src/rendering/system.h b/src/rendering/system.h index 5bda037..1515c15 100644 --- a/src/rendering/system.h +++ b/src/rendering/system.h @@ -9,8 +9,6 @@ extern "C" { #endif -typedef void* (*ThreadFunction)(void* data); - void systemInit(); int systemGetCoreCount(); @@ -19,23 +17,6 @@ int systemGetFileSize(const char* path); #ifdef HAVE_GLIB #include -typedef GThread Thread; - -static inline Thread* threadCreate(ThreadFunction function, void* data) -{ -#ifdef GLIB_VERSION_2_32 - return g_thread_new("thread", (GThreadFunc)function, data); -#else - GError* error; - return g_thread_create((GThreadFunc)function, data, 1, &error); -#endif -} - -static inline void* threadJoin(Thread* thread) -{ - return g_thread_join(thread); -} - typedef GMutex Mutex; static inline Mutex* mutexCreate() diff --git a/src/rendering/tools/parallel.c b/src/rendering/tools/parallel.c index f72156c..52f74b9 100644 --- a/src/rendering/tools/parallel.c +++ b/src/rendering/tools/parallel.c @@ -2,6 +2,7 @@ #include #include "../system.h" +#include "Thread.h" #define PARALLEL_MAX_THREADS 20 diff --git a/src/system/PictureFile.cpp b/src/system/PictureFile.cpp index afcdb07..4db268d 100644 --- a/src/system/PictureFile.cpp +++ b/src/system/PictureFile.cpp @@ -7,6 +7,8 @@ PictureFile::PictureFile() { } +// Transitional C-API + int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callback_pixel, void* data, int width, int height) { QImage result(width, height, QImage::Format_ARGB32); diff --git a/src/system/PictureFile.h b/src/system/PictureFile.h index 727a0a4..0b8f2c1 100644 --- a/src/system/PictureFile.h +++ b/src/system/PictureFile.h @@ -1,9 +1,10 @@ #ifndef PICTUREFILE_H #define PICTUREFILE_H -#ifdef __cplusplus #include "system_global.h" +#ifdef __cplusplus + namespace paysages { namespace system diff --git a/src/system/Thread.cpp b/src/system/Thread.cpp new file mode 100644 index 0000000..e2d5590 --- /dev/null +++ b/src/system/Thread.cpp @@ -0,0 +1,45 @@ +#include "Thread.h" + +Thread::Thread(ThreadFunction function): + function(function), data(0), result(0) +{ +} + +void Thread::start(void* data) +{ + this->data = data; + QThread::start(); +} + +void* Thread::join() +{ + QThread::wait(); + return result; +} + +void Thread::run() +{ + result = function(data); +} + +// Transitional C-API + +Thread* threadCreate(ThreadFunction function, void* data) +{ + Thread* result = new Thread(function); + + result->start(data); + + return result; +} + +void* threadJoin(Thread* thread) +{ + void* result; + + result = thread->join(); + + delete thread; + + return result; +} diff --git a/src/system/Thread.h b/src/system/Thread.h new file mode 100644 index 0000000..6701681 --- /dev/null +++ b/src/system/Thread.h @@ -0,0 +1,71 @@ +#ifndef THREAD_H +#define THREAD_H + +#include "system_global.h" + +typedef void* (*ThreadFunction)(void* data); + +#ifdef __cplusplus + +#include + +namespace paysages +{ +namespace system +{ + +/*! + * \brief System thread + */ +class Thread: private QThread +{ +public: + /*! + * \brief Create a new thread + * \param function Function to call inside the thread once it is started + * + * The thread is not started automatically. A call to method start() needs to be done. + */ + Thread(ThreadFunction function); + + /*! + * \brief Start the thread + * \param data User data to pass to the threaded function + */ + void start(void* data); + + /*! + * \brief Wait for the thread to end, and collect its result. + * \return The value returned by the threaded function. + */ + void* join(); + +protected: + virtual void run(); + +private: + ThreadFunction function; + void* data; + void* result; +}; + +} +} + +extern "C" { +#endif + +// Transitional C-API + +#ifndef __cplusplus +typedef struct Thread Thread; +#endif + +Thread* threadCreate(ThreadFunction function, void* data); +void* threadJoin(Thread* thread); + +#ifdef __cplusplus +} +#endif + +#endif // THREAD_H diff --git a/src/system/system.pro b/src/system/system.pro index 6fb74de..bf2a8dc 100644 --- a/src/system/system.pro +++ b/src/system/system.pro @@ -12,11 +12,13 @@ TEMPLATE = lib DEFINES += SYSTEM_LIBRARY SOURCES += \ - PictureFile.cpp + PictureFile.cpp \ + Thread.cpp HEADERS +=\ system_global.h \ - PictureFile.h + PictureFile.h \ + Thread.h unix:!symbian { maemo5 { diff --git a/src/system/system_global.h b/src/system/system_global.h index 5b4e693..0e02f0c 100644 --- a/src/system/system_global.h +++ b/src/system/system_global.h @@ -1,6 +1,7 @@ #ifndef SYSTEM_GLOBAL_H #define SYSTEM_GLOBAL_H +#ifdef __cplusplus #include #if defined(SYSTEM_LIBRARY) @@ -9,7 +10,6 @@ # define SYSTEMSHARED_EXPORT Q_DECL_IMPORT #endif -#ifdef __cplusplus namespace paysages { namespace system {}