1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
67 #define BASETHREADS 17
69 #define BASETHREADS 16
73 #define BASETHREADS 11
74 #endif /* CONFIG_CODE == * */
76 #ifndef TARGET_EXTRA_THREADS
77 #define TARGET_EXTRA_THREADS 0
80 #define MAXTHREADS (BASETHREADS+TARGET_EXTRA_THREADS)
83 * We need more stack when we run under a host
84 * maybe more expensive C lib functions?
86 * simulator doesn't simulate stack usage anyway but well ... */
87 #if ((CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(SIMULATOR))
88 #define DEFAULT_STACK_SIZE 0x400 /* Bytes */
90 #define DEFAULT_STACK_SIZE 0x1000 /* Bytes */
94 #if (CONFIG_PLATFORM & (PLATFORM_NATIVE|PLATFORM_ANDROID))
95 /* Need to keep structures inside the header file because debug_menu
100 uint32_t macsr
; /* 0 - EMAC status register */
101 uint32_t d
[6]; /* 4-24 - d2-d7 */
102 uint32_t a
[5]; /* 28-44 - a2-a6 */
103 uint32_t sp
; /* 48 - Stack pointer (a7) */
104 uint32_t start
; /* 52 - Thread start address, or NULL when started */
106 #elif CONFIG_CPU == SH7034
109 uint32_t r
[7]; /* 0-24 - Registers r8 thru r14 */
110 uint32_t sp
; /* 28 - Stack pointer (r15) */
111 uint32_t pr
; /* 32 - Procedure register */
112 uint32_t start
; /* 36 - Thread start address, or NULL when started */
114 #elif defined(CPU_ARM) || (CONFIG_PLATFORM & PLATFORM_ANDROID)
117 uint32_t r
[8]; /* 0-28 - Registers r4-r11 */
118 uint32_t sp
; /* 32 - Stack pointer (r13) */
119 uint32_t lr
; /* 36 - r14 (lr) */
120 uint32_t start
; /* 40 - Thread start address, or NULL when started */
124 #ifdef HAVE_CORELOCK_OBJECT
125 /* No reliable atomic instruction available - use Peterson's algorithm */
128 volatile unsigned char myl
[NUM_CORES
];
129 volatile unsigned char turn
;
130 } __attribute__((packed
));
132 /* Too big to inline everywhere */
133 void corelock_init(struct corelock
*cl
);
134 void corelock_lock(struct corelock
*cl
);
135 int corelock_try_lock(struct corelock
*cl
);
136 void corelock_unlock(struct corelock
*cl
);
137 #endif /* HAVE_CORELOCK_OBJECT */
139 #elif defined(CPU_MIPS)
142 uint32_t r
[9]; /* 0-32 - Registers s0-s7, fp */
143 uint32_t sp
; /* 36 - Stack pointer */
144 uint32_t ra
; /* 40 - Return address */
145 uint32_t start
; /* 44 - Thread start address, or NULL when started */
147 #endif /* CONFIG_CPU */
148 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
151 void *t
; /* OS thread */
152 void *told
; /* Last thread in slot (explained in thead-sdl.c) */
153 void *s
; /* Semaphore for blocking and wakeup */
154 void (*start
)(void); /* Start function */
156 #endif /* PLATFORM_NATIVE */
158 /* NOTE: The use of the word "queue" may also refer to a linked list of
159 threads being maintained that are normally dealt with in FIFO order
160 and not necessarily kernel event_queue */
163 /* States without a timeout must be first */
164 STATE_KILLED
= 0, /* Thread is killed (default) */
165 STATE_RUNNING
, /* Thread is currently running */
166 STATE_BLOCKED
, /* Thread is indefinitely blocked on a queue */
167 /* These states involve adding the thread to the tmo list */
168 STATE_SLEEPING
, /* Thread is sleeping with a timeout */
169 STATE_BLOCKED_W_TMO
, /* Thread is blocked on a queue with a timeout */
170 /* Miscellaneous states */
171 STATE_FROZEN
, /* Thread is suspended and will not run until
172 thread_thaw is called with its ID */
174 TIMEOUT_STATE_FIRST
= STATE_SLEEPING
,
178 /* Pointer value for name field to indicate thread is being killed. Using
179 * an alternate STATE_* won't work since that would interfere with operation
180 * while the thread is still running. */
181 #define THREAD_DESTRUCT ((const char *)~(intptr_t)0)
184 /* Link information for lists thread is in */
185 struct thread_entry
; /* forward */
188 struct thread_entry
*prev
; /* Previous thread in a list */
189 struct thread_entry
*next
; /* Next thread in a list */
192 #ifndef HAVE_CORELOCK_OBJECT
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 /* HAVE_CORELOCK_OBJECT */
200 #ifdef HAVE_PRIORITY_SCHEDULING
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.
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
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
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 */
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 */
260 struct thread_entry
*queue
; /* List of threads waiting for thread to be
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 */
270 #if defined(HAVE_EXTENDED_MESSAGING_AND_NAME) || NUM_CORES > 1
271 intptr_t retval
; /* Return value from a blocked operation/
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
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) */
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 */
295 #ifdef HAVE_IO_PRIORITY
296 unsigned char io_priority
;
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)
311 /* Operations to be performed just before stopping a thread and starting
312 a new one if specified before calling switch_thread */
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
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
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
342 long next_tmo_check
; /* soonest time to check tmo threads */
344 struct thread_blk_ops blk_ops
; /* operations to perform when
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(...)
355 #define IFN_PRIO(...) __VA_ARGS__
358 void core_idle(void);
359 void core_wake(IF_COP_VOID(unsigned int core
));
361 /* Initialize the scheduler */
362 void init_threads(void) INIT_ATTR
;
364 /* Allocate a thread in the scheduler */
365 #define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
366 unsigned int create_thread(void (*function
)(void),
367 void* stack
, size_t stack_size
,
368 unsigned flags
, const char *name
369 IF_PRIO(, int priority
)
370 IF_COP(, unsigned int core
));
372 /* Set and clear the CPU frequency boost flag for the calling thread */
373 #ifdef HAVE_SCHEDULER_BOOSTCTRL
374 void trigger_cpu_boost(void);
375 void cancel_cpu_boost(void);
377 #define trigger_cpu_boost()
378 #define cancel_cpu_boost()
380 /* Return thread entry from id */
381 struct thread_entry
*thread_id_entry(unsigned int thread_id
);
382 /* Make a frozed thread runnable (when started with CREATE_THREAD_FROZEN).
383 * Has no effect on a thread not frozen. */
384 void thread_thaw(unsigned int thread_id
);
385 /* Wait for a thread to exit */
386 void thread_wait(unsigned int thread_id
);
387 /* Exit the current thread */
388 void thread_exit(void) __attribute__((noreturn
));
389 #if defined(DEBUG) || defined(ROCKBOX_HAS_LOGF)
390 #define ALLOW_REMOVE_THREAD
391 /* Remove a thread from the scheduler */
392 void remove_thread(unsigned int thread_id
);
395 /* Switch to next runnable thread */
396 void switch_thread(void);
397 /* Blocks a thread for at least the specified number of ticks (0 = wait until
399 void sleep_thread(int ticks
);
400 /* Indefinitely blocks the current thread on a thread queue */
401 void block_thread(struct thread_entry
*current
);
402 /* Blocks the current thread on a thread queue until explicitely woken or
403 * the timeout is reached */
404 void block_thread_w_tmo(struct thread_entry
*current
, int timeout
);
406 /* Return bit flags for thread wakeup */
407 #define THREAD_NONE 0x0 /* No thread woken up (exclusive) */
408 #define THREAD_OK 0x1 /* A thread was woken up */
409 #define THREAD_SWITCH 0x2 /* Task switch recommended (one or more of
410 higher priority than current were woken) */
412 /* A convenience function for waking an entire queue of threads. */
413 unsigned int thread_queue_wake(struct thread_entry
**list
);
415 /* Wakeup a thread at the head of a list */
416 unsigned int wakeup_thread(struct thread_entry
**list
);
418 #ifdef HAVE_PRIORITY_SCHEDULING
419 int thread_set_priority(unsigned int thread_id
, int priority
);
420 int thread_get_priority(unsigned int thread_id
);
421 #endif /* HAVE_PRIORITY_SCHEDULING */
422 #ifdef HAVE_IO_PRIORITY
423 void thread_set_io_priority(unsigned int thread_id
, int io_priority
);
424 int thread_get_io_priority(unsigned int thread_id
);
425 #endif /* HAVE_IO_PRIORITY */
427 unsigned int switch_core(unsigned int new_core
);
429 unsigned int thread_get_current(void);
431 /* Debugging info - only! */
432 int thread_stack_usage(const struct thread_entry
*thread
);
434 int idle_stack_usage(unsigned int core
);
436 void thread_get_name(char *buffer
, int size
,
437 struct thread_entry
*thread
);
439 void profile_thread(void);
442 #endif /* THREAD_H */