µGFX  2.9
version 2.9
gos_linux.c
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 // We need to include stdio.h below. Turn off GFILE_NEED_STDIO just for this file to prevent conflicts
9 #define GFILE_NEED_STDIO_MUST_BE_OFF
10 
11 #include "../../gfx.h"
12 
13 #if GFX_USE_OS_LINUX
14 
15 // Linux seems to have deprecated pthread_yield() and now says to use sched_yield()
16 #define USE_SCHED_NOT_PTHREAD_YIELD GFXON
17 
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <time.h>
21 #if USE_SCHED_NOT_PTHREAD_YIELD
22  #include <sched.h>
23  #define linuxyield() sched_yield()
24 #else
25  #define linuxyield() pthread_yield()
26 #endif
27 
28 static gMutex SystemMutex;
29 
30 void _gosInit(void)
31 {
32  /* No initialization of the operating system itself is needed */
33  gfxMutexInit(&SystemMutex);
34 }
35 
36 void _gosPostInit(void)
37 {
38 }
39 
40 void _gosDeinit(void)
41 {
42  /* ToDo */
43 }
44 
45 void gfxSystemLock(void) {
46  gfxMutexEnter(&SystemMutex);
47 }
48 
49 void gfxSystemUnlock(void) {
50  gfxMutexExit(&SystemMutex);
51 }
52 
53 void gfxYield(void) {
54  linuxyield();
55 }
56 
57 void gfxHalt(const char *msg) {
58  if (msg)
59  fprintf(stderr, "%s\n", msg);
60  exit(1);
61 }
62 
63 void gfxSleepMilliseconds(gDelay ms) {
64  struct timespec ts;
65 
66  switch(ms) {
67  case gDelayNone:
68  linuxyield();
69  return;
70 
71  case gDelayForever:
72  while(1)
73  sleep(60);
74  return;
75 
76  default:
77  ts.tv_sec = ms / 1000;
78  ts.tv_nsec = (ms % 1000) * 1000000;
79  nanosleep(&ts, 0);
80  return;
81  }
82 }
83 
84 void gfxSleepMicroseconds(gDelay us) {
85  struct timespec ts;
86 
87  switch(us) {
88  case gDelayNone:
89  linuxyield();
90  return;
91 
92  case gDelayForever:
93  while(1)
94  sleep(60);
95  return;
96 
97  default:
98  ts.tv_sec = us / 1000000;
99  ts.tv_nsec = (us % 1000000) * 1000;
100  nanosleep(&ts, 0);
101  return;
102  }
103 }
104 
105 gTicks gfxSystemTicks(void) {
106  struct timespec ts;
107 
108  clock_gettime(CLOCK_MONOTONIC, &ts);
109  return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
110 }
111 
112 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param) {
113  gThread th;
114  (void) stackarea;
115  (void) stacksz;
116  (void) prio;
117 
118  // Implementing priority with pthreads is a rats nest that is also pthreads implementation dependent.
119  // Only some pthreads schedulers support it, some implementations use the operating system process priority mechanisms.
120  // Even those that do support it can have different ranges of priority and "normal" priority is an undefined concept.
121  // Across different UNIX style operating systems things can be very different (let alone OS's such as Windows).
122  // Even just Linux changes the way priority works with different kernel schedulers and across kernel versions.
123  // For these reasons we ignore the priority.
124 
125  if (pthread_create(&th, 0, fn, param))
126  return 0;
127 
128  return th;
129 }
130 
131 gThreadreturn gfxThreadWait(gThread thread) {
132  gThreadreturn retval;
133 
134  if (pthread_join(thread, &retval))
135  return 0;
136 
137  return retval;
138 }
139 
140 #if GFX_USE_POSIX_SEMAPHORES
141  void gfxSemInit(gSem *pSem, gSemcount val, gSemcount limit) {
142  pSem->max = limit;
143  sem_init(&pSem->sem, 0, val);
144  }
145  void gfxSemDestroy(gSem *pSem) {
146  sem_destroy(&pSem->sem);
147  }
148  gBool gfxSemWait(gSem *pSem, gDelay ms) {
149  switch (ms) {
150  case gDelayForever:
151  return sem_wait(&pSem->sem) ? gFalse : gTrue;
152 
153  case gDelayNone:
154  return sem_trywait(&pSem->sem) ? gFalse : gTrue;
155 
156  default:
157  {
158  struct timespec tm;
159 
160  clock_gettime(CLOCK_REALTIME, &tm);
161  tm.tv_sec += ms / 1000;
162  tm.tv_nsec += (ms % 1000) * 1000000;
163  return sem_timedwait(&pSem->sem, &tm) ? gFalse : gTrue;
164  }
165  }
166  }
167  void gfxSemSignal(gSem *pSem) {
168  int res;
169 
170  res = 0;
171  sem_getvalue(&pSem->sem, &res);
172  if (res < pSem->max)
173  sem_post(&pSem->sem);
174  }
175 #else
176  void gfxSemInit(gSem *pSem, gSemcount val, gSemcount limit) {
177  pthread_mutex_init(&pSem->mtx, 0);
178  pthread_cond_init(&pSem->cond, 0);
179  pthread_mutex_lock(&pSem->mtx);
180  pSem->cnt = val;
181  pSem->max = limit;
182  pthread_mutex_unlock(&pSem->mtx);
183  }
184  void gfxSemDestroy(gSem *pSem) {
185  pthread_mutex_destroy(&pSem->mtx);
186  pthread_cond_destroy(&pSem->cond);
187  }
188  gBool gfxSemWait(gSem *pSem, gDelay ms) {
189  pthread_mutex_lock(&pSem->mtx);
190 
191  switch (ms) {
192  case gDelayForever:
193  while (!pSem->cnt)
194  pthread_cond_wait(&pSem->cond, &pSem->mtx);
195  break;
196 
197  case gDelayNone:
198  if (!pSem->cnt) {
199  pthread_mutex_unlock(&pSem->mtx);
200  return gFalse;
201  }
202  break;
203 
204  default:
205  {
206  struct timespec tm;
207 
208  clock_gettime(CLOCK_REALTIME, &tm);
209  tm.tv_sec += ms / 1000;
210  tm.tv_nsec += (ms % 1000) * 1000000;
211  while (!pSem->cnt) {
212  // We used to test the return value for ETIMEDOUT. This doesn't
213  // work in some current pthread libraries which return -1 instead
214  // and set errno to ETIMEDOUT. So, we will return gFalse on any error
215  // including a ETIMEDOUT.
216  if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
217  pthread_mutex_unlock(&pSem->mtx);
218  return gFalse;
219  }
220  }
221  }
222  break;
223  }
224 
225  pSem->cnt--;
226  pthread_mutex_unlock(&pSem->mtx);
227  return gTrue;
228  }
229  void gfxSemSignal(gSem *pSem) {
230  pthread_mutex_lock(&pSem->mtx);
231 
232  if (pSem->cnt < pSem->max) {
233  pSem->cnt++;
234  pthread_cond_signal(&pSem->cond);
235  }
236 
237  pthread_mutex_unlock(&pSem->mtx);
238  }
239 #endif // GFX_USE_POSIX_SEMAPHORES
240 
241 #endif /* GFX_USE_OS_LINUX */
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.
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
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.
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.
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 gfxSleepMilliseconds(gDelay ms)
Put the current thread to sleep for the specified period in milliseconds.
A mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104