Convert many #ifdef SIMULATOR to #if (CONFIG_PLATFORM & PLATFORM_HOSTED) (and similar).
[kugel-rb.git] / firmware / export / thread.h
blob88bfa7accfc67796be23d5ae09222c3979a08d9e
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 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
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 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
122 struct regs
124 void *t; /* OS thread */
125 void *told; /* Last thread in slot (explained in thead-sdl.c) */
126 void *s; /* Semaphore for blocking and wakeup */
127 void (*start)(void); /* Start function */
129 #endif /* PLATFORM_NATIVE */
131 /* NOTE: The use of the word "queue" may also refer to a linked list of
132 threads being maintained that are normally dealt with in FIFO order
133 and not necessarily kernel event_queue */
134 enum
136 /* States without a timeout must be first */
137 STATE_KILLED = 0, /* Thread is killed (default) */
138 STATE_RUNNING, /* Thread is currently running */
139 STATE_BLOCKED, /* Thread is indefinitely blocked on a queue */
140 /* These states involve adding the thread to the tmo list */
141 STATE_SLEEPING, /* Thread is sleeping with a timeout */
142 STATE_BLOCKED_W_TMO, /* Thread is blocked on a queue with a timeout */
143 /* Miscellaneous states */
144 STATE_FROZEN, /* Thread is suspended and will not run until
145 thread_thaw is called with its ID */
146 THREAD_NUM_STATES,
147 TIMEOUT_STATE_FIRST = STATE_SLEEPING,
150 #if NUM_CORES > 1
151 /* Pointer value for name field to indicate thread is being killed. Using
152 * an alternate STATE_* won't work since that would interfere with operation
153 * while the thread is still running. */
154 #define THREAD_DESTRUCT ((const char *)~(intptr_t)0)
155 #endif
157 /* Link information for lists thread is in */
158 struct thread_entry; /* forward */
159 struct thread_list
161 struct thread_entry *prev; /* Previous thread in a list */
162 struct thread_entry *next; /* Next thread in a list */
165 /* Small objects for core-wise mutual exclusion */
166 #if CONFIG_CORELOCK == SW_CORELOCK
167 /* No reliable atomic instruction available - use Peterson's algorithm */
168 struct corelock
170 volatile unsigned char myl[NUM_CORES];
171 volatile unsigned char turn;
172 } __attribute__((packed));
174 void corelock_init(struct corelock *cl);
175 void corelock_lock(struct corelock *cl);
176 int corelock_try_lock(struct corelock *cl);
177 void corelock_unlock(struct corelock *cl);
178 #else
179 /* No atomic corelock op needed or just none defined */
180 #define corelock_init(cl)
181 #define corelock_lock(cl)
182 #define corelock_try_lock(cl)
183 #define corelock_unlock(cl)
184 #endif /* core locking selection */
186 #ifdef HAVE_PRIORITY_SCHEDULING
187 struct blocker
189 struct thread_entry *thread; /* thread blocking other threads
190 (aka. object owner) */
191 int priority; /* highest priority waiter */
192 struct thread_entry * (*wakeup_protocol)(struct thread_entry *thread);
195 /* Choices of wakeup protocol */
197 /* For transfer of object ownership by one thread to another thread by
198 * the owning thread itself (mutexes) */
199 struct thread_entry *
200 wakeup_priority_protocol_transfer(struct thread_entry *thread);
202 /* For release by owner where ownership doesn't change - other threads,
203 * interrupts, timeouts, etc. (mutex timeout, queues) */
204 struct thread_entry *
205 wakeup_priority_protocol_release(struct thread_entry *thread);
208 struct priority_distribution
210 uint8_t hist[NUM_PRIORITIES]; /* Histogram: Frequency for each priority */
211 uint32_t mask; /* Bitmask of hist entries that are not zero */
214 #endif /* HAVE_PRIORITY_SCHEDULING */
216 /* Information kept in each thread slot
217 * members are arranged according to size - largest first - in order
218 * to ensure both alignment and packing at the same time.
220 struct thread_entry
222 struct regs context; /* Register context at switch -
223 _must_ be first member */
224 uintptr_t *stack; /* Pointer to top of stack */
225 const char *name; /* Thread name */
226 long tmo_tick; /* Tick when thread should be woken from
227 timeout -
228 states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
229 struct thread_list l; /* Links for blocked/waking/running -
230 circular linkage in both directions */
231 struct thread_list tmo; /* Links for timeout list -
232 Circular in reverse direction, NULL-terminated in
233 forward direction -
234 states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
235 struct thread_entry **bqp; /* Pointer to list variable in kernel
236 object where thread is blocked - used
237 for implicit unblock and explicit wake
238 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
239 #if NUM_CORES > 1
240 struct corelock *obj_cl; /* Object corelock where thead is blocked -
241 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
242 struct corelock waiter_cl; /* Corelock for thread_wait */
243 struct corelock slot_cl; /* Corelock to lock thread slot */
244 unsigned char core; /* The core to which thread belongs */
245 #endif
246 struct thread_entry *queue; /* List of threads waiting for thread to be
247 removed */
248 #ifdef HAVE_WAKEUP_EXT_CB
249 void (*wakeup_ext_cb)(struct thread_entry *thread); /* Callback that
250 performs special steps needed when being
251 forced off of an object's wait queue that
252 go beyond the standard wait queue removal
253 and priority disinheritance */
254 /* Only enabled when using queue_send for now */
255 #endif
256 #if defined(HAVE_EXTENDED_MESSAGING_AND_NAME) || NUM_CORES > 1
257 intptr_t retval; /* Return value from a blocked operation/
258 misc. use */
259 #endif
260 #ifdef HAVE_PRIORITY_SCHEDULING
261 /* Priority summary of owned objects that support inheritance */
262 struct blocker *blocker; /* Pointer to blocker when this thread is blocked
263 on an object that supports PIP -
264 states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
265 struct priority_distribution pdist; /* Priority summary of owned objects
266 that have blocked threads and thread's own
267 base priority */
268 int skip_count; /* Number of times skipped if higher priority
269 thread was running */
270 unsigned char base_priority; /* Base priority (set explicitly during
271 creation or thread_set_priority) */
272 unsigned char priority; /* Scheduled priority (higher of base or
273 all threads blocked by this one) */
274 #endif
275 uint16_t id; /* Current slot id */
276 unsigned short stack_size; /* Size of stack in bytes */
277 unsigned char state; /* Thread slot state (STATE_*) */
278 #ifdef HAVE_SCHEDULER_BOOSTCTRL
279 unsigned char cpu_boost; /* CPU frequency boost flag */
280 #endif
281 #ifdef HAVE_IO_PRIORITY
282 unsigned char io_priority;
283 #endif
286 /*** Macros for internal use ***/
287 /* Thread ID, 16 bits = |VVVVVVVV|SSSSSSSS| */
288 #define THREAD_ID_VERSION_SHIFT 8
289 #define THREAD_ID_VERSION_MASK 0xff00
290 #define THREAD_ID_SLOT_MASK 0x00ff
291 #define THREAD_ID_INIT(n) ((1u << THREAD_ID_VERSION_SHIFT) | (n))
293 /* Specify current thread in a function taking an ID. */
294 #define THREAD_ID_CURRENT ((unsigned int)-1)
296 #if NUM_CORES > 1
297 /* Operations to be performed just before stopping a thread and starting
298 a new one if specified before calling switch_thread */
299 enum
301 TBOP_CLEAR = 0, /* No operation to do */
302 TBOP_UNLOCK_CORELOCK, /* Unlock a corelock variable */
303 TBOP_SWITCH_CORE, /* Call the core switch preparation routine */
306 struct thread_blk_ops
308 struct corelock *cl_p; /* pointer to corelock */
309 unsigned char flags; /* TBOP_* flags */
311 #endif /* NUM_CORES > 1 */
313 /* Information kept for each core
314 * Members are arranged for the same reason as in thread_entry
316 struct core_entry
318 /* "Active" lists - core is constantly active on these and are never
319 locked and interrupts do not access them */
320 struct thread_entry *running; /* threads that are running (RTR) */
321 struct thread_entry *timeout; /* threads that are on a timeout before
322 running again */
323 struct thread_entry *block_task; /* Task going off running list */
324 #ifdef HAVE_PRIORITY_SCHEDULING
325 struct priority_distribution rtr; /* Summary of running and ready-to-run
326 threads */
327 #endif
328 long next_tmo_check; /* soonest time to check tmo threads */
329 #if NUM_CORES > 1
330 struct thread_blk_ops blk_ops; /* operations to perform when
331 blocking a thread */
332 struct corelock rtr_cl; /* Lock for rtr list */
333 #endif /* NUM_CORES */
336 #ifdef HAVE_PRIORITY_SCHEDULING
337 #define IF_PRIO(...) __VA_ARGS__
338 #define IFN_PRIO(...)
339 #else
340 #define IF_PRIO(...)
341 #define IFN_PRIO(...) __VA_ARGS__
342 #endif
344 /* Macros generate better code than an inline function is this case */
345 #if defined (CPU_ARM)
346 /* atomic */
347 #if CONFIG_CORELOCK == SW_CORELOCK
348 #define test_and_set(a, v, cl) \
349 xchg8((a), (v), (cl))
350 /* atomic */
351 #define xchg8(a, v, cl) \
352 ({ uint32_t o; \
353 corelock_lock(cl); \
354 o = *(uint8_t *)(a); \
355 *(uint8_t *)(a) = (v); \
356 corelock_unlock(cl); \
357 o; })
358 #define xchg32(a, v, cl) \
359 ({ uint32_t o; \
360 corelock_lock(cl); \
361 o = *(uint32_t *)(a); \
362 *(uint32_t *)(a) = (v); \
363 corelock_unlock(cl); \
364 o; })
365 #define xchgptr(a, v, cl) \
366 ({ typeof (*(a)) o; \
367 corelock_lock(cl); \
368 o = *(a); \
369 *(a) = (v); \
370 corelock_unlock(cl); \
371 o; })
372 #endif /* locking selection */
373 #elif defined (CPU_COLDFIRE)
374 /* atomic */
375 /* one branch will be optimized away if v is a constant expression */
376 #define test_and_set(a, v, ...) \
377 ({ uint32_t o = 0; \
378 if (v) { \
379 asm volatile ( \
380 "bset.b #0, (%0)" \
381 : : "a"((uint8_t*)(a)) \
382 : "cc"); \
383 } else { \
384 asm volatile ( \
385 "bclr.b #0, (%0)" \
386 : : "a"((uint8_t*)(a)) \
387 : "cc"); \
389 asm volatile ("sne.b %0" \
390 : "+d"(o)); \
391 o; })
392 #elif CONFIG_CPU == SH7034
393 /* atomic */
394 #define test_and_set(a, v, ...) \
395 ({ uint32_t o; \
396 asm volatile ( \
397 "tas.b @%2 \n" \
398 "mov #-1, %0 \n" \
399 "negc %0, %0 \n" \
400 : "=r"(o) \
401 : "M"((uint32_t)(v)), /* Value of_v must be 1 */ \
402 "r"((uint8_t *)(a))); \
403 o; })
404 #endif /* CONFIG_CPU == */
406 /* defaults for no asm version */
407 #ifndef test_and_set
408 /* not atomic */
409 #define test_and_set(a, v, ...) \
410 ({ uint32_t o = *(uint8_t *)(a); \
411 *(uint8_t *)(a) = (v); \
412 o; })
413 #endif /* test_and_set */
414 #ifndef xchg8
415 /* not atomic */
416 #define xchg8(a, v, ...) \
417 ({ uint32_t o = *(uint8_t *)(a); \
418 *(uint8_t *)(a) = (v); \
419 o; })
420 #endif /* xchg8 */
421 #ifndef xchg32
422 /* not atomic */
423 #define xchg32(a, v, ...) \
424 ({ uint32_t o = *(uint32_t *)(a); \
425 *(uint32_t *)(a) = (v); \
426 o; })
427 #endif /* xchg32 */
428 #ifndef xchgptr
429 /* not atomic */
430 #define xchgptr(a, v, ...) \
431 ({ typeof (*(a)) o = *(a); \
432 *(a) = (v); \
433 o; })
434 #endif /* xchgptr */
436 void core_idle(void);
437 void core_wake(IF_COP_VOID(unsigned int core));
439 /* Initialize the scheduler */
440 void init_threads(void) INIT_ATTR;
442 /* Allocate a thread in the scheduler */
443 #define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
444 unsigned int create_thread(void (*function)(void),
445 void* stack, size_t stack_size,
446 unsigned flags, const char *name
447 IF_PRIO(, int priority)
448 IF_COP(, unsigned int core));
450 /* Set and clear the CPU frequency boost flag for the calling thread */
451 #ifdef HAVE_SCHEDULER_BOOSTCTRL
452 void trigger_cpu_boost(void);
453 void cancel_cpu_boost(void);
454 #else
455 #define trigger_cpu_boost()
456 #define cancel_cpu_boost()
457 #endif
458 /* Return thread entry from id */
459 struct thread_entry *thread_id_entry(unsigned int thread_id);
460 /* Make a frozed thread runnable (when started with CREATE_THREAD_FROZEN).
461 * Has no effect on a thread not frozen. */
462 void thread_thaw(unsigned int thread_id);
463 /* Wait for a thread to exit */
464 void thread_wait(unsigned int thread_id);
465 /* Exit the current thread */
466 void thread_exit(void);
467 #if defined(DEBUG) || defined(ROCKBOX_HAS_LOGF)
468 #define ALLOW_REMOVE_THREAD
469 /* Remove a thread from the scheduler */
470 void remove_thread(unsigned int thread_id);
471 #endif
473 /* Switch to next runnable thread */
474 void switch_thread(void);
475 /* Blocks a thread for at least the specified number of ticks (0 = wait until
476 * next tick) */
477 void sleep_thread(int ticks);
478 /* Indefinitely blocks the current thread on a thread queue */
479 void block_thread(struct thread_entry *current);
480 /* Blocks the current thread on a thread queue until explicitely woken or
481 * the timeout is reached */
482 void block_thread_w_tmo(struct thread_entry *current, int timeout);
484 /* Return bit flags for thread wakeup */
485 #define THREAD_NONE 0x0 /* No thread woken up (exclusive) */
486 #define THREAD_OK 0x1 /* A thread was woken up */
487 #define THREAD_SWITCH 0x2 /* Task switch recommended (one or more of
488 higher priority than current were woken) */
490 /* A convenience function for waking an entire queue of threads. */
491 unsigned int thread_queue_wake(struct thread_entry **list);
493 /* Wakeup a thread at the head of a list */
494 unsigned int wakeup_thread(struct thread_entry **list);
496 #ifdef HAVE_PRIORITY_SCHEDULING
497 int thread_set_priority(unsigned int thread_id, int priority);
498 int thread_get_priority(unsigned int thread_id);
499 #endif /* HAVE_PRIORITY_SCHEDULING */
500 #ifdef HAVE_IO_PRIORITY
501 void thread_set_io_priority(unsigned int thread_id, int io_priority);
502 int thread_get_io_priority(unsigned int thread_id);
503 #endif /* HAVE_IO_PRIORITY */
504 #if NUM_CORES > 1
505 unsigned int switch_core(unsigned int new_core);
506 #endif
507 unsigned int thread_get_current(void);
509 /* Debugging info - only! */
510 int thread_stack_usage(const struct thread_entry *thread);
511 #if NUM_CORES > 1
512 int idle_stack_usage(unsigned int core);
513 #endif
514 void thread_get_name(char *buffer, int size,
515 struct thread_entry *thread);
516 #ifdef RB_PROFILE
517 void profile_thread(void);
518 #endif
520 #endif /* THREAD_H */