Make the statusbar setting use a temp variable so it doesnt look wierd when changing it
[Rockbox.git] / firmware / export / thread.h
blob81c7c2374f2f9e7f37e6a142cd0121b58318d9e7
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; /* EMAC status register */
57 unsigned int d[6]; /* d2-d7 */
58 unsigned int a[5]; /* a2-a6 */
59 void *sp; /* Stack pointer (a7) */
60 void *start; /* Thread start address, or NULL when started */
62 # elif CONFIG_CPU == SH7034
63 struct regs
65 unsigned int r[7]; /* Registers r8 thru r14 */
66 void *sp; /* Stack pointer (r15) */
67 void *pr; /* Procedure register */
68 void *start; /* Thread start address, or NULL when started */
70 # elif defined(CPU_ARM)
71 struct regs
73 unsigned int r[8]; /* Registers r4-r11 */
74 void *sp; /* Stack pointer (r13) */
75 unsigned int lr; /* r14 (lr) */
76 void *start; /* Thread start address, or NULL when started */
78 # endif
80 #endif /* !SIMULATOR */
82 #define STATE_RUNNING 0x00000000
83 #define STATE_BLOCKED 0x20000000
84 #define STATE_SLEEPING 0x40000000
85 #define STATE_BLOCKED_W_TMO 0x60000000
87 #define THREAD_STATE_MASK 0x60000000
88 #define STATE_ARG_MASK 0x1FFFFFFF
90 #define GET_STATE_ARG(state) (state & STATE_ARG_MASK)
91 #define GET_STATE(state) (state & THREAD_STATE_MASK)
92 #define SET_STATE(var,state,arg) (var = (state | ((arg) & STATE_ARG_MASK)))
93 #define CLEAR_STATE_ARG(var) (var &= ~STATE_ARG_MASK)
95 #define STATE_BOOSTED 0x80000000
96 #define STATE_IS_BOOSTED(var) (var & STATE_BOOSTED)
97 #define SET_BOOST_STATE(var) (var |= STATE_BOOSTED)
99 struct thread_entry {
100 #ifndef SIMULATOR
101 struct regs context;
102 #endif
103 const char *name;
104 void *stack;
105 unsigned long statearg;
106 unsigned short stack_size;
107 # if NUM_CORES > 1
108 unsigned char core; /* To which core threads belongs to. */
109 # endif
110 #ifdef HAVE_PRIORITY_SCHEDULING
111 unsigned char priority;
112 unsigned char priority_x;
113 long last_run;
114 #endif
115 struct thread_entry *next, *prev;
116 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
117 intptr_t retval;
118 #endif
121 struct core_entry {
122 struct thread_entry *running;
123 struct thread_entry *sleeping;
124 struct thread_entry *waking;
125 struct thread_entry **wakeup_list;
126 #ifdef HAVE_PRIORITY_SCHEDULING
127 long highest_priority;
128 #endif
129 #if NUM_CORES > 1
130 volatile bool lock_issued;
131 volatile bool kernel_running;
132 #endif
133 long last_tick;
134 int switch_to_irq_level;
135 #define STAY_IRQ_LEVEL -1
138 #ifdef HAVE_PRIORITY_SCHEDULING
139 #define IF_PRIO(empty, type) , type
140 #else
141 #define IF_PRIO(empty, type)
142 #endif
144 /* PortalPlayer chips have 2 cores, therefore need atomic mutexes
145 * Just use it for ARM, Coldfire and whatever else well...why not?
148 /* Macros generate better code than an inline function is this case */
149 #if defined (CPU_PP) || defined (CPU_ARM)
150 #define test_and_set(x_, v_) \
151 ({ \
152 uint32_t old; \
153 asm volatile ( \
154 "swpb %[old], %[v], [%[x]] \r\n" \
155 : [old]"=r"(old) \
156 : [v]"r"((uint32_t)v_), [x]"r"((uint32_t *)x_) \
157 ); \
158 old; \
160 #elif defined (CPU_COLDFIRE)
161 #define test_and_set(x_, v_) \
162 ({ \
163 uint8_t old; \
164 asm volatile ( \
165 "bset.l %[v], (%[x]) \r\n" \
166 "sne.b %[old] \r\n" \
167 : [old]"=d,d"(old) \
168 : [v]"i,d"((uint32_t)v_), [x]"a,a"((uint32_t *)x_) \
169 ); \
170 old; \
172 #else
173 /* default for no asm version */
174 #define test_and_set(x_, v_) \
175 ({ \
176 uint32_t old = *(uint32_t *)x_; \
177 *(uint32_t *)x_ = v_; \
178 old; \
180 #endif
182 #if NUM_CORES > 1
183 inline void lock_cores(void);
184 inline void unlock_cores(void);
185 #else
186 #define lock_cores(...)
187 #define unlock_cores(...)
188 #endif
190 struct thread_entry*
191 create_thread(void (*function)(void), void* stack, int stack_size,
192 const char *name IF_PRIO(, int priority)
193 IF_COP(, unsigned int core, bool fallback));
195 #ifdef HAVE_SCHEDULER_BOOSTCTRL
196 void trigger_cpu_boost(void);
197 #else
198 #define trigger_cpu_boost()
199 #endif
201 void remove_thread(struct thread_entry *thread);
202 void switch_thread(bool save_context, struct thread_entry **blocked_list);
203 void sleep_thread(int ticks);
204 void block_thread(struct thread_entry **thread);
205 void block_thread_w_tmo(struct thread_entry **thread, int timeout);
206 void set_irq_level_and_block_thread(struct thread_entry **thread, int level);
207 void set_irq_level_and_block_thread_w_tmo(struct thread_entry **list,
208 int timeout, int level);
209 void wakeup_thread(struct thread_entry **thread);
210 void wakeup_thread_irq_safe(struct thread_entry **thread);
211 #ifdef HAVE_PRIORITY_SCHEDULING
212 int thread_set_priority(struct thread_entry *thread, int priority);
213 int thread_get_priority(struct thread_entry *thread);
214 /* Yield that guarantees thread execution once per round regardless of
215 thread's scheduler priority - basically a transient realtime boost
216 without altering the scheduler's thread precedence. */
217 void priority_yield(void);
218 #else
219 #define priority_yield yield
220 #endif /* HAVE_PRIORITY_SCHEDULING */
221 struct thread_entry * thread_get_current(void);
222 void init_threads(void);
223 int thread_stack_usage(const struct thread_entry *thread);
224 int thread_get_status(const struct thread_entry *thread);
225 #ifdef RB_PROFILE
226 void profile_thread(void);
227 #endif
229 #endif