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