9 #define GFILE_NEED_STDIO_MUST_BE_OFF
11 #include "../../gfx.h"
16 #define USE_SCHED_NOT_PTHREAD_YIELD GFXON
21 #if USE_SCHED_NOT_PTHREAD_YIELD
23 #define linuxyield() sched_yield()
25 #define linuxyield() pthread_yield()
36 void _gosPostInit(
void)
59 fprintf(stderr,
"%s\n", msg);
77 ts.tv_sec = ms / 1000;
78 ts.tv_nsec = (ms % 1000) * 1000000;
98 ts.tv_sec = us / 1000000;
99 ts.tv_nsec = (us % 1000000) * 1000;
108 clock_gettime(CLOCK_MONOTONIC, &ts);
109 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
125 if (pthread_create(&th, 0, fn, param))
132 gThreadreturn retval;
134 if (pthread_join(thread, &retval))
140 #if GFX_USE_POSIX_SEMAPHORES
143 sem_init(&pSem->sem, 0, val);
146 sem_destroy(&pSem->sem);
151 return sem_wait(&pSem->sem) ? gFalse : gTrue;
154 return sem_trywait(&pSem->sem) ? gFalse : gTrue;
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;
171 sem_getvalue(&pSem->sem, &res);
173 sem_post(&pSem->sem);
177 pthread_mutex_init(&pSem->mtx, 0);
178 pthread_cond_init(&pSem->cond, 0);
179 pthread_mutex_lock(&pSem->mtx);
182 pthread_mutex_unlock(&pSem->mtx);
185 pthread_mutex_destroy(&pSem->mtx);
186 pthread_cond_destroy(&pSem->cond);
189 pthread_mutex_lock(&pSem->mtx);
194 pthread_cond_wait(&pSem->cond, &pSem->mtx);
199 pthread_mutex_unlock(&pSem->mtx);
208 clock_gettime(CLOCK_REALTIME, &tm);
209 tm.tv_sec += ms / 1000;
210 tm.tv_nsec += (ms % 1000) * 1000000;
216 if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
217 pthread_mutex_unlock(&pSem->mtx);
226 pthread_mutex_unlock(&pSem->mtx);
230 pthread_mutex_lock(&pSem->mtx);
232 if (pSem->cnt < pSem->max) {
234 pthread_cond_signal(&pSem->cond);
237 pthread_mutex_unlock(&pSem->mtx);
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
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.
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.