ugfx/src/gos/gos_osx.c

184 lines
4.3 KiB
C
Raw Normal View History

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:
*
2018-10-01 15:32:39 +00:00
* http://ugfx.io/license.html
2013-07-22 22:47:42 +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
#include "../../gfx.h"
2013-07-22 22:47:42 +00:00
#if GFX_USE_OS_OSX
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sched.h>
#include <mach/clock.h>
#include <mach/mach.h>
static gMutex SystemMutex;
2013-07-22 22:47:42 +00:00
void _gosInit(void)
{
/* No initialization of the operating system itself is needed */
2013-07-22 22:47:42 +00:00
gfxMutexInit(&SystemMutex);
}
void _gosPostInit(void)
{
}
void _gosDeinit(void)
{
/* ToDo */
}
2013-07-22 22:47:42 +00:00
void gfxSystemLock(void) {
gfxMutexEnter(&SystemMutex);
}
void gfxSystemUnlock(void) {
gfxMutexExit(&SystemMutex);
}
void gfxHalt(const char *msg) {
if (msg)
fprintf(stderr, "%s\n", msg);
exit(1);
}
void gfxSleepMilliseconds(gDelay ms) {
2013-07-22 22:47:42 +00:00
struct timespec ts;
switch(ms) {
case gDelayNone: gfxYield(); return;
case gDelayForever: while(1) sleep(60); return;
2013-07-22 22:47:42 +00:00
default:
ts.tv_sec = ms / 1000;
2014-09-29 05:46:08 +00:00
ts.tv_nsec = (ms % 1000) * 1000000;
2013-07-22 22:47:42 +00:00
nanosleep(&ts, 0);
return;
}
}
void gfxSleepMicroseconds(gDelay us) {
2013-07-22 22:47:42 +00:00
struct timespec ts;
2014-09-29 05:46:08 +00:00
switch(us) {
case gDelayNone: gfxYield(); return;
case gDelayForever: while(1) sleep(60); return;
2013-07-22 22:47:42 +00:00
default:
2014-09-29 05:46:08 +00:00
ts.tv_sec = us / 1000000;
ts.tv_nsec = (us % 1000000) * 1000;
2013-07-22 22:47:42 +00:00
nanosleep(&ts, 0);
return;
}
}
gTicks gfxSystemTicks(void) {
2014-09-29 05:46:08 +00:00
mach_timespec_t ts;
clock_serv_t cclock;
2014-09-29 05:46:08 +00:00
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &ts);
mach_port_deallocate(mach_task_self(), cclock);
2014-09-29 05:46:08 +00:00
return ts.tv_sec * 1000UL + ts.tv_nsec / 1000000;
2013-07-22 22:47:42 +00:00
}
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param) {
gThread th;
(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.
// 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.
if (pthread_create(&th, 0, fn, param))
return 0;
return th;
}
gThreadreturn gfxThreadWait(gThread thread) {
gThreadreturn retval;
2013-07-22 22:47:42 +00:00
if (pthread_join(thread, &retval))
return 0;
return retval;
}
void gfxSemInit(gSem *pSem, gSemcount val, gSemcount limit) {
2013-07-22 22:47:42 +00:00
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(gSem *pSem) {
2013-07-22 22:47:42 +00:00
pthread_mutex_destroy(&pSem->mtx);
pthread_cond_destroy(&pSem->cond);
}
gBool gfxSemWait(gSem *pSem, gDelay ms) {
2013-07-22 22:47:42 +00:00
pthread_mutex_lock(&pSem->mtx);
switch (ms) {
case gDelayForever:
2013-07-22 22:47:42 +00:00
while (!pSem->cnt)
pthread_cond_wait(&pSem->cond, &pSem->mtx);
break;
case gDelayNone:
2013-07-22 22:47:42 +00:00
if (!pSem->cnt) {
pthread_mutex_unlock(&pSem->mtx);
return gFalse;
2013-07-22 22:47:42 +00:00
}
break;
default:
{
struct timeval now;
struct timespec tm;
gettimeofday(&now, 0);
2013-07-22 22:47:42 +00:00
tm.tv_sec = now.tv_sec + ms / 1000;
2014-09-29 05:46:08 +00:00
tm.tv_nsec = now.tv_usec * 1000 + (ms % 1000) * 1000000;
2013-07-22 22:47:42 +00:00
while (!pSem->cnt) {
2014-09-29 05:46:08 +00:00
// 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 gFalse on any error
2014-09-29 05:46:08 +00:00
// including a ETIMEDOUT.
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
2013-07-22 22:47:42 +00:00
pthread_mutex_unlock(&pSem->mtx);
return gFalse;
2013-07-22 22:47:42 +00:00
}
}
}
break;
}
pSem->cnt--;
pthread_mutex_unlock(&pSem->mtx);
return gTrue;
2013-07-22 22:47:42 +00:00
}
void gfxSemSignal(gSem *pSem) {
2013-07-22 22:47:42 +00:00
pthread_mutex_lock(&pSem->mtx);
if (pSem->cnt < pSem->max) {
pSem->cnt++;
pthread_cond_signal(&pSem->cond);
}
pthread_mutex_unlock(&pSem->mtx);
}
#endif /* GFX_USE_OS_OSX */