Threading now uses QThread instead of glib threads

This commit is contained in:
Michaël Lemaire 2013-10-20 14:53:53 +02:00
parent b896832e76
commit 4ee1c4f7e1
10 changed files with 128 additions and 23 deletions

View file

@ -8,6 +8,7 @@
#include "camera.h" #include "camera.h"
#include "system.h" #include "system.h"
#include "PictureFile.h" #include "PictureFile.h"
#include "Thread.h"
typedef struct typedef struct
{ {

View file

@ -5,6 +5,7 @@
#include "render.h" #include "render.h"
#include "scenery.h" #include "scenery.h"
#include "tools.h" #include "tools.h"
#include "Thread.h"
static RayCastingResult _RAYCASTING_NULL = {0}; static RayCastingResult _RAYCASTING_NULL = {0};

View file

@ -9,8 +9,6 @@
extern "C" { extern "C" {
#endif #endif
typedef void* (*ThreadFunction)(void* data);
void systemInit(); void systemInit();
int systemGetCoreCount(); int systemGetCoreCount();
@ -19,23 +17,6 @@ int systemGetFileSize(const char* path);
#ifdef HAVE_GLIB #ifdef HAVE_GLIB
#include <glib.h> #include <glib.h>
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; typedef GMutex Mutex;
static inline Mutex* mutexCreate() static inline Mutex* mutexCreate()

View file

@ -2,6 +2,7 @@
#include <assert.h> #include <assert.h>
#include "../system.h" #include "../system.h"
#include "Thread.h"
#define PARALLEL_MAX_THREADS 20 #define PARALLEL_MAX_THREADS 20

View file

@ -7,6 +7,8 @@ PictureFile::PictureFile()
{ {
} }
// Transitional C-API
int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callback_pixel, void* data, int width, int height) int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callback_pixel, void* data, int width, int height)
{ {
QImage result(width, height, QImage::Format_ARGB32); QImage result(width, height, QImage::Format_ARGB32);

View file

@ -1,9 +1,10 @@
#ifndef PICTUREFILE_H #ifndef PICTUREFILE_H
#define PICTUREFILE_H #define PICTUREFILE_H
#ifdef __cplusplus
#include "system_global.h" #include "system_global.h"
#ifdef __cplusplus
namespace paysages namespace paysages
{ {
namespace system namespace system

45
src/system/Thread.cpp Normal file
View file

@ -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;
}

71
src/system/Thread.h Normal file
View file

@ -0,0 +1,71 @@
#ifndef THREAD_H
#define THREAD_H
#include "system_global.h"
typedef void* (*ThreadFunction)(void* data);
#ifdef __cplusplus
#include <QThread>
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

View file

@ -12,11 +12,13 @@ TEMPLATE = lib
DEFINES += SYSTEM_LIBRARY DEFINES += SYSTEM_LIBRARY
SOURCES += \ SOURCES += \
PictureFile.cpp PictureFile.cpp \
Thread.cpp
HEADERS +=\ HEADERS +=\
system_global.h \ system_global.h \
PictureFile.h PictureFile.h \
Thread.h
unix:!symbian { unix:!symbian {
maemo5 { maemo5 {

View file

@ -1,6 +1,7 @@
#ifndef SYSTEM_GLOBAL_H #ifndef SYSTEM_GLOBAL_H
#define SYSTEM_GLOBAL_H #define SYSTEM_GLOBAL_H
#ifdef __cplusplus
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#if defined(SYSTEM_LIBRARY) #if defined(SYSTEM_LIBRARY)
@ -9,7 +10,6 @@
# define SYSTEMSHARED_EXPORT Q_DECL_IMPORT # define SYSTEMSHARED_EXPORT Q_DECL_IMPORT
#endif #endif
#ifdef __cplusplus
namespace paysages namespace paysages
{ {
namespace system {} namespace system {}