ugfx/src/gos/gos_qt.cpp

224 lines
3.5 KiB
C++
Raw Normal View History

2016-07-18 23:30:20 +00:00
/*
* This file is subject to the terms of the GFX License. If a copy of
* the license was not distributed with this file, you can obtain one at:
*
2018-10-01 15:32:39 +00:00
* http://ugfx.io/license.html
2016-07-18 23:30:20 +00:00
*/
#include "../../gfx.h"
2016-08-13 18:05:49 +00:00
#if GFX_USE_OS_QT
2016-07-18 23:30:20 +00:00
#include <QMutex>
#include <QSemaphore>
#include <QThread>
#include <QElapsedTimer>
2018-02-20 13:53:17 +00:00
extern "C" void _gosPostInit(void);
2016-07-18 23:30:20 +00:00
class Thread : public QThread
{
public:
typedef gThreadreturn (*fptr)(void* param);
2016-07-18 23:30:20 +00:00
void setFunction(fptr function, void* param)
{
_function = function;
_param = param;
}
gThreadreturn returnValue()
2016-07-18 23:30:20 +00:00
{
return _returnValue;
}
virtual void run() override
{
if (!_function) {
return;
}
_returnValue = _function(_param);
}
private:
fptr _function;
void* _param;
gThreadreturn _returnValue;
2016-07-18 23:30:20 +00:00
};
static QElapsedTimer _systickTimer;
static QMutex _systemMutex;
void _gosInit(void)
{
_systickTimer.start();
}
void _gosPostInit(void)
{
}
2016-07-18 23:30:20 +00:00
void _gosDeinit(void)
{
}
void gfxHalt(const char *msg)
{
volatile gU32 dummy;
2016-07-18 23:30:20 +00:00
(void)msg;
while(1) {
dummy++;
}
}
void gfxExit(void)
{
volatile gU32 dummy;
2016-07-18 23:30:20 +00:00
while(1) {
dummy++;
}
}
void* gfxAlloc(gMemSize sz)
2016-07-18 23:30:20 +00:00
{
return malloc(sz);
}
void* gfxRealloc(void* ptr, gMemSize oldsz, gMemSize newsz)
2018-02-20 13:51:37 +00:00
{
2018-02-20 13:54:02 +00:00
Q_UNUSED(oldsz)
2018-03-06 08:42:00 +00:00
return realloc(ptr, newsz);
2018-02-20 13:51:37 +00:00
}
2016-07-18 23:30:20 +00:00
void gfxFree(void* ptr)
{
free(ptr);
}
void gfxYield(void)
{
QThread::msleep(0);
}
void gfxSleepMilliseconds(gDelay ms)
2016-07-18 23:30:20 +00:00
{
QThread::msleep(ms);
}
void gfxSleepMicroseconds(gDelay us)
2016-07-18 23:30:20 +00:00
{
QThread::usleep(us);
}
gTicks gfxSystemTicks(void)
2016-07-18 23:30:20 +00:00
{
return _systickTimer.elapsed();
}
gTicks gfxMillisecondsToTicks(gDelay ms)
2016-07-18 23:30:20 +00:00
{
return ms;
}
void gfxSystemLock(void)
{
_systemMutex.lock();
}
void gfxSystemUnlock(void)
{
_systemMutex.unlock();
}
void gfxMutexInit(gMutex *pmutex)
2016-07-18 23:30:20 +00:00
{
*pmutex = new QMutex;
}
void gfxMutexDestroy(gMutex *pmutex)
2016-07-18 23:30:20 +00:00
{
delete static_cast<QMutex*>(*pmutex);
}
void gfxMutexEnter(gMutex *pmutex)
2016-07-18 23:30:20 +00:00
{
static_cast<QMutex*>(*pmutex)->lock();
}
void gfxMutexExit(gMutex *pmutex)
2016-07-18 23:30:20 +00:00
{
static_cast<QMutex*>(*pmutex)->unlock();
}
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
2016-07-18 23:30:20 +00:00
{
*psem = new QSemaphore(limit);
static_cast<QSemaphore*>(*psem)->release(val);
}
void gfxSemDestroy(gSem *psem)
2016-07-18 23:30:20 +00:00
{
delete static_cast<QSemaphore*>(*psem);
}
gBool gfxSemWait(gSem *psem, gDelay ms)
2016-07-18 23:30:20 +00:00
{
return static_cast<QSemaphore*>(*psem)->tryAcquire(1, ms);
}
gBool gfxSemWaitI(gSem *psem)
2016-07-18 23:30:20 +00:00
{
return static_cast<QSemaphore*>(*psem)->tryAcquire(1);
}
void gfxSemSignal(gSem *psem)
2016-07-18 23:30:20 +00:00
{
static_cast<QSemaphore*>(*psem)->release(1);
}
void gfxSemSignalI(gSem *psem)
2016-07-18 23:30:20 +00:00
{
static_cast<QSemaphore*>(*psem)->release(1);
}
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param)
2016-07-18 23:30:20 +00:00
{
Q_UNUSED(stackarea)
Thread* thread = new Thread;
thread->setFunction(fn, param);
if (stacksz > 0) {
thread->setStackSize(stacksz);
}
thread->start(static_cast<QThread::Priority>(prio));
return static_cast<gThread>(thread);
2016-07-18 23:30:20 +00:00
}
gThreadreturn gfxThreadWait(gThread thread)
2016-07-18 23:30:20 +00:00
{
Thread* t = static_cast<Thread*>(thread);
gThreadreturn returnValue = t->returnValue();
2016-07-18 23:30:20 +00:00
t->wait();
t->exit();
return returnValue;
}
gThread gfxThreadMe(void)
2016-07-18 23:30:20 +00:00
{
return static_cast<Thread*>(QThread::currentThread());
}
void gfxThreadClose(gThread thread)
2016-07-18 23:30:20 +00:00
{
static_cast<Thread*>(thread)->exit();
}
#endif /* GFX_USE_OS_QT */