Update the 'Supported audio formats' table, we now recognize ogg/vorbis files with...
[kugel-rb.git] / firmware / export / thread.h
blobc4dfbf4ed3efa6f9478474a13a406a4925eb234a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Ulf Ralberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifndef THREAD_H
22 #define THREAD_H
24 #include "config.h"
25 #include <inttypes.h>
26 #include <stddef.h>
27 #include <stdbool.h>
29 /* Priority scheduling (when enabled with HAVE_PRIORITY_SCHEDULING) works
30 * by giving high priority threads more CPU time than lower priority threads
31 * when they need it. Priority is differential such that the priority
32 * difference between a lower priority runnable thread and the highest priority
33 * runnable thread determines the amount of aging necessary for the lower
34 * priority thread to be scheduled in order to prevent starvation.
36 * If software playback codec pcm buffer is going down to critical, codec
37 * can gradually raise its own priority to override user interface and
38 * prevent playback skipping.
40 #define PRIORITY_RESERVED_HIGH 0 /* Reserved */
41 #define PRIORITY_RESERVED_LOW 32 /* Reserved */
42 #define HIGHEST_PRIORITY 1 /* The highest possible thread priority */
43 #define LOWEST_PRIORITY 31 /* The lowest possible thread priority */
44 /* Realtime range reserved for threads that will not allow threads of lower
45 * priority to age and run (future expansion) */
46 #define PRIORITY_REALTIME_1 1
47 #define PRIORITY_REALTIME_2 2
48 #define PRIORITY_REALTIME_3 3
49 #define PRIORITY_REALTIME_4 4
50 #define PRIORITY_REALTIME 4 /* Lowest realtime range */
51 #define PRIORITY_BUFFERING 15 /* Codec buffering thread */
52 #define PRIORITY_USER_INTERFACE 16 /* The main thread */
53 #define PRIORITY_RECORDING 16 /* Recording thread */
54 #define PRIORITY_PLAYBACK 16 /* Variable between this and MAX */
55 #define PRIORITY_PLAYBACK_MAX 5 /* Maximum allowable playback priority */
56 #define PRIORITY_SYSTEM 18 /* All other firmware threads */
57 #define PRIORITY_BACKGROUND 20 /* Normal application threads */
58 #define NUM_PRIORITIES 32
59 #define PRIORITY_IDLE 32 /* Priority representative of no tasks */
61 /* TODO: Only a minor tweak to create_thread would be needed to let
62 * thread slots be caller allocated - no essential threading functionality
63 * depends upon an array */
64 #if CONFIG_CODEC == SWCODEC
66 #ifdef HAVE_RECORDING
67 #define BASETHREADS 18
68 #else
69 #define BASETHREADS 17
70 #endif
72 #else
73 #define BASETHREADS 11
74 #endif /* CONFIG_CODE == * */
76 #ifndef TARGET_EXTRA_THREADS
77 #define TARGET_EXTRA_THREADS 0
78 #endif
80 #define MAXTHREADS (BASETHREADS+TARGET_EXTRA_THREADS)
82 #define DEFAULT_STACK_SIZE 0x400 /* Bytes */
84 #ifndef SIMULATOR
85 /* Need to keep structures inside the header file because debug_menu
86 * needs them. */
87 #ifdef CPU_COLDFIRE
88 struct regs
90 uint32_t macsr; /* 0 - EMAC status register */
91 uint32_t d[6]; /* 4-24 - d2-d7 */
92 uint32_t a[5]; /* 28-44 - a2-a6 */
93 uint32_t sp; /* 48 - Stack pointer (a7) */
94 uint32_t start; /* 52 - Thread start address, or NULL when started */
96 #elif CONFIG_CPU == SH7034
97 struct regs
99 uint32_t r[7]; /* 0-24 - Registers r8 thru r14 */
100 uint32_t sp; /* 28 - Stack pointer (r15) */
101 uint32_t pr; /* 32 - Procedure register */
102 uint32_t start; /* 36 - Thread start address, or NULL when started */
104 #elif defined(CPU_ARM)
105 struct regs
107 uint32_t r[8]; /* 0-28 - Registers r4-r11 */
108 uint32_t sp; /* 32 - Stack pointer (r13) */
109 uint32_t lr; /* 36 - r14 (lr) */
110 uint32_t start; /* 40 - Thread start address, or NULL when started */
112 #elif defined(CPU_MIPS)
113 struct regs
115 uint32_t r[10]; /* 0-36 - Registers s0-s7, gp, fp */
116 uint32_t sp; /* 40 - Stack pointer */
117 uint32_t ra; /* 44 - Return address */
118 uint32_t start; /* 48 - Thread start address, or NULL when started */
120 #endif /* CONFIG_CPU */
121 #else
122 struct regs
124 void *t; /* Simulator OS thread */
125 void *s; /* Semaphore for blocking and wakeup */
126 void (*start)(void); /* Start function */
128 #endif /* !SIMULATOR */
130 /* NOTE: The use of the word "queue" may also refer to a linked list of
131 threads being maintained that are normally dealt with in FIFO order
132 and not necessarily kernel event_queue */
133 enum
135 /* States without a timeout must be first */
136 STATE_KILLED = 0, /* Thread is killed (default) */
137 STATE_RUNNING, /* Thread is currently running */
138 STATE_BLOCKED, /* Thread is indefinitely blocked on a queue */
139 /* These states involve adding the thread to the tmo list */
140 STATE_SLEEPING, /* Thread is sleeping with a timeout */
141 STATE_BLOCKED_W_TMO, /* Thread is blocked on a queue with a timeout */
142 /* Miscellaneous states */
143 STATE_FROZEN, /* Thread is suspended and will not run until
144 thread_thaw is called with its ID */
145 THREAD_NUM_STATES,
146 TIMEOUT_STATE_FIRST = STATE_SLEEPING,
149 #if NUM_CORES > 1
150 /* Pointer value for name field to indicate thread is being killed. Using
151 * an alternate STATE_* won't work since that would interfere with operation
152 * while the thread is still running. */
153 #define THREAD_DESTRUCT ((const char *)~(intptr_t)0)
154 #endif
156 /* Link information for lists thread is in */
157 struct thread_entry; /* forward */
158 struct thread_list
160 struct thread_entry *prev; /* Previous thread in a list */
161 struct thread_entry *next; /* Next thread in a list */
164 /* Small objects for core-wise mutual exclusion */
165 #if CONFIG_CORELOCK == SW_CORELOCK
166 /* No reliable atomic instruction available - use Peterson's algorithm */
167 struct corelock
169 volatile unsigned char myl[NUM_CORES];
170 volatile unsigned char turn;
171 } __attribute__((packed));
173 void corelock_init(struct corelock *cl);
174 void corelock_lock(struct corelock *cl);
175 int corelock_try_lock(struct corelock *cl);
176 void corelock_unlock(struct corelock *cl);
177 #elif CONFIG_CORELOCK == CORELOCK_SWAP
178 /* Use native atomic swap/exchange instruction */
179 struct corelock
181 volatile unsigned char locked;
182 } __attribute__((packed));
184 #define corelock_init(cl) \
185 ({ (cl)->locked = 0; })
186 #define corelock_lock(cl) \
187 ({ while (test_and_set(&(cl)->locked, 1)); })
188 #define corelock_try_lock(cl) \
189 ({ test_and_set(&(cl)->locked, 1) ? 0 : 1; })
190 #define corelock_unlock(cl) \
191 ({ (cl)->locked = 0; })
192 #else
193 /* No atomic corelock op needed or just none defined */
194 #define corelock_init(cl)
195 #define corelock_lock(cl)
196 #define corelock_try_lock(cl)
197 #define corelock_unlock(cl)
198 #endif /* core locking selection */
200 #ifdef HAVE_PRIORITY_SCHEDULING
201 struct blocker
203 struct thread_entry *thread; /* thread blocking other threads
204 (aka. object owner) */
205 int priority; /* highest priority waiter */
206 struct thread_entry * (*wakeup_protocol)(struct thread_entry *thread);
209 /* Choices of wakeup protocol */
211 /* For transfer of object ownership by one thread to another thread by
212 * the owning thread itself (mutexes) */
213 struct thread_entry *
214 wakeup_priority_protocol_transfer(struct thread_entry *thread);
216 /* For release by owner where ownership doesn't change - other threads,
217 * interrupts, timeouts, etc. (mutex timeout, queues) */
218 struct thread_entry *
219 wakeup_priority_protocol_release(struct thread_entry *thread);
222 struct priority_distribution
224 uint8_t hist[NUM_PRIORITIES]; /* Histogram: Frequency for each priority */
225 uint32_t mask; /* Bitmask of hist entries that are not zero */
228 #endif /* HAVE_PRIORITY_SCHEDULING */
230 /* Information kept in each thread slot
231 * members are arranged according to size - largest first - in order
232 * to ensure both alignment and packing at the same time.
234 struct thread_entry
236 struct regs context; /* Register context at switch -
237 _must_ be first member */
238 uintptr_t *stack; /* Pointer to top of stack */
239 const char *name; /* Thread name */
240 long tmo_tick; /* Tick when thread should be woken from
241 timeout -
242 states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
243 struct thread_list l; /* Links for blocked/waking/running -
244 circular linkage in both directions */
245 struct thread_list tmo; /* Links for timeout list -
246 Circular in reverse direction, NULL-terminated in
247 forward direction -
248 states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
249 struct thread_entry **bqp; /* Pointer to list variable in kernel
250 object where thread is blocked - used
251 for implicit unblock and explicit wake
252 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
253 #if NUM_CORES > 1
254 struct corelock *obj_cl; /* Object corelock where thead is blocked -
255 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
256 #endif
257 struct thread_entry *queue; /* List of threads waiting for thread to be
258 removed */
259 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
260 #define HAVE_WAKEUP_EXT_CB
261 void (*wakeup_ext_cb)(struct thread_entry *thread); /* Callback that
262 performs special steps needed when being
263 forced off of an object's wait queue that
264 go beyond the standard wait queue removal
265 and priority disinheritance */
266 /* Only enabled when using queue_send for now */
267 #endif
268 #if defined(HAVE_EXTENDED_MESSAGING_AND_NAME) || NUM_CORES > 1
269 intptr_t retval; /* Return value from a blocked operation/
270 misc. use */
271 #endif
272 #ifdef HAVE_PRIORITY_SCHEDULING
273 /* Priority summary of owned objects that support inheritance */
274 struct blocker *blocker; /* Pointer to blocker when this thread is blocked
275 on an object that supports PIP -
276 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
277 struct priority_distribution pdist; /* Priority summary of owned objects
278 that have blocked threads and thread's own
279 base priority */
280 int skip_count; /* Number of times skipped if higher priority
281 thread was running */
282 #endif
283 unsigned short stack_size; /* Size of stack in bytes */
284 #ifdef HAVE_PRIORITY_SCHEDULING
285 unsigned char base_priority; /* Base priority (set explicitly during
286 creation or thread_set_priority) */
287 unsigned char priority; /* Scheduled priority (higher of base or
288 all threads blocked by this one) */
289 #endif
290 unsigned char state; /* Thread slot state (STATE_*) */
291 #ifdef HAVE_SCHEDULER_BOOSTCTRL
292 unsigned char cpu_boost; /* CPU frequency boost flag */
293 #endif
294 #if NUM_CORES > 1
295 unsigned char core; /* The core to which thread belongs */
296 struct corelock waiter_cl; /* Corelock for thread_wait */
297 struct corelock slot_cl; /* Corelock to lock thread slot */
298 #endif
301 #if NUM_CORES > 1
302 /* Operations to be performed just before stopping a thread and starting
303 a new one if specified before calling switch_thread */
304 enum
306 TBOP_CLEAR = 0, /* No operation to do */
307 TBOP_UNLOCK_CORELOCK, /* Unlock a corelock variable */
308 TBOP_SWITCH_CORE, /* Call the core switch preparation routine */
311 struct thread_blk_ops
313 struct corelock *cl_p; /* pointer to corelock */
314 unsigned char flags; /* TBOP_* flags */
316 #endif /* NUM_CORES > 1 */
318 /* Information kept for each core
319 * Members are arranged for the same reason as in thread_entry
321 struct core_entry
323 /* "Active" lists - core is constantly active on these and are never
324 locked and interrupts do not access them */
325 struct thread_entry *running; /* threads that are running (RTR) */
326 struct thread_entry *timeout; /* threads that are on a timeout before
327 running again */
328 struct thread_entry *block_task; /* Task going off running list */
329 #ifdef HAVE_PRIORITY_SCHEDULING
330 struct priority_distribution rtr; /* Summary of running and ready-to-run
331 threads */
332 #endif
333 long next_tmo_check; /* soonest time to check tmo threads */
334 #if NUM_CORES > 1
335 struct thread_blk_ops blk_ops; /* operations to perform when
336 blocking a thread */
337 struct corelock rtr_cl; /* Lock for rtr list */
338 #endif /* NUM_CORES */
341 #ifdef HAVE_PRIORITY_SCHEDULING
342 #define IF_PRIO(...) __VA_ARGS__
343 #define IFN_PRIO(...)
344 #else
345 #define IF_PRIO(...)
346 #define IFN_PRIO(...) __VA_ARGS__
347 #endif
349 /* Macros generate better code than an inline function is this case */
350 #if (defined (CPU_PP) || defined (CPU_ARM))
351 /* atomic */
352 #if CONFIG_CORELOCK == SW_CORELOCK
353 #define test_and_set(a, v, cl) \
354 xchg8((a), (v), (cl))
355 /* atomic */
356 #define xchg8(a, v, cl) \
357 ({ uint32_t o; \
358 corelock_lock(cl); \
359 o = *(uint8_t *)(a); \
360 *(uint8_t *)(a) = (v); \
361 corelock_unlock(cl); \
362 o; })
363 #define xchg32(a, v, cl) \
364 ({ uint32_t o; \
365 corelock_lock(cl); \
366 o = *(uint32_t *)(a); \
367 *(uint32_t *)(a) = (v); \
368 corelock_unlock(cl); \
369 o; })
370 #define xchgptr(a, v, cl) \
371 ({ typeof (*(a)) o; \
372 corelock_lock(cl); \
373 o = *(a); \
374 *(a) = (v); \
375 corelock_unlock(cl); \
376 o; })
377 #elif CONFIG_CORELOCK == CORELOCK_SWAP
378 /* atomic */
379 #define test_and_set(a, v, ...) \
380 xchg8((a), (v))
381 #define xchg8(a, v, ...) \
382 ({ uint32_t o; \
383 asm volatile( \
384 "swpb %0, %1, [%2]" \
385 : "=&r"(o) \
386 : "r"(v), \
387 "r"((uint8_t*)(a))); \
388 o; })
389 /* atomic */
390 #define xchg32(a, v, ...) \
391 ({ uint32_t o; \
392 asm volatile( \
393 "swp %0, %1, [%2]" \
394 : "=&r"(o) \
395 : "r"((uint32_t)(v)), \
396 "r"((uint32_t*)(a))); \
397 o; })
398 /* atomic */
399 #define xchgptr(a, v, ...) \
400 ({ typeof (*(a)) o; \
401 asm volatile( \
402 "swp %0, %1, [%2]" \
403 : "=&r"(o) \
404 : "r"(v), "r"(a)); \
405 o; })
406 #endif /* locking selection */
407 #elif defined (CPU_COLDFIRE)
408 /* atomic */
409 /* one branch will be optimized away if v is a constant expression */
410 #define test_and_set(a, v, ...) \
411 ({ uint32_t o = 0; \
412 if (v) { \
413 asm volatile ( \
414 "bset.b #0, (%0)" \
415 : : "a"((uint8_t*)(a)) \
416 : "cc"); \
417 } else { \
418 asm volatile ( \
419 "bclr.b #0, (%0)" \
420 : : "a"((uint8_t*)(a)) \
421 : "cc"); \
423 asm volatile ("sne.b %0" \
424 : "+d"(o)); \
425 o; })
426 #elif CONFIG_CPU == SH7034
427 /* atomic */
428 #define test_and_set(a, v, ...) \
429 ({ uint32_t o; \
430 asm volatile ( \
431 "tas.b @%2 \n" \
432 "mov #-1, %0 \n" \
433 "negc %0, %0 \n" \
434 : "=r"(o) \
435 : "M"((uint32_t)(v)), /* Value of_v must be 1 */ \
436 "r"((uint8_t *)(a))); \
437 o; })
438 #endif /* CONFIG_CPU == */
440 /* defaults for no asm version */
441 #ifndef test_and_set
442 /* not atomic */
443 #define test_and_set(a, v, ...) \
444 ({ uint32_t o = *(uint8_t *)(a); \
445 *(uint8_t *)(a) = (v); \
446 o; })
447 #endif /* test_and_set */
448 #ifndef xchg8
449 /* not atomic */
450 #define xchg8(a, v, ...) \
451 ({ uint32_t o = *(uint8_t *)(a); \
452 *(uint8_t *)(a) = (v); \
453 o; })
454 #endif /* xchg8 */
455 #ifndef xchg32
456 /* not atomic */
457 #define xchg32(a, v, ...) \
458 ({ uint32_t o = *(uint32_t *)(a); \
459 *(uint32_t *)(a) = (v); \
460 o; })
461 #endif /* xchg32 */
462 #ifndef xchgptr
463 /* not atomic */
464 #define xchgptr(a, v, ...) \
465 ({ typeof (*(a)) o = *(a); \
466 *(a) = (v); \
467 o; })
468 #endif /* xchgptr */
470 void core_idle(void);
471 void core_wake(IF_COP_VOID(unsigned int core));
473 /* Initialize the scheduler */
474 void init_threads(void);
476 /* Allocate a thread in the scheduler */
477 #define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
478 struct thread_entry*
479 create_thread(void (*function)(void), void* stack, size_t stack_size,
480 unsigned flags, const char *name
481 IF_PRIO(, int priority)
482 IF_COP(, unsigned int core));
484 /* Set and clear the CPU frequency boost flag for the calling thread */
485 #ifdef HAVE_SCHEDULER_BOOSTCTRL
486 void trigger_cpu_boost(void);
487 void cancel_cpu_boost(void);
488 #else
489 #define trigger_cpu_boost()
490 #define cancel_cpu_boost()
491 #endif
492 /* Make a frozed thread runnable (when started with CREATE_THREAD_FROZEN).
493 * Has no effect on a thread not frozen. */
494 void thread_thaw(struct thread_entry *thread);
495 /* Wait for a thread to exit */
496 void thread_wait(struct thread_entry *thread);
497 /* Exit the current thread */
498 void thread_exit(void);
499 #if defined(DEBUG) || defined(ROCKBOX_HAS_LOGF)
500 #define ALLOW_REMOVE_THREAD
501 /* Remove a thread from the scheduler */
502 void remove_thread(struct thread_entry *thread);
503 #endif
505 /* Switch to next runnable thread */
506 void switch_thread(void);
507 /* Blocks a thread for at least the specified number of ticks (0 = wait until
508 * next tick) */
509 void sleep_thread(int ticks);
510 /* Indefinitely blocks the current thread on a thread queue */
511 void block_thread(struct thread_entry *current);
512 /* Blocks the current thread on a thread queue until explicitely woken or
513 * the timeout is reached */
514 void block_thread_w_tmo(struct thread_entry *current, int timeout);
516 /* Return bit flags for thread wakeup */
517 #define THREAD_NONE 0x0 /* No thread woken up (exclusive) */
518 #define THREAD_OK 0x1 /* A thread was woken up */
519 #define THREAD_SWITCH 0x2 /* Task switch recommended (one or more of
520 higher priority than current were woken) */
522 /* A convenience function for waking an entire queue of threads. */
523 unsigned int thread_queue_wake(struct thread_entry **list);
525 /* Wakeup a thread at the head of a list */
526 unsigned int wakeup_thread(struct thread_entry **list);
528 #ifdef HAVE_PRIORITY_SCHEDULING
529 int thread_set_priority(struct thread_entry *thread, int priority);
530 int thread_get_priority(struct thread_entry *thread);
531 #endif /* HAVE_PRIORITY_SCHEDULING */
532 #if NUM_CORES > 1
533 unsigned int switch_core(unsigned int new_core);
534 #endif
535 struct thread_entry * thread_get_current(void);
537 /* Debugging info - only! */
538 int thread_stack_usage(const struct thread_entry *thread);
539 #if NUM_CORES > 1
540 int idle_stack_usage(unsigned int core);
541 #endif
542 void thread_get_name(char *buffer, int size,
543 struct thread_entry *thread);
544 #ifdef RB_PROFILE
545 void profile_thread(void);
546 #endif
548 #endif /* THREAD_H */