aboutsummaryrefslogtreecommitdiffstats
path: root/src/gos/gos_linux.h
blob: 583c0c0fe5ca3af58dabbd1e48e0a2a467db803c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * 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.io/license.html
 */

#ifndef _GOS_LINUX_H
#define _GOS_LINUX_H

#if GFX_USE_OS_LINUX

// We don't put this in the general sys_options.h as it is Linux specific.
#ifndef GFX_USE_POSIX_SEMAPHORES
	#define GFX_USE_POSIX_SEMAPHORES	GFXON
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>

#if GFX_USE_POSIX_SEMAPHORES
	#include <semaphore.h>
#endif

typedef unsigned long		gTicks;
typedef void *				gThreadreturn;
typedef unsigned long		gDelay;
typedef pthread_t 			gThread;
typedef int					gThreadpriority;
typedef uint32_t			gSemcount;
typedef pthread_mutex_t		gfxMutex;

#define DECLARE_THREAD_FUNCTION(fnName, param)	gThreadreturn fnName(void *param)
#define DECLARE_THREAD_STACK(name, sz)			uint8_t name[0];
#define THREAD_RETURN(retval)					return retval

#define gfxExit()						exit(0)
#define gfxAlloc(sz)					malloc(sz)
#define gfxRealloc(p,osz,nsz)			realloc(p, nsz)
#define gfxFree(ptr)					free(ptr)
#define gfxMillisecondsToTicks(ms)		(ms)
#define gfxThreadMe()					pthread_self()
#define gfxThreadClose(th)				(void)th
#define gfxMutexInit(pmtx)				pthread_mutex_init(pmtx, 0)
#define gfxMutexDestroy(pmtx)			pthread_mutex_destroy(pmtx)
#define gfxMutexEnter(pmtx)				pthread_mutex_lock(pmtx)
#define gfxMutexExit(pmtx)				pthread_mutex_unlock(pmtx)
#define gfxSemWaitI(psem)				gfxSemWait(psem, gDelayNone)
#define gfxSemSignalI(psem)				gfxSemSignal(psem)

#define gDelayNone					0
#define gDelayForever				((gDelay)-1)
#define MAX_SEMAPHORE_COUNT			((gSemcount)-1)
#define gThreadpriorityLow				10
#define gThreadpriorityNormal				0
#define gThreadpriorityHigh				-10

#if GFX_USE_POSIX_SEMAPHORES
	typedef struct gfxSem {
		sem_t			sem;
		gSemcount		max;
	} gfxSem;
#else
	typedef struct gfxSem {
		pthread_mutex_t	mtx;
		pthread_cond_t	cond;
		gSemcount		cnt;
		gSemcount		max;
	} gfxSem;
#endif

/*===========================================================================*/
/* Function declarations.                                                    */
/*===========================================================================*/

void gfxYield(void);
void gfxHalt(const char *msg);
void gfxSleepMilliseconds(gDelay ms);
void gfxSleepMicroseconds(gDelay ms);
gTicks gfxSystemTicks(void);
void gfxSystemLock(void);
void gfxSystemUnlock(void);
void gfxSemInit(gfxSem *psem, gSemcount val, gSemcount limit);
void gfxSemDestroy(gfxSem *psem);
gBool gfxSemWait(gfxSem *psem, gDelay ms);
void gfxSemSignal(gfxSem *psem);
gThread gfxThreadCreate(void *stackarea, size_t stacksz, gThreadpriority prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param);
gThreadreturn gfxThreadWait(gThread thread);

#endif /* GFX_USE_OS_LINUX */

#endif /* _GOS_LINUX_H */