µGFX  2.9
version 2.9
gos_x_threads.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.io/license.html
6  */
7 
8 /**
9  * This threading implementation supports most 32 bit processors with or without an
10  * underlying operating system. It uses cooperative multi-tasking. Be careful
11  * when writing device drivers not to disturb the assumptions this creates by performing
12  * call-backs from interrupt handlers to uGFX code unless you define the INTERRUPTS_OFF()
13  * and INTERRUPTS_ON() macros.
14  * It still requires some C runtime library support for the setjmp implementation...
15  * setjmp() and longjmp() - for threading
16  * memcpy() - for heap and threading
17  *
18  * You must also define the following routines in your own code so that timing functions will work...
19  * gTicks gfxSystemTicks(void);
20  * gTicks gfxMillisecondsToTicks(gDelay ms);
21  */
22 #ifndef _GOS_X_THREADS_H
23 #define _GOS_X_THREADS_H
24 
25 #if GOS_NEED_X_THREADS
26 
27 typedef gU32 gDelay;
28 typedef gU32 gTicks;
29 typedef short gSemcount;
30 typedef int gThreadreturn;
31 typedef int gThreadpriority;
32 
33 #define GFX_THREAD_FUNCTION(fnName, param) gThreadreturn fnName(void *param)
34 #define GFX_THREAD_STACK(name, sz) gU8 name[(sz) & ~3];
35 #define gfxThreadReturn(retval) return retval
36 
37 #define gDelayNone 0
38 #define gDelayForever ((gDelay)-1)
39 #define gSemMaxCount 0x7FFF
40 #define gThreadpriorityLow 0
41 #define gThreadpriorityNormal 1
42 #define gThreadpriorityHigh 2
43 
44 typedef struct {
45  gSemcount cnt;
46  gSemcount limit;
47 } gSem;
48 
49 typedef gU32 gMutex;
50 typedef void * gThread;
51 
52 // Required timing functions - supplied by the user or the operating system
53 gTicks gfxSystemTicks(void);
54 gTicks gfxMillisecondsToTicks(gDelay ms);
55 
56 // Sleep Functions
57 void gfxSleepMilliseconds(gDelay ms);
58 void gfxSleepMicroseconds(gDelay ms);
59 void gfxYield(void);
60 
61 // System Locking
62 void gfxSystemLock(void);
63 void gfxSystemUnlock(void);
64 
65 // Mutexes
66 void gfxMutexInit(gMutex *pmutex);
67 #define gfxMutexDestroy(pmutex)
68 void gfxMutexEnter(gMutex *pmutex);
69 void gfxMutexExit(gMutex *pmutex);
70 
71 // Semaphores
72 void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit);
73 #define gfxSemDestroy(psem)
74 gBool gfxSemWait(gSem *psem, gDelay ms);
75 gBool gfxSemWaitI(gSem *psem);
76 void gfxSemSignal(gSem *psem);
77 void gfxSemSignalI(gSem *psem);
78 
79 // Threads
80 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param);
81 #define gfxThreadClose(thread)
82 gThreadreturn gfxThreadWait(gThread thread);
83 gThread gfxThreadMe(void);
84 
85 /** The following is not part of the public ugfx API as some operating systems
86  * simply do not provide this capability.
87  * For RAW32 we need it anyway so we might as well declare it here.
88  */
89 void gfxThreadExit(gThreadreturn ret);
90 
91 #endif /* GOS_NEED_X_THREADS */
92 #endif /* _GOS_X_THREADS_H */
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
Definition: gos.h:116
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.
Definition: gos.h:73
void gfxMutexExit(gMutex *pmutex)
Exit the critical code region protected by the mutex.
gTicks gfxMillisecondsToTicks(gDelay ms)
Convert a given number of millseconds to a number of operating system ticks.
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.
gThread gfxThreadMe(void)
Get the current thread handle.
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.
gBool gfxSemWaitI(gSem *psem)
Test if a wait on a semaphore can be satisfied immediately.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
void gfxSleepMilliseconds(gDelay ms)
Put the current thread to sleep for the specified period in milliseconds.
A mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104