version 2.8
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.org/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  * systemticks_t gfxSystemTicks(void);
20  * systemticks_t gfxMillisecondsToTicks(delaytime_t ms);
21  */
22 #ifndef _GOS_X_THREADS_H
23 #define _GOS_X_THREADS_H
24 
25 #if GOS_NEED_X_THREADS
26 
27 typedef uint32_t delaytime_t;
28 typedef uint32_t systemticks_t;
29 typedef short semcount_t;
30 typedef int threadreturn_t;
31 typedef int threadpriority_t;
32 
33 #define DECLARE_THREAD_FUNCTION(fnName, param) threadreturn_t fnName(void *param)
34 #define DECLARE_THREAD_STACK(name, sz) uint8_t name[(sz) & ~3];
35 #define THREAD_RETURN(retval) return retval
36 
37 #define TIME_IMMEDIATE 0
38 #define TIME_INFINITE ((delaytime_t)-1)
39 #define MAX_SEMAPHORE_COUNT 0x7FFF
40 #define LOW_PRIORITY 0
41 #define NORMAL_PRIORITY 1
42 #define HIGH_PRIORITY 2
43 
44 typedef struct {
45  semcount_t cnt;
46  semcount_t limit;
47 } gfxSem;
48 
49 typedef uint32_t gfxMutex;
50 typedef void * gfxThreadHandle;
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56  // Required timing functions - supplied by the user or the operating system
57  systemticks_t gfxSystemTicks(void);
58  systemticks_t gfxMillisecondsToTicks(delaytime_t ms);
59 
60  // Sleep Functions
61  void gfxSleepMilliseconds(delaytime_t ms);
62  void gfxSleepMicroseconds(delaytime_t ms);
63  void gfxYield(void);
64 
65  // System Locking
66  void gfxSystemLock(void);
67  void gfxSystemUnlock(void);
68 
69  // Mutexes
70  void gfxMutexInit(gfxMutex *pmutex);
71  #define gfxMutexDestroy(pmutex)
72  void gfxMutexEnter(gfxMutex *pmutex);
73  void gfxMutexExit(gfxMutex *pmutex);
74 
75  // Semaphores
76  void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit);
77  #define gfxSemDestroy(psem)
78  bool_t gfxSemWait(gfxSem *psem, delaytime_t ms);
79  bool_t gfxSemWaitI(gfxSem *psem);
80  void gfxSemSignal(gfxSem *psem);
81  void gfxSemSignalI(gfxSem *psem);
82 
83  // Threads
84  gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param);
85  #define gfxThreadClose(thread)
86  threadreturn_t gfxThreadWait(gfxThreadHandle thread);
88 
89  /** The following is not part of the public ugfx API as some operating systems
90  * simply do not provide this capability.
91  * For RAW32 we need it anyway so we might as well declare it here.
92  */
93  void gfxThreadExit(threadreturn_t ret);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #endif /* GOS_NEED_X_THREADS */
100 #endif /* _GOS_X_THREADS_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.
systemticks_t gfxMillisecondsToTicks(delaytime_t ms)
Convert a given number of millseconds to a number of operating system ticks.
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
Wait on a semaphore.
void gfxSemSignalI(gfxSem *psem)
Signal a semaphore.
A semaphore.
Definition: gos.h:105
void gfxMutexExit(gfxMutex *pmutex)
Exit the critical code region protected by the mutex.
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 gfxMutexEnter(gfxMutex *pmutex)
Enter the critical code region protected by the mutex.
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 gfxMutexInit(gfxMutex *pmutex)
Initialise a mutex to protect a region of code from other threads.
void gfxYield(void)
Yield the current thread.
gfxThreadHandle gfxThreadMe(void)
Get the current thread handle.
void gfxSleepMilliseconds(delaytime_t ms)
Put the current thread to sleep for the specified period in milliseconds.
bool_t gfxSemWaitI(gfxSem *psem)
Test if a wait on a semaphore can be satisfied immediately.
void gfxSystemUnlock(void)
Unlock the operating system previous locked by gfxSystemLock()