µGFX  2.9
version 2.9
gos_qt.cpp
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 #include "../../gfx.h"
9 
10 #if GFX_USE_OS_QT
11 #include <QMutex>
12 #include <QSemaphore>
13 #include <QThread>
14 #include <QElapsedTimer>
15 
16 extern "C" void _gosPostInit(void);
17 
18 class Thread : public QThread
19 {
20 public:
21  typedef gThreadreturn (*fptr)(void* param);
22 
23  void setFunction(fptr function, void* param)
24  {
25  _function = function;
26  _param = param;
27  }
28 
29  gThreadreturn returnValue()
30  {
31  return _returnValue;
32  }
33 
34  virtual void run() override
35  {
36  if (!_function) {
37  return;
38  }
39 
40  _returnValue = _function(_param);
41  }
42 
43 private:
44  fptr _function;
45  void* _param;
46  gThreadreturn _returnValue;
47 };
48 
49 static QElapsedTimer _systickTimer;
50 static QMutex _systemMutex;
51 
52 void _gosInit(void)
53 {
54  _systickTimer.start();
55 }
56 
57 void _gosPostInit(void)
58 {
59 }
60 
61 void _gosDeinit(void)
62 {
63 }
64 
65 void gfxHalt(const char *msg)
66 {
67  volatile gU32 dummy;
68 
69  (void)msg;
70 
71  while(1) {
72  dummy++;
73  }
74 }
75 
76 void gfxExit(void)
77 {
78  volatile gU32 dummy;
79 
80  while(1) {
81  dummy++;
82  }
83 }
84 
85 void* gfxAlloc(gMemSize sz)
86 {
87  return malloc(sz);
88 }
89 
90 void* gfxRealloc(void* ptr, gMemSize oldsz, gMemSize newsz)
91 {
92  Q_UNUSED(oldsz)
93  return realloc(ptr, newsz);
94 }
95 
96 void gfxFree(void* ptr)
97 {
98  free(ptr);
99 }
100 
101 void gfxYield(void)
102 {
103  QThread::msleep(0);
104 }
105 
106 void gfxSleepMilliseconds(gDelay ms)
107 {
108  QThread::msleep(ms);
109 }
110 
111 void gfxSleepMicroseconds(gDelay us)
112 {
113  QThread::usleep(us);
114 }
115 
116 gTicks gfxSystemTicks(void)
117 {
118  return _systickTimer.elapsed();
119 }
120 
121 gTicks gfxMillisecondsToTicks(gDelay ms)
122 {
123  return ms;
124 }
125 
126 void gfxSystemLock(void)
127 {
128  _systemMutex.lock();
129 }
130 
131 void gfxSystemUnlock(void)
132 {
133  _systemMutex.unlock();
134 }
135 
136 void gfxMutexInit(gMutex *pmutex)
137 {
138  *pmutex = new QMutex;
139 }
140 
141 void gfxMutexDestroy(gMutex *pmutex)
142 {
143  delete static_cast<QMutex*>(*pmutex);
144 }
145 
146 void gfxMutexEnter(gMutex *pmutex)
147 {
148  static_cast<QMutex*>(*pmutex)->lock();
149 }
150 
151 void gfxMutexExit(gMutex *pmutex)
152 {
153  static_cast<QMutex*>(*pmutex)->unlock();
154 }
155 
156 void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
157 {
158  *psem = new QSemaphore(limit);
159 
160  static_cast<QSemaphore*>(*psem)->release(val);
161 }
162 
163 void gfxSemDestroy(gSem *psem)
164 {
165  delete static_cast<QSemaphore*>(*psem);
166 }
167 
168 gBool gfxSemWait(gSem *psem, gDelay ms)
169 {
170  return static_cast<QSemaphore*>(*psem)->tryAcquire(1, ms);
171 }
172 
173 gBool gfxSemWaitI(gSem *psem)
174 {
175  return static_cast<QSemaphore*>(*psem)->tryAcquire(1);
176 }
177 
178 void gfxSemSignal(gSem *psem)
179 {
180  static_cast<QSemaphore*>(*psem)->release(1);
181 }
182 
183 void gfxSemSignalI(gSem *psem)
184 {
185  static_cast<QSemaphore*>(*psem)->release(1);
186 }
187 
188 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param)
189 {
190  Q_UNUSED(stackarea)
191 
192  Thread* thread = new Thread;
193  thread->setFunction(fn, param);
194  if (stacksz > 0) {
195  thread->setStackSize(stacksz);
196  }
197  thread->start(static_cast<QThread::Priority>(prio));
198 
199  return static_cast<gThread>(thread);
200 }
201 
202 gThreadreturn gfxThreadWait(gThread thread)
203 {
204  Thread* t = static_cast<Thread*>(thread);
205 
206  gThreadreturn returnValue = t->returnValue();
207  t->wait();
208  t->exit();
209 
210  return returnValue;
211 }
212 
213 gThread gfxThreadMe(void)
214 {
215  return static_cast<Thread*>(QThread::currentThread());
216 }
217 
218 void gfxThreadClose(gThread thread)
219 {
220  static_cast<Thread*>(thread)->exit();
221 }
222 
223 #endif /* GFX_USE_OS_QT */
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
Definition: gos.h:116
gTicks gfxSystemTicks(void)
Get the current operating system tick time.
void gfxYield(void)
Yield the current thread.
#define GFX_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:73
void gfxMutexExit(gMutex *pmutex)
Exit the critical code region protected by the mutex.
gTicks gfxMillisecondsToTicks(gDelay ms)
Convert a given number of millseconds to a number of operating system ticks.
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
void gfxExit(void)
Exit the GFX application.
void gfxSystemUnlock(void)
Unlock the operating system previous locked by gfxSystemLock()
void gfxSleepMicroseconds(gDelay us)
Put the current thread to sleep for the specified period in microseconds.
void gfxMutexEnter(gMutex *pmutex)
Enter the critical code region protected by the mutex.
gThread gfxThreadMe(void)
Get the current thread handle.
void * gfxAlloc(gMemSize sz)
Allocate memory.
void gfxSystemLock(void)
Lock the operating system to protect a sequence of code.
void gfxMutexInit(gMutex *pmutex)
Initialise a mutex to protect a region of code from other threads.
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
gBool gfxSemWaitI(gSem *psem)
Test if a wait on a semaphore can be satisfied immediately.
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
void gfxSemDestroy(gSem *psem)
Destroy a Counted Semaphore.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
void * gfxRealloc(void *ptr, gMemSize oldsz, gMemSize newsz)
Re-allocate memory.
void gfxMutexDestroy(gMutex *pmutex)
Destroy a Mutex.
void gfxFree(void *ptr)
Free memory.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
void gfxSleepMilliseconds(gDelay ms)
Put the current thread to sleep for the specified period in milliseconds.
void gfxThreadClose(gThread thread)
Close the thread handle.
A mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104