fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / export / thread.h
bloba473e3a4ff9c2d52ca61e75073ed06a1f54dc658
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 #define IO_PRIORITY_IMMEDIATE 0
62 #define IO_PRIORITY_BACKGROUND 32
64 #if CONFIG_CODEC == SWCODEC
66 #ifdef HAVE_RECORDING
67 #define BASETHREADS 17
68 #else
69 #define BASETHREADS 16
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[9]; /* 0-32 - Registers s0-s7, fp */
116 uint32_t sp; /* 36 - Stack pointer */
117 uint32_t ra; /* 40 - Return address */
118 uint32_t start; /* 44 - 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 struct corelock waiter_cl; /* Corelock for thread_wait */
257 struct corelock slot_cl; /* Corelock to lock thread slot */
258 unsigned char core; /* The core to which thread belongs */
259 #endif
260 struct thread_entry *queue; /* List of threads waiting for thread to be
261 removed */
262 #ifdef HAVE_WAKEUP_EXT_CB
263 void (*wakeup_ext_cb)(struct thread_entry *thread); /* Callback that
264 performs special steps needed when being
265 forced off of an object's wait queue that
266 go beyond the standard wait queue removal
267 and priority disinheritance */
268 /* Only enabled when using queue_send for now */
269 #endif
270 #if defined(HAVE_EXTENDED_MESSAGING_AND_NAME) || NUM_CORES > 1
271 intptr_t retval; /* Return value from a blocked operation/
272 misc. use */
273 #endif
274 #ifdef HAVE_PRIORITY_SCHEDULING
275 /* Priority summary of owned objects that support inheritance */
276 struct blocker *blocker; /* Pointer to blocker when this thread is blocked
277 on an object that supports PIP -
278 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
279 struct priority_distribution pdist; /* Priority summary of owned objects
280 that have blocked threads and thread's own
281 base priority */
282 int skip_count; /* Number of times skipped if higher priority
283 thread was running */
284 unsigned char base_priority; /* Base priority (set explicitly during
285 creation or thread_set_priority) */
286 unsigned char priority; /* Scheduled priority (higher of base or
287 all threads blocked by this one) */
288 #endif
289 uint16_t id; /* Current slot id */
290 unsigned short stack_size; /* Size of stack in bytes */
291 unsigned char state; /* Thread slot state (STATE_*) */
292 #ifdef HAVE_SCHEDULER_BOOSTCTRL
293 unsigned char cpu_boost; /* CPU frequency boost flag */
294 #endif
295 #ifdef HAVE_IO_PRIORITY
296 unsigned char io_priority;
297 #endif
300 /*** Macros for internal use ***/
301 /* Thread ID, 16 bits = |VVVVVVVV|SSSSSSSS| */
302 #define THREAD_ID_VERSION_SHIFT 8
303 #define THREAD_ID_VERSION_MASK 0xff00
304 #define THREAD_ID_SLOT_MASK 0x00ff
305 #define THREAD_ID_INIT(n) ((1u << THREAD_ID_VERSION_SHIFT) | (n))
307 /* Specify current thread in a function taking an ID. */
308 #define THREAD_ID_CURRENT ((unsigned int)-1)
310 #if NUM_CORES > 1
311 /* Operations to be performed just before stopping a thread and starting
312 a new one if specified before calling switch_thread */
313 enum
315 TBOP_CLEAR = 0, /* No operation to do */
316 TBOP_UNLOCK_CORELOCK, /* Unlock a corelock variable */
317 TBOP_SWITCH_CORE, /* Call the core switch preparation routine */
320 struct thread_blk_ops
322 struct corelock *cl_p; /* pointer to corelock */
323 unsigned char flags; /* TBOP_* flags */
325 #endif /* NUM_CORES > 1 */
327 /* Information kept for each core
328 * Members are arranged for the same reason as in thread_entry
330 struct core_entry
332 /* "Active" lists - core is constantly active on these and are never
333 locked and interrupts do not access them */
334 struct thread_entry *running; /* threads that are running (RTR) */
335 struct thread_entry *timeout; /* threads that are on a timeout before
336 running again */
337 struct thread_entry *block_task; /* Task going off running list */
338 #ifdef HAVE_PRIORITY_SCHEDULING
339 struct priority_distribution rtr; /* Summary of running and ready-to-run
340 threads */
341 #endif
342 long next_tmo_check; /* soonest time to check tmo threads */
343 #if NUM_CORES > 1
344 struct thread_blk_ops blk_ops; /* operations to perform when
345 blocking a thread */
346 struct corelock rtr_cl; /* Lock for rtr list */
347 #endif /* NUM_CORES */
350 #ifdef HAVE_PRIORITY_SCHEDULING
351 #define IF_PRIO(...) __VA_ARGS__
352 #define IFN_PRIO(...)
353 #else
354 #define IF_PRIO(...)
355 #define IFN_PRIO(...) __VA_ARGS__
356 #endif
358 /* Macros generate better code than an inline function is this case */
359 #if (defined (CPU_PP) || defined (CPU_ARM))
360 /* atomic */
361 #if CONFIG_CORELOCK == SW_CORELOCK
362 #define test_and_set(a, v, cl) \
363 xchg8((a), (v), (cl))
364 /* atomic */
365 #define xchg8(a, v, cl) \
366 ({ uint32_t o; \
367 corelock_lock(cl); \
368 o = *(uint8_t *)(a); \
369 *(uint8_t *)(a) = (v); \
370 corelock_unlock(cl); \
371 o; })
372 #define xchg32(a, v, cl) \
373 ({ uint32_t o; \
374 corelock_lock(cl); \
375 o = *(uint32_t *)(a); \
376 *(uint32_t *)(a) = (v); \
377 corelock_unlock(cl); \
378 o; })
379 #define xchgptr(a, v, cl) \
380 ({ typeof (*(a)) o; \
381 corelock_lock(cl); \
382 o = *(a); \
383 *(a) = (v); \
384 corelock_unlock(cl); \
385 o; })
386 #elif CONFIG_CORELOCK == CORELOCK_SWAP
387 /* atomic */
388 #define test_and_set(a, v, ...) \
389 xchg8((a), (v))
390 #define xchg8(a, v, ...) \
391 ({ uint32_t o; \
392 asm volatile( \
393 "swpb %0, %1, [%2]" \
394 : "=&r"(o) \
395 : "r"(v), \
396 "r"((uint8_t*)(a))); \
397 o; })
398 /* atomic */
399 #define xchg32(a, v, ...) \
400 ({ uint32_t o; \
401 asm volatile( \
402 "swp %0, %1, [%2]" \
403 : "=&r"(o) \
404 : "r"((uint32_t)(v)), \
405 "r"((uint32_t*)(a))); \
406 o; })
407 /* atomic */
408 #define xchgptr(a, v, ...) \
409 ({ typeof (*(a)) o; \
410 asm volatile( \
411 "swp %0, %1, [%2]" \
412 : "=&r"(o) \
413 : "r"(v), "r"(a)); \
414 o; })
415 #endif /* locking selection */
416 #elif defined (CPU_COLDFIRE)
417 /* atomic */
418 /* one branch will be optimized away if v is a constant expression */
419 #define test_and_set(a, v, ...) \
420 ({ uint32_t o = 0; \
421 if (v) { \
422 asm volatile ( \
423 "bset.b #0, (%0)" \
424 : : "a"((uint8_t*)(a)) \
425 : "cc"); \
426 } else { \
427 asm volatile ( \
428 "bclr.b #0, (%0)" \
429 : : "a"((uint8_t*)(a)) \
430 : "cc"); \
432 asm volatile ("sne.b %0" \
433 : "+d"(o)); \
434 o; })
435 #elif CONFIG_CPU == SH7034
436 /* atomic */
437 #define test_and_set(a, v, ...) \
438 ({ uint32_t o; \
439 asm volatile ( \
440 "tas.b @%2 \n" \
441 "mov #-1, %0 \n" \
442 "negc %0, %0 \n" \
443 : "=r"(o) \
444 : "M"((uint32_t)(v)), /* Value of_v must be 1 */ \
445 "r"((uint8_t *)(a))); \
446 o; })
447 #endif /* CONFIG_CPU == */
449 /* defaults for no asm version */
450 #ifndef test_and_set
451 /* not atomic */
452 #define test_and_set(a, v, ...) \
453 ({ uint32_t o = *(uint8_t *)(a); \
454 *(uint8_t *)(a) = (v); \
455 o; })
456 #endif /* test_and_set */
457 #ifndef xchg8
458 /* not atomic */
459 #define xchg8(a, v, ...) \
460 ({ uint32_t o = *(uint8_t *)(a); \
461 *(uint8_t *)(a) = (v); \
462 o; })
463 #endif /* xchg8 */
464 #ifndef xchg32
465 /* not atomic */
466 #define xchg32(a, v, ...) \
467 ({ uint32_t o = *(uint32_t *)(a); \
468 *(uint32_t *)(a) = (v); \
469 o; })
470 #endif /* xchg32 */
471 #ifndef xchgptr
472 /* not atomic */
473 #define xchgptr(a, v, ...) \
474 ({ typeof (*(a)) o = *(a); \
475 *(a) = (v); \
476 o; })
477 #endif /* xchgptr */
479 void core_idle(void);
480 void core_wake(IF_COP_VOID(unsigned int core));
482 /* Initialize the scheduler */
483 void init_threads(void) INIT_ATTR;
485 /* Allocate a thread in the scheduler */
486 #define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
487 unsigned int create_thread(void (*function)(void),
488 void* stack, size_t stack_size,
489 unsigned flags, const char *name
490 IF_PRIO(, int priority)
491 IF_COP(, unsigned int core));
493 /* Set and clear the CPU frequency boost flag for the calling thread */
494 #ifdef HAVE_SCHEDULER_BOOSTCTRL
495 void trigger_cpu_boost(void);
496 void cancel_cpu_boost(void);
497 #else
498 #define trigger_cpu_boost()
499 #define cancel_cpu_boost()
500 #endif
501 /* Return thread entry from id */
502 struct thread_entry *thread_id_entry(unsigned int thread_id);
503 /* Make a frozed thread runnable (when started with CREATE_THREAD_FROZEN).
504 * Has no effect on a thread not frozen. */
505 void thread_thaw(unsigned int thread_id);
506 /* Wait for a thread to exit */
507 void thread_wait(unsigned int thread_id);
508 /* Exit the current thread */
509 void thread_exit(void);
510 #if defined(DEBUG) || defined(ROCKBOX_HAS_LOGF)
511 #define ALLOW_REMOVE_THREAD
512 /* Remove a thread from the scheduler */
513 void remove_thread(unsigned int thread_id);
514 #endif
516 /* Switch to next runnable thread */
517 void switch_thread(void);
518 /* Blocks a thread for at least the specified number of ticks (0 = wait until
519 * next tick) */
520 void sleep_thread(int ticks);
521 /* Indefinitely blocks the current thread on a thread queue */
522 void block_thread(struct thread_entry *current);
523 /* Blocks the current thread on a thread queue until explicitely woken or
524 * the timeout is reached */
525 void block_thread_w_tmo(struct thread_entry *current, int timeout);
527 /* Return bit flags for thread wakeup */
528 #define THREAD_NONE 0x0 /* No thread woken up (exclusive) */
529 #define THREAD_OK 0x1 /* A thread was woken up */
530 #define THREAD_SWITCH 0x2 /* Task switch recommended (one or more of
531 higher priority than current were woken) */
533 /* A convenience function for waking an entire queue of threads. */
534 unsigned int thread_queue_wake(struct thread_entry **list);
536 /* Wakeup a thread at the head of a list */
537 unsigned int wakeup_thread(struct thread_entry **list);
539 #ifdef HAVE_PRIORITY_SCHEDULING
540 int thread_set_priority(unsigned int thread_id, int priority);
541 int thread_get_priority(unsigned int thread_id);
542 #endif /* HAVE_PRIORITY_SCHEDULING */
543 #ifdef HAVE_IO_PRIORITY
544 void thread_set_io_priority(unsigned int thread_id, int io_priority);
545 int thread_get_io_priority(unsigned int thread_id);
546 #endif /* HAVE_IO_PRIORITY */
547 #if NUM_CORES > 1
548 unsigned int switch_core(unsigned int new_core);
549 #endif
550 unsigned int thread_get_current(void);
552 /* Debugging info - only! */
553 int thread_stack_usage(const struct thread_entry *thread);
554 #if NUM_CORES > 1
555 int idle_stack_usage(unsigned int core);
556 #endif
557 void thread_get_name(char *buffer, int size,
558 struct thread_entry *thread);
559 #ifdef RB_PROFILE
560 void profile_thread(void);
561 #endif
563 #endif /* THREAD_H */