version 2.8
gos_linux.h
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.org/license.html
6  */
7 
8 #ifndef _GOS_LINUX_H
9 #define _GOS_LINUX_H
10 
11 #if GFX_USE_OS_LINUX
12 
13 // We don't put this in the general sys_options.h as it is Linux specific.
14 #ifndef GFX_USE_POSIX_SEMAPHORES
15  #define GFX_USE_POSIX_SEMAPHORES TRUE
16 #endif
17 
18 #include <sys/types.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <pthread.h>
22 
23 #if GFX_USE_POSIX_SEMAPHORES
24  #include <semaphore.h>
25 #endif
26 
27 /* Already defined int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, size_t */
28 
29 typedef int8_t bool_t;
30 typedef unsigned long systemticks_t;
31 typedef void * threadreturn_t;
32 typedef unsigned long delaytime_t;
33 typedef pthread_t gfxThreadHandle;
34 typedef int threadpriority_t;
35 typedef uint32_t semcount_t;
36 typedef pthread_mutex_t gfxMutex;
37 
38 #define DECLARE_THREAD_FUNCTION(fnName, param) threadreturn_t fnName(void *param)
39 #define DECLARE_THREAD_STACK(name, sz) uint8_t name[0];
40 #define THREAD_RETURN(retval) return retval
41 
42 #define gfxExit() exit(0)
43 #define gfxAlloc(sz) malloc(sz)
44 #define gfxRealloc(p,osz,nsz) realloc(p, nsz)
45 #define gfxFree(ptr) free(ptr)
46 #define gfxMillisecondsToTicks(ms) (ms)
47 #define gfxThreadMe() pthread_self()
48 #define gfxThreadClose(th) (void)th
49 #define gfxMutexInit(pmtx) pthread_mutex_init(pmtx, 0)
50 #define gfxMutexDestroy(pmtx) pthread_mutex_destroy(pmtx)
51 #define gfxMutexEnter(pmtx) pthread_mutex_lock(pmtx)
52 #define gfxMutexExit(pmtx) pthread_mutex_unlock(pmtx)
53 #define gfxSemWaitI(psem) gfxSemWait(psem, TIME_IMMEDIATE)
54 #define gfxSemSignalI(psem) gfxSemSignal(psem)
55 
56 #define TIME_IMMEDIATE 0
57 #define TIME_INFINITE ((delaytime_t)-1)
58 #define MAX_SEMAPHORE_COUNT ((semcount_t)-1)
59 #define LOW_PRIORITY 10
60 #define NORMAL_PRIORITY 0
61 #define HIGH_PRIORITY -10
62 
63 #if GFX_USE_POSIX_SEMAPHORES
64  typedef struct gfxSem {
65  sem_t sem;
66  semcount_t max;
67  } gfxSem;
68 #else
69  typedef struct gfxSem {
70  pthread_mutex_t mtx;
71  pthread_cond_t cond;
72  semcount_t cnt;
73  semcount_t max;
74  } gfxSem;
75 #endif
76 
77 /*===========================================================================*/
78 /* Function declarations. */
79 /*===========================================================================*/
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 
85 void gfxYield(void);
86 void gfxHalt(const char *msg);
87 void gfxSleepMilliseconds(delaytime_t ms);
88 void gfxSleepMicroseconds(delaytime_t ms);
89 systemticks_t gfxSystemTicks(void);
90 void gfxSystemLock(void);
91 void gfxSystemUnlock(void);
92 void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit);
93 void gfxSemDestroy(gfxSem *psem);
94 bool_t gfxSemWait(gfxSem *psem, delaytime_t ms);
95 void gfxSemSignal(gfxSem *psem);
96 gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param);
97 threadreturn_t gfxThreadWait(gfxThreadHandle thread);
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif /* GFX_USE_OS_LINUX */
104 
105 #endif /* _GOS_LINUX_H */
void * gfxThreadHandle
A thread handle.
Definition: gos.h:117
void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit)
Initialise a Counted Semaphore.
systemticks_t gfxSystemTicks(void)
Get the current operating system tick time.
void gfxSemDestroy(gfxSem *psem)
Destroy a Counted Semaphore.
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
Wait on a semaphore.
A semaphore.
Definition: gos.h:105
void gfxSystemLock(void)
Lock the operating system to protect a sequence of code.
#define DECLARE_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:62
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
void gfxSemSignal(gfxSem *psem)
Signal a semaphore.
void gfxSleepMicroseconds(delaytime_t us)
Put the current thread to sleep for the specified period in microseconds.
gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
A mutex.
Definition: gos.h:111
threadreturn_t gfxThreadWait(gfxThreadHandle thread)
Wait for a thread to finish.
void gfxYield(void)
Yield the current thread.
void gfxSleepMilliseconds(delaytime_t ms)
Put the current thread to sleep for the specified period in milliseconds.
void gfxSystemUnlock(void)
Unlock the operating system previous locked by gfxSystemLock()