2003-02-07 09:41:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Ulf Ralberg
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2003-02-07 09:41:57 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef THREAD_H
|
|
|
|
#define THREAD_H
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
#include <inttypes.h>
|
2007-10-16 01:25:17 +00:00
|
|
|
#include <stddef.h>
|
2003-02-14 09:44:34 +00:00
|
|
|
#include <stdbool.h>
|
2014-08-08 05:39:29 +00:00
|
|
|
#include "config.h"
|
2010-08-12 13:38:25 +00:00
|
|
|
#include "gcc_extensions.h"
|
2014-08-08 10:33:51 +00:00
|
|
|
#include "linked_list.h"
|
2014-04-24 08:09:18 +00:00
|
|
|
#include "bitarray.h"
|
2014-08-08 05:39:29 +00:00
|
|
|
#include "corelock.h"
|
2003-02-14 09:44:34 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Priority scheduling (when enabled with HAVE_PRIORITY_SCHEDULING) works
|
2008-05-18 14:34:29 +00:00
|
|
|
* by giving high priority threads more CPU time than lower priority threads
|
2008-03-25 02:34:12 +00:00
|
|
|
* when they need it. Priority is differential such that the priority
|
|
|
|
* difference between a lower priority runnable thread and the highest priority
|
2008-05-18 14:34:29 +00:00
|
|
|
* runnable thread determines the amount of aging necessary for the lower
|
2008-03-25 02:34:12 +00:00
|
|
|
* priority thread to be scheduled in order to prevent starvation.
|
|
|
|
*
|
2006-09-16 16:18:11 +00:00
|
|
|
* If software playback codec pcm buffer is going down to critical, codec
|
2008-03-25 02:34:12 +00:00
|
|
|
* can gradually raise its own priority to override user interface and
|
2006-09-16 16:18:11 +00:00
|
|
|
* prevent playback skipping.
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
#define PRIORITY_RESERVED_HIGH 0 /* Reserved */
|
|
|
|
#define PRIORITY_RESERVED_LOW 32 /* Reserved */
|
2007-10-16 01:25:17 +00:00
|
|
|
#define HIGHEST_PRIORITY 1 /* The highest possible thread priority */
|
2008-03-25 02:34:12 +00:00
|
|
|
#define LOWEST_PRIORITY 31 /* The lowest possible thread priority */
|
|
|
|
/* Realtime range reserved for threads that will not allow threads of lower
|
|
|
|
* priority to age and run (future expansion) */
|
|
|
|
#define PRIORITY_REALTIME_1 1
|
|
|
|
#define PRIORITY_REALTIME_2 2
|
|
|
|
#define PRIORITY_REALTIME_3 3
|
|
|
|
#define PRIORITY_REALTIME_4 4
|
|
|
|
#define PRIORITY_REALTIME 4 /* Lowest realtime range */
|
2008-03-29 23:21:19 +00:00
|
|
|
#define PRIORITY_BUFFERING 15 /* Codec buffering thread */
|
2014-08-08 10:33:51 +00:00
|
|
|
#define PRIORITY_USER_INTERFACE 16 /* For most UI thrads */
|
2008-03-25 02:34:12 +00:00
|
|
|
#define PRIORITY_RECORDING 16 /* Recording thread */
|
|
|
|
#define PRIORITY_PLAYBACK 16 /* Variable between this and MAX */
|
|
|
|
#define PRIORITY_PLAYBACK_MAX 5 /* Maximum allowable playback priority */
|
|
|
|
#define PRIORITY_SYSTEM 18 /* All other firmware threads */
|
|
|
|
#define PRIORITY_BACKGROUND 20 /* Normal application threads */
|
|
|
|
#define NUM_PRIORITIES 32
|
|
|
|
#define PRIORITY_IDLE 32 /* Priority representative of no tasks */
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2014-08-08 10:33:51 +00:00
|
|
|
#define PRIORITY_MAIN_THREAD PRIORITY_USER_INTERFACE
|
2010-04-03 22:02:09 +00:00
|
|
|
#define IO_PRIORITY_IMMEDIATE 0
|
|
|
|
#define IO_PRIORITY_BACKGROUND 32
|
|
|
|
|
2013-05-31 06:41:02 +00:00
|
|
|
# ifdef HAVE_HARDWARE_CLICK
|
|
|
|
# define BASETHREADS 17
|
|
|
|
# else
|
|
|
|
# define BASETHREADS 16
|
|
|
|
# endif
|
2005-11-07 20:09:08 +00:00
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
#ifndef TARGET_EXTRA_THREADS
|
|
|
|
#define TARGET_EXTRA_THREADS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXTHREADS (BASETHREADS+TARGET_EXTRA_THREADS)
|
2014-04-24 08:09:18 +00:00
|
|
|
|
|
|
|
BITARRAY_TYPE_DECLARE(threadbit_t, threadbit, MAXTHREADS)
|
|
|
|
BITARRAY_TYPE_DECLARE(priobit_t, priobit, NUM_PRIORITIES)
|
|
|
|
|
2014-08-08 05:39:29 +00:00
|
|
|
struct thread_entry;
|
|
|
|
|
2010-08-02 20:34:47 +00:00
|
|
|
/*
|
|
|
|
* We need more stack when we run under a host
|
|
|
|
* maybe more expensive C lib functions?
|
|
|
|
*
|
2011-02-18 22:46:01 +00:00
|
|
|
* simulator (possibly) doesn't simulate stack usage anyway but well ... */
|
2010-06-10 17:31:45 +00:00
|
|
|
|
2012-01-22 18:46:27 +00:00
|
|
|
#if defined(HAVE_SDL_THREADS) || defined(__PCTOOL__)
|
2012-01-04 17:07:21 +00:00
|
|
|
#define DEFAULT_STACK_SIZE 0x100 /* tiny, ignored anyway */
|
|
|
|
#else
|
|
|
|
#include "asm/thread.h"
|
|
|
|
#endif /* HAVE_SDL_THREADS */
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2014-08-08 05:39:29 +00:00
|
|
|
extern void yield(void);
|
|
|
|
extern unsigned sleep(unsigned ticks);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2014-08-08 05:39:29 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
#define IF_PRIO(...) __VA_ARGS__
|
|
|
|
#define IFN_PRIO(...)
|
|
|
|
#else
|
|
|
|
#define IF_PRIO(...)
|
|
|
|
#define IFN_PRIO(...) __VA_ARGS__
|
2007-03-21 22:58:53 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2014-08-08 10:33:51 +00:00
|
|
|
#define __wait_queue lld_head
|
|
|
|
#define __wait_queue_node lld_node
|
|
|
|
|
2014-04-24 08:09:18 +00:00
|
|
|
/* Basic structure describing the owner of an object */
|
2008-03-25 02:34:12 +00:00
|
|
|
struct blocker
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2010-12-29 13:53:30 +00:00
|
|
|
struct thread_entry * volatile thread; /* thread blocking other threads
|
|
|
|
(aka. object owner) */
|
2014-04-24 08:09:18 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
int priority; /* highest priority waiter */
|
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
2014-04-24 08:09:18 +00:00
|
|
|
/* If a thread has a blocker but the blocker's registered thread is NULL,
|
|
|
|
then it references this and the struct blocker pointer may be
|
|
|
|
reinterpreted as such. */
|
|
|
|
struct blocker_splay
|
|
|
|
{
|
|
|
|
struct blocker blocker; /* blocker info (first!) */
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
threadbit_t mask; /* mask of nonzero tcounts */
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
struct corelock cl; /* mutual exclusion */
|
|
|
|
#endif
|
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
|
|
|
};
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
void core_idle(void);
|
|
|
|
void core_wake(IF_COP_VOID(unsigned int core));
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Allocate a thread in the scheduler */
|
2007-10-16 01:25:17 +00:00
|
|
|
#define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
|
2008-12-10 08:57:10 +00:00
|
|
|
unsigned int create_thread(void (*function)(void),
|
|
|
|
void* stack, size_t stack_size,
|
|
|
|
unsigned flags, const char *name
|
|
|
|
IF_PRIO(, int priority)
|
|
|
|
IF_COP(, unsigned int core));
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Set and clear the CPU frequency boost flag for the calling thread */
|
2006-10-15 11:57:52 +00:00
|
|
|
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
|
|
|
void trigger_cpu_boost(void);
|
2007-11-20 03:44:25 +00:00
|
|
|
void cancel_cpu_boost(void);
|
2006-10-15 11:57:52 +00:00
|
|
|
#else
|
2010-12-22 16:05:22 +00:00
|
|
|
#define trigger_cpu_boost() do { } while(0)
|
|
|
|
#define cancel_cpu_boost() do { } while(0)
|
2006-10-15 11:57:52 +00:00
|
|
|
#endif
|
2014-08-08 05:39:29 +00:00
|
|
|
/* Make a frozen thread runnable (when started with CREATE_THREAD_FROZEN).
|
2008-03-25 02:34:12 +00:00
|
|
|
* Has no effect on a thread not frozen. */
|
2008-12-10 08:57:10 +00:00
|
|
|
void thread_thaw(unsigned int thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Wait for a thread to exit */
|
2008-12-10 08:57:10 +00:00
|
|
|
void thread_wait(unsigned int thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Exit the current thread */
|
2010-08-12 13:38:25 +00:00
|
|
|
void thread_exit(void) NORETURN_ATTR;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2008-12-10 08:57:10 +00:00
|
|
|
int thread_set_priority(unsigned int thread_id, int priority);
|
|
|
|
int thread_get_priority(unsigned int thread_id);
|
2007-03-09 08:03:18 +00:00
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
2014-04-24 08:09:18 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
unsigned int switch_core(unsigned int new_core);
|
|
|
|
#endif
|
2011-03-05 17:48:06 +00:00
|
|
|
|
|
|
|
/* Return the id of the calling thread. */
|
|
|
|
unsigned int thread_self(void);
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Debugging info - only! */
|
2007-09-28 10:20:02 +00:00
|
|
|
#if NUM_CORES > 1
|
2014-08-08 05:39:29 +00:00
|
|
|
struct core_debug_info
|
|
|
|
{
|
|
|
|
unsigned int idle_stack_usage;
|
|
|
|
};
|
|
|
|
|
|
|
|
int core_get_debug_info(unsigned int core, struct core_debug_info *infop);
|
|
|
|
|
|
|
|
#endif /* NUM_CORES */
|
|
|
|
|
2014-08-08 10:33:51 +00:00
|
|
|
#ifdef HAVE_SDL_THREADS
|
|
|
|
#define IF_SDL(x...) x
|
|
|
|
#define IFN_SDL(x...)
|
|
|
|
#else
|
|
|
|
#define IF_SDL(x...)
|
|
|
|
#define IFN_SDL(x...) x
|
|
|
|
#endif
|
|
|
|
|
2014-08-08 05:39:29 +00:00
|
|
|
struct thread_debug_info
|
|
|
|
{
|
|
|
|
char statusstr[4];
|
|
|
|
char name[32];
|
2014-08-08 10:33:51 +00:00
|
|
|
#ifndef HAVE_SDL_THREADS
|
2014-08-08 05:39:29 +00:00
|
|
|
unsigned int stack_usage;
|
2014-08-08 10:33:51 +00:00
|
|
|
#endif
|
2014-08-08 05:39:29 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
unsigned int core;
|
2007-09-28 10:20:02 +00:00
|
|
|
#endif
|
2014-08-08 05:39:29 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
int base_priority;
|
|
|
|
int current_priority;
|
2006-01-18 20:54:13 +00:00
|
|
|
#endif
|
2014-08-08 05:39:29 +00:00
|
|
|
};
|
|
|
|
int thread_get_debug_info(unsigned int thread_id,
|
|
|
|
struct thread_debug_info *infop);
|
2003-02-07 09:41:57 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif /* THREAD_H */
|