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:
|
|
|
|
*
|
|
|
|
* http://ugfx.org/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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:
|
2018-07-08 04:30:31 +00:00
|
|
|
typedef gThreadreturn (*fptr)(void* param);
|
2016-07-18 23:30:20 +00:00
|
|
|
|
|
|
|
void setFunction(fptr function, void* param)
|
|
|
|
{
|
|
|
|
_function = function;
|
|
|
|
_param = param;
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
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;
|
2018-07-08 04:30:31 +00:00
|
|
|
gThreadreturn _returnValue;
|
2016-07-18 23:30:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static QElapsedTimer _systickTimer;
|
|
|
|
static QMutex _systemMutex;
|
|
|
|
|
|
|
|
void _gosInit(void)
|
|
|
|
{
|
|
|
|
_systickTimer.start();
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:02:55 +00:00
|
|
|
void _gosPostInit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-18 23:30:20 +00:00
|
|
|
void _gosDeinit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxHalt(const char *msg)
|
|
|
|
{
|
|
|
|
volatile uint32_t dummy;
|
|
|
|
|
|
|
|
(void)msg;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
dummy++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxExit(void)
|
|
|
|
{
|
|
|
|
volatile uint32_t dummy;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
dummy++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* gfxAlloc(size_t sz)
|
|
|
|
{
|
|
|
|
return malloc(sz);
|
|
|
|
}
|
|
|
|
|
2018-02-20 13:51:37 +00:00
|
|
|
void* gfxRealloc(void* ptr, size_t oldsz, size_t newsz)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
void gfxSleepMilliseconds(gDelay ms)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
QThread::msleep(ms);
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
void gfxSleepMicroseconds(gDelay us)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
QThread::usleep(us);
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
gTicks gfxSystemTicks(void)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
return _systickTimer.elapsed();
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
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(gfxMutex *pmutex)
|
|
|
|
{
|
|
|
|
*pmutex = new QMutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxMutexDestroy(gfxMutex *pmutex)
|
|
|
|
{
|
|
|
|
delete static_cast<QMutex*>(*pmutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxMutexEnter(gfxMutex *pmutex)
|
|
|
|
{
|
|
|
|
static_cast<QMutex*>(*pmutex)->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxMutexExit(gfxMutex *pmutex)
|
|
|
|
{
|
|
|
|
static_cast<QMutex*>(*pmutex)->unlock();
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
void gfxSemInit(gfxSem *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(gfxSem *psem)
|
|
|
|
{
|
|
|
|
delete static_cast<QSemaphore*>(*psem);
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
gBool gfxSemWait(gfxSem *psem, gDelay ms)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
return static_cast<QSemaphore*>(*psem)->tryAcquire(1, ms);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:02:07 +00:00
|
|
|
gBool gfxSemWaitI(gfxSem *psem)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
return static_cast<QSemaphore*>(*psem)->tryAcquire(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxSemSignal(gfxSem *psem)
|
|
|
|
{
|
|
|
|
static_cast<QSemaphore*>(*psem)->release(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxSemSignalI(gfxSem *psem)
|
|
|
|
{
|
|
|
|
static_cast<QSemaphore*>(*psem)->release(1);
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:40:27 +00:00
|
|
|
gThread gfxThreadCreate(void *stackarea, size_t stacksz, gThreadpriority prio, DECLARE_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));
|
|
|
|
|
2018-07-08 05:40:27 +00:00
|
|
|
return static_cast<gThread>(thread);
|
2016-07-18 23:30:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 05:40:27 +00:00
|
|
|
gThreadreturn gfxThreadWait(gThread thread)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
Thread* t = static_cast<Thread*>(thread);
|
|
|
|
|
2018-07-08 04:30:31 +00:00
|
|
|
gThreadreturn returnValue = t->returnValue();
|
2016-07-18 23:30:20 +00:00
|
|
|
t->wait();
|
|
|
|
t->exit();
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:40:27 +00:00
|
|
|
gThread gfxThreadMe(void)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
return static_cast<Thread*>(QThread::currentThread());
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:40:27 +00:00
|
|
|
void gfxThreadClose(gThread thread)
|
2016-07-18 23:30:20 +00:00
|
|
|
{
|
|
|
|
static_cast<Thread*>(thread)->exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* GFX_USE_OS_QT */
|