Fix typo.
[Rockbox.git] / firmware / export / thread.h
blobe16baa225637f347b5f7b66bff35d54988e43afd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Ulf Ralberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #ifndef THREAD_H
20 #define THREAD_H
22 #include "config.h"
23 #include <inttypes.h>
24 #include <stdbool.h>
26 /* Priority scheduling (when enabled with HAVE_PRIORITY_SCHEDULING) works
27 * by giving high priority threads more CPU time than less priority threads
28 * when they need it.
30 * If software playback codec pcm buffer is going down to critical, codec
31 * can change it own priority to REALTIME to override user interface and
32 * prevent playback skipping.
34 #define PRIORITY_REALTIME 1
35 #define PRIORITY_USER_INTERFACE 4 /* The main thread */
36 #define PRIORITY_RECORDING 4 /* Recording thread */
37 #define PRIORITY_PLAYBACK 4 /* or REALTIME when needed */
38 #define PRIORITY_BUFFERING 4 /* Codec buffering thread */
39 #define PRIORITY_SYSTEM 6 /* All other firmware threads */
40 #define PRIORITY_BACKGROUND 8 /* Normal application threads */
42 #if CONFIG_CODEC == SWCODEC
43 #define MAXTHREADS 16
44 #else
45 #define MAXTHREADS 11
46 #endif
48 #define DEFAULT_STACK_SIZE 0x400 /* Bytes */
50 #ifndef SIMULATOR
51 /* Need to keep structures inside the header file because debug_menu
52 * needs them. */
53 #ifdef CPU_COLDFIRE
54 struct regs
56 unsigned int macsr; /* 0 - EMAC status register */
57 unsigned int d[6]; /* 4-24 - d2-d7 */
58 unsigned int a[5]; /* 28-44 - a2-a6 */
59 void *sp; /* 48 - Stack pointer (a7) */
60 void *start; /* 52 - Thread start address, or NULL when started */
61 } __attribute__((packed));
62 #elif CONFIG_CPU == SH7034
63 struct regs
65 unsigned int r[7]; /* 0-24 - Registers r8 thru r14 */
66 void *sp; /* 28 - Stack pointer (r15) */
67 void *pr; /* 32 - Procedure register */
68 void *start; /* 36 - Thread start address, or NULL when started */
69 } __attribute__((packed));
70 #elif defined(CPU_ARM)
71 struct regs
73 unsigned int r[8]; /* 0-28 - Registers r4-r11 */
74 void *sp; /* 32 - Stack pointer (r13) */
75 unsigned int lr; /* 36 - r14 (lr) */
76 void *start; /* 40 - Thread start address, or NULL when started */
77 } __attribute__((packed));
78 #endif /* CONFIG_CPU */
79 #else
80 struct regs
82 void *t; /* Simulator OS thread */
83 void *c; /* Condition for blocking and sync */
84 void (*start)(void); /* Start function */
86 #endif /* !SIMULATOR */
88 #define STATE_RUNNING 0x00000000
89 #define STATE_BLOCKED 0x20000000
90 #define STATE_SLEEPING 0x40000000
91 #define STATE_BLOCKED_W_TMO 0x60000000
93 #define THREAD_STATE_MASK 0x60000000
94 #define STATE_ARG_MASK 0x1FFFFFFF
96 #define GET_STATE_ARG(state) (state & STATE_ARG_MASK)
97 #define GET_STATE(state) (state & THREAD_STATE_MASK)
98 #define SET_STATE(var,state,arg) (var = (state | ((arg) & STATE_ARG_MASK)))
99 #define CLEAR_STATE_ARG(var) (var &= ~STATE_ARG_MASK)
101 #define STATE_BOOSTED 0x80000000
102 #define STATE_IS_BOOSTED(var) (var & STATE_BOOSTED)
103 #define SET_BOOST_STATE(var) (var |= STATE_BOOSTED)
105 struct thread_entry {
106 struct regs context;
107 const char *name;
108 void *stack;
109 unsigned long statearg;
110 unsigned short stack_size;
111 # if NUM_CORES > 1
112 unsigned char core; /* To which core threads belongs to. */
113 # endif
114 #ifdef HAVE_PRIORITY_SCHEDULING
115 unsigned char priority;
116 unsigned char priority_x;
117 long last_run;
118 #endif
119 struct thread_entry *next, *prev;
120 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
121 intptr_t retval;
122 #endif
125 struct core_entry {
126 struct thread_entry *running;
127 struct thread_entry *sleeping;
128 struct thread_entry *waking;
129 struct thread_entry **wakeup_list;
130 #ifdef HAVE_PRIORITY_SCHEDULING
131 long highest_priority;
132 #endif
133 #if NUM_CORES > 1
134 volatile bool lock_issued;
135 volatile bool kernel_running;
136 #endif
137 long last_tick;
138 int switch_to_irq_level;
139 #define STAY_IRQ_LEVEL -1
142 #ifdef HAVE_PRIORITY_SCHEDULING
143 #define IF_PRIO(...) __VA_ARGS__
144 #else
145 #define IF_PRIO(...)
146 #endif
148 /* PortalPlayer chips have 2 cores, therefore need atomic mutexes
149 * Just use it for ARM, Coldfire and whatever else well...why not?
152 /* Macros generate better code than an inline function is this case */
153 #if defined (CPU_PP) || defined (CPU_ARM)
154 #define test_and_set(x_, v_) \
155 ({ \
156 uint32_t old; \
157 asm volatile ( \
158 "swpb %[old], %[v], [%[x]] \r\n" \
159 : [old]"=r"(old) \
160 : [v]"r"((uint32_t)v_), [x]"r"((uint32_t *)x_) \
161 ); \
162 old; \
164 #elif defined (CPU_COLDFIRE)
165 #define test_and_set(x_, v_) \
166 ({ \
167 uint8_t old; \
168 asm volatile ( \
169 "bset.l %[v], (%[x]) \r\n" \
170 "sne.b %[old] \r\n" \
171 : [old]"=d,d"(old) \
172 : [v]"i,d"((uint32_t)v_), [x]"a,a"((uint32_t *)x_) \
173 ); \
174 old; \
176 #elif CONFIG_CPU == SH7034
177 #define test_and_set(x_, v_) \
178 ({ \
179 uint32_t old; \
180 asm volatile ( \
181 "tas.b @%[x] \r\n" \
182 "mov #-1, %[old] \r\n" \
183 "negc %[old], %[old] \r\n" \
184 : [old]"=r"(old) \
185 : [v]"M"((uint32_t)v_), /* Value of v_ must be 1 */ \
186 [x]"r"((uint8_t *)x_) \
187 ); \
188 old; \
190 #else
191 /* default for no asm version */
192 #define test_and_set(x_, v_) \
193 ({ \
194 uint32_t old = *(uint32_t *)x_; \
195 *(uint32_t *)x_ = v_; \
196 old; \
198 #endif
200 struct thread_entry*
201 create_thread(void (*function)(void), void* stack, int stack_size,
202 const char *name IF_PRIO(, int priority)
203 IF_COP(, unsigned int core, bool fallback));
205 #ifdef HAVE_SCHEDULER_BOOSTCTRL
206 void trigger_cpu_boost(void);
207 #else
208 #define trigger_cpu_boost()
209 #endif
211 void remove_thread(struct thread_entry *thread);
212 void switch_thread(bool save_context, struct thread_entry **blocked_list);
213 void sleep_thread(int ticks);
214 void block_thread(struct thread_entry **thread);
215 void block_thread_w_tmo(struct thread_entry **thread, int timeout);
216 void set_irq_level_and_block_thread(struct thread_entry **thread, int level);
217 void set_irq_level_and_block_thread_w_tmo(struct thread_entry **list,
218 int timeout, int level);
219 void wakeup_thread(struct thread_entry **thread);
220 void wakeup_thread_irq_safe(struct thread_entry **thread);
221 #ifdef HAVE_PRIORITY_SCHEDULING
222 int thread_set_priority(struct thread_entry *thread, int priority);
223 int thread_get_priority(struct thread_entry *thread);
224 /* Yield that guarantees thread execution once per round regardless of
225 thread's scheduler priority - basically a transient realtime boost
226 without altering the scheduler's thread precedence. */
227 void priority_yield(void);
228 #else
229 #define priority_yield yield
230 #endif /* HAVE_PRIORITY_SCHEDULING */
231 struct thread_entry * thread_get_current(void);
232 void init_threads(void);
233 int thread_stack_usage(const struct thread_entry *thread);
234 #if NUM_CORES > 1
235 int idle_stack_usage(unsigned int core);
236 #endif
237 int thread_get_status(const struct thread_entry *thread);
238 void thread_get_name(char *buffer, int size,
239 struct thread_entry *thread);
240 #ifdef RB_PROFILE
241 void profile_thread(void);
242 #endif
244 #endif