2013-07-22 22:47:42 +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
|
|
|
|
*/
|
|
|
|
|
2015-02-20 23:23:33 +00:00
|
|
|
// We need to include stdio.h below. Turn off GFILE_NEED_STDIO just for this file to prevent conflicts
|
|
|
|
#define GFILE_NEED_STDIO_MUST_BE_OFF
|
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
#include "gfx.h"
|
|
|
|
|
2013-07-23 00:12:52 +00:00
|
|
|
#if GFX_USE_OS_LINUX
|
2013-07-22 22:47:42 +00:00
|
|
|
|
2014-09-27 15:41:07 +00:00
|
|
|
// Linux seems to have deprecated pthread_yield() and now says to use sched_yield()
|
|
|
|
#define USE_SCHED_NOT_PTHREAD_YIELD TRUE
|
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2014-09-27 15:41:07 +00:00
|
|
|
#if USE_SCHED_NOT_PTHREAD_YIELD
|
|
|
|
#include <sched.h>
|
|
|
|
#define linuxyield() sched_yield()
|
|
|
|
#else
|
|
|
|
#define linuxyield() pthread_yield()
|
|
|
|
#endif
|
2013-07-22 22:47:42 +00:00
|
|
|
|
|
|
|
static gfxMutex SystemMutex;
|
|
|
|
|
2014-02-02 18:24:43 +00:00
|
|
|
void _gosInit(void)
|
|
|
|
{
|
2014-07-29 02:00:47 +00:00
|
|
|
/* No initialization of the operating system itself is needed */
|
2013-07-22 22:47:42 +00:00
|
|
|
gfxMutexInit(&SystemMutex);
|
|
|
|
}
|
|
|
|
|
2014-02-02 18:24:43 +00:00
|
|
|
void _gosDeinit(void)
|
|
|
|
{
|
|
|
|
/* ToDo */
|
|
|
|
}
|
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
void gfxSystemLock(void) {
|
|
|
|
gfxMutexEnter(&SystemMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxSystemUnlock(void) {
|
|
|
|
gfxMutexExit(&SystemMutex);
|
|
|
|
}
|
|
|
|
|
2014-09-27 15:41:07 +00:00
|
|
|
void gfxYield(void) {
|
|
|
|
linuxyield();
|
|
|
|
}
|
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
void gfxHalt(const char *msg) {
|
|
|
|
if (msg)
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxSleepMilliseconds(delaytime_t ms) {
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
switch(ms) {
|
2013-12-07 21:01:57 +00:00
|
|
|
case TIME_IMMEDIATE:
|
2014-09-27 15:41:07 +00:00
|
|
|
linuxyield();
|
2013-12-07 21:01:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TIME_INFINITE:
|
|
|
|
while(1)
|
|
|
|
sleep(60);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ts.tv_sec = ms / 1000;
|
2014-09-29 05:45:44 +00:00
|
|
|
ts.tv_nsec = (ms % 1000) * 1000000;
|
2013-12-07 21:01:57 +00:00
|
|
|
nanosleep(&ts, 0);
|
|
|
|
return;
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
void gfxSleepMicroseconds(delaytime_t us) {
|
2013-07-22 22:47:42 +00:00
|
|
|
struct timespec ts;
|
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
switch(us) {
|
2013-12-07 21:01:57 +00:00
|
|
|
case TIME_IMMEDIATE:
|
2014-09-27 15:41:07 +00:00
|
|
|
linuxyield();
|
2013-12-07 21:01:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TIME_INFINITE:
|
|
|
|
while(1)
|
|
|
|
sleep(60);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2014-09-29 05:45:44 +00:00
|
|
|
ts.tv_sec = us / 1000000;
|
|
|
|
ts.tv_nsec = (us % 1000000) * 1000;
|
2013-12-07 21:01:57 +00:00
|
|
|
nanosleep(&ts, 0);
|
|
|
|
return;
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
systemticks_t gfxSystemTicks(void) {
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2014-09-29 05:45:44 +00:00
|
|
|
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
|
|
|
|
gfxThreadHandle th;
|
2014-09-27 15:41:07 +00:00
|
|
|
(void) stackarea;
|
|
|
|
(void) stacksz;
|
|
|
|
(void) prio;
|
2013-07-22 22:47:42 +00:00
|
|
|
|
|
|
|
// Implementing priority with pthreads is a rats nest that is also pthreads implementation dependent.
|
2013-12-07 21:01:57 +00:00
|
|
|
// Only some pthreads schedulers support it, some implementations use the operating system process priority mechanisms.
|
|
|
|
// Even those that do support it can have different ranges of priority and "normal" priority is an undefined concept.
|
|
|
|
// Across different UNIX style operating systems things can be very different (let alone OS's such as Windows).
|
|
|
|
// Even just Linux changes the way priority works with different kernel schedulers and across kernel versions.
|
|
|
|
// For these reasons we ignore the priority.
|
2013-07-22 22:47:42 +00:00
|
|
|
|
|
|
|
if (pthread_create(&th, 0, fn, param))
|
|
|
|
return 0;
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
return th;
|
|
|
|
}
|
|
|
|
|
|
|
|
threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
|
|
|
|
threadreturn_t retval;
|
|
|
|
|
|
|
|
if (pthread_join(thread, &retval))
|
|
|
|
return 0;
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2013-07-22 22:47:42 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
#if GFX_USE_POSIX_SEMAPHORES
|
|
|
|
void gfxSemInit(gfxSem *pSem, semcount_t val, semcount_t limit) {
|
|
|
|
pSem->max = limit;
|
|
|
|
sem_init(&pSem->sem, 0, val);
|
|
|
|
}
|
|
|
|
void gfxSemDestroy(gfxSem *pSem) {
|
|
|
|
sem_destroy(&pSem->sem);
|
|
|
|
}
|
|
|
|
bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
|
|
|
|
switch (ms) {
|
2013-12-07 21:01:57 +00:00
|
|
|
case TIME_INFINITE:
|
2014-09-29 05:45:44 +00:00
|
|
|
return sem_wait(&pSem->sem) ? FALSE : TRUE;
|
2013-12-07 21:01:57 +00:00
|
|
|
|
|
|
|
case TIME_IMMEDIATE:
|
2014-09-29 05:45:44 +00:00
|
|
|
return sem_trywait(&pSem->sem) ? FALSE : TRUE;
|
2013-12-07 21:01:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
struct timespec tm;
|
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &tm);
|
|
|
|
tm.tv_sec += ms / 1000;
|
|
|
|
tm.tv_nsec += (ms % 1000) * 1000000;
|
|
|
|
return sem_timedwait(&pSem->sem, &tm) ? FALSE : TRUE;
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
2014-09-29 05:45:44 +00:00
|
|
|
}
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
2014-09-29 05:45:44 +00:00
|
|
|
void gfxSemSignal(gfxSem *pSem) {
|
|
|
|
if (gfxSemCounter(pSem) < pSem->max)
|
|
|
|
sem_post(&pSem->sem);
|
|
|
|
}
|
|
|
|
semcount_t gfxSemCounter(gfxSem *pSem) {
|
|
|
|
int res;
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
res = 0;
|
|
|
|
sem_getvalue(&pSem->sem, &res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void gfxSemInit(gfxSem *pSem, semcount_t val, semcount_t limit) {
|
|
|
|
pthread_mutex_init(&pSem->mtx, 0);
|
|
|
|
pthread_cond_init(&pSem->cond, 0);
|
|
|
|
pthread_mutex_lock(&pSem->mtx);
|
|
|
|
pSem->cnt = val;
|
|
|
|
pSem->max = limit;
|
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
|
|
|
}
|
|
|
|
void gfxSemDestroy(gfxSem *pSem) {
|
|
|
|
pthread_mutex_destroy(&pSem->mtx);
|
|
|
|
pthread_cond_destroy(&pSem->cond);
|
|
|
|
}
|
|
|
|
bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
|
|
|
|
pthread_mutex_lock(&pSem->mtx);
|
|
|
|
|
|
|
|
switch (ms) {
|
|
|
|
case TIME_INFINITE:
|
|
|
|
while (!pSem->cnt)
|
|
|
|
pthread_cond_wait(&pSem->cond, &pSem->mtx);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIME_IMMEDIATE:
|
|
|
|
if (!pSem->cnt) {
|
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
struct timespec tm;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tm);
|
|
|
|
tm.tv_sec += ms / 1000;
|
|
|
|
tm.tv_nsec += (ms % 1000) * 1000000;
|
|
|
|
while (!pSem->cnt) {
|
|
|
|
// We used to test the return value for ETIMEDOUT. This doesn't
|
|
|
|
// work in some current pthread libraries which return -1 instead
|
|
|
|
// and set errno to ETIMEDOUT. So, we will return FALSE on any error
|
|
|
|
// including a ETIMEDOUT.
|
|
|
|
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
|
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
pSem->cnt--;
|
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
|
|
|
return TRUE;
|
2013-07-22 22:47:42 +00:00
|
|
|
}
|
2014-09-29 05:45:44 +00:00
|
|
|
void gfxSemSignal(gfxSem *pSem) {
|
|
|
|
pthread_mutex_lock(&pSem->mtx);
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
if (pSem->cnt < pSem->max) {
|
|
|
|
pSem->cnt++;
|
|
|
|
pthread_cond_signal(&pSem->cond);
|
|
|
|
}
|
2013-07-22 22:47:42 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
|
|
|
}
|
|
|
|
semcount_t gfxSemCounter(gfxSem *pSem) {
|
|
|
|
semcount_t res;
|
2013-07-22 22:47:42 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
// The locking is really only required if obtaining the count is a divisible operation
|
|
|
|
// which it might be on a 8/16 bit processor with a 32 bit semaphore count.
|
|
|
|
pthread_mutex_lock(&pSem->mtx);
|
|
|
|
res = pSem->cnt;
|
|
|
|
pthread_mutex_unlock(&pSem->mtx);
|
2013-12-07 21:01:57 +00:00
|
|
|
|
2014-09-29 05:45:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif // GFX_USE_POSIX_SEMAPHORES
|
2013-07-22 22:47:42 +00:00
|
|
|
|
2013-07-23 00:12:52 +00:00
|
|
|
#endif /* GFX_USE_OS_LINUX */
|