mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-13 18:17:39 -04:00
Add a complete priority inheritance implementation to the scheduler (all mutex ownership and queue_send calls are inheritable). Priorities are differential so that dispatch depends on the runnable range of priorities. Codec priority can therefore be raised in small steps (pcmbuf updated to enable). Simplify the kernel functions to ease implementation and use the same kernel.c for both sim and target (I'm tired of maintaining two ;_). 1) Not sure if a minor audio break at first buffering issue will exist on large-sector disks (the main mutex speed issue was genuinely resolved earlier). At this point it's best dealt with at the buffering level. It seems a larger filechunk could be used again. 2) Perhaps 64-bit sims will have some minor issues (finicky) but a backroll of the code of concern there is a 5-minute job. All kernel objects become incompatible so a full rebuild and update is needed.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@16791 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
bc2f8fd8f3
commit
27cf677339
36 changed files with 3271 additions and 3250 deletions
|
@ -1446,16 +1446,21 @@ void buffering_thread(void)
|
|||
|
||||
void buffering_init(void) {
|
||||
mutex_init(&llist_mutex);
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
/* This behavior not safe atm */
|
||||
mutex_set_preempt(&llist_mutex, false);
|
||||
#endif
|
||||
|
||||
conf_watermark = BUFFERING_DEFAULT_WATERMARK;
|
||||
|
||||
queue_init(&buffering_queue, true);
|
||||
queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list);
|
||||
|
||||
buffering_thread_p = create_thread( buffering_thread, buffering_stack,
|
||||
sizeof(buffering_stack), CREATE_THREAD_FROZEN,
|
||||
buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
|
||||
IF_COP(, CPU));
|
||||
|
||||
queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
|
||||
buffering_thread_p);
|
||||
}
|
||||
|
||||
/* Initialise the buffering subsystem */
|
||||
|
|
|
@ -76,6 +76,7 @@ struct codec_api ci = {
|
|||
false, /* stop_codec */
|
||||
0, /* new_track */
|
||||
0, /* seek_time */
|
||||
NULL, /* struct dsp_config *dsp */
|
||||
NULL, /* get_codec_memory */
|
||||
NULL, /* pcmbuf_insert */
|
||||
NULL, /* set_elapsed */
|
||||
|
@ -95,6 +96,23 @@ struct codec_api ci = {
|
|||
PREFIX(sleep),
|
||||
yield,
|
||||
|
||||
#if NUM_CORES > 1
|
||||
create_thread,
|
||||
thread_thaw,
|
||||
thread_wait,
|
||||
semaphore_init,
|
||||
semaphore_wait,
|
||||
semaphore_release,
|
||||
event_init,
|
||||
event_wait,
|
||||
event_set_state,
|
||||
#endif
|
||||
|
||||
#ifdef CACHE_FUNCTIONS_AS_CALL
|
||||
flush_icache,
|
||||
invalidate_icache,
|
||||
#endif
|
||||
|
||||
/* strings and memory */
|
||||
strcpy,
|
||||
strncpy,
|
||||
|
@ -147,24 +165,6 @@ struct codec_api ci = {
|
|||
/* new stuff at the end, sort into place next time
|
||||
the API gets incompatible */
|
||||
|
||||
#ifdef CACHE_FUNCTIONS_AS_CALL
|
||||
flush_icache,
|
||||
invalidate_icache,
|
||||
#endif
|
||||
|
||||
NULL, /* struct dsp_config *dsp */
|
||||
|
||||
#if NUM_CORES > 1
|
||||
create_thread,
|
||||
thread_thaw,
|
||||
thread_wait,
|
||||
semaphore_init,
|
||||
semaphore_wait,
|
||||
semaphore_release,
|
||||
event_init,
|
||||
event_wait,
|
||||
event_set_state,
|
||||
#endif
|
||||
};
|
||||
|
||||
void codec_get_full_path(char *path, const char *codec_root_fn)
|
||||
|
|
|
@ -80,12 +80,12 @@
|
|||
#define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
|
||||
|
||||
/* increase this every time the api struct changes */
|
||||
#define CODEC_API_VERSION 22
|
||||
#define CODEC_API_VERSION 23
|
||||
|
||||
/* update this to latest version if a change to the api struct breaks
|
||||
backwards compatibility (and please take the opportunity to sort in any
|
||||
new function which are "waiting" at the end of the function table) */
|
||||
#define CODEC_MIN_API_VERSION 22
|
||||
#define CODEC_MIN_API_VERSION 23
|
||||
|
||||
/* codec return codes */
|
||||
enum codec_status {
|
||||
|
@ -119,6 +119,9 @@ struct codec_api {
|
|||
if codec supports seeking. */
|
||||
long seek_time;
|
||||
|
||||
/* The dsp instance to be used for audio output */
|
||||
struct dsp_config *dsp;
|
||||
|
||||
/* Returns buffer to malloc array. Only codeclib should need this. */
|
||||
void* (*get_codec_memory)(size_t *size);
|
||||
/* Insert PCM data into audio buffer for playback. Playback will start
|
||||
|
@ -160,6 +163,28 @@ struct codec_api {
|
|||
void (*PREFIX(sleep))(int ticks);
|
||||
void (*yield)(void);
|
||||
|
||||
#if NUM_CORES > 1
|
||||
struct thread_entry *
|
||||
(*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));
|
||||
|
||||
void (*thread_thaw)(struct thread_entry *thread);
|
||||
void (*thread_wait)(struct thread_entry *thread);
|
||||
void (*semaphore_init)(struct semaphore *s, int max, int start);
|
||||
void (*semaphore_wait)(struct semaphore *s);
|
||||
void (*semaphore_release)(struct semaphore *s);
|
||||
void (*event_init)(struct event *e, unsigned int flags);
|
||||
void (*event_wait)(struct event *e, unsigned int for_state);
|
||||
void (*event_set_state)(struct event *e, unsigned int state);
|
||||
#endif /* NUM_CORES */
|
||||
|
||||
#ifdef CACHE_FUNCTIONS_AS_CALL
|
||||
void (*flush_icache)(void);
|
||||
void (*invalidate_icache)(void);
|
||||
#endif
|
||||
|
||||
/* strings and memory */
|
||||
char* (*strcpy)(char *dst, const char *src);
|
||||
char* (*strncpy)(char *dst, const char *src, size_t length);
|
||||
|
@ -218,29 +243,6 @@ struct codec_api {
|
|||
/* new stuff at the end, sort into place next time
|
||||
the API gets incompatible */
|
||||
|
||||
#ifdef CACHE_FUNCTIONS_AS_CALL
|
||||
void (*flush_icache)(void);
|
||||
void (*invalidate_icache)(void);
|
||||
#endif
|
||||
|
||||
struct dsp_config *dsp;
|
||||
|
||||
#if NUM_CORES > 1
|
||||
struct thread_entry *
|
||||
(*create_thread)(void (*function)(void), void* stack,
|
||||
int stack_size, unsigned flags, const char *name
|
||||
IF_PRIO(, int priority)
|
||||
IF_COP(, unsigned int core));
|
||||
|
||||
void (*thread_thaw)(struct thread_entry *thread);
|
||||
void (*thread_wait)(struct thread_entry *thread);
|
||||
void (*semaphore_init)(struct semaphore *s, int max, int start);
|
||||
void (*semaphore_wait)(struct semaphore *s);
|
||||
void (*semaphore_release)(struct semaphore *s);
|
||||
void (*event_init)(struct event *e, unsigned int flags);
|
||||
void (*event_wait)(struct event *e, unsigned int for_state);
|
||||
void (*event_set_state)(struct event *e, unsigned int state);
|
||||
#endif /* NUM_CORES */
|
||||
};
|
||||
|
||||
/* codec header */
|
||||
|
|
|
@ -127,11 +127,6 @@ static char thread_status_char(unsigned status)
|
|||
[STATE_KILLED] = 'K',
|
||||
};
|
||||
|
||||
#if NUM_CORES > 1
|
||||
if (status == STATE_BUSY) /* Not a state index */
|
||||
return '.';
|
||||
#endif
|
||||
|
||||
if (status > THREAD_NUM_STATES)
|
||||
status = THREAD_NUM_STATES;
|
||||
|
||||
|
@ -166,15 +161,15 @@ static char* threads_getname(int selected_item, void * data, char *buffer)
|
|||
thread_get_name(name, 32, thread);
|
||||
|
||||
snprintf(buffer, MAX_PATH,
|
||||
"%2d: " IF_COP("(%d) ") "%c%c " IF_PRIO("%d ") "%2d%% %s",
|
||||
"%2d: " IF_COP("(%d) ") "%c%c " IF_PRIO("%d %d ") "%2d%% %s",
|
||||
selected_item,
|
||||
IF_COP(thread->core,)
|
||||
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
||||
(thread->boosted) ? '+' :
|
||||
(thread->cpu_boost) ? '+' :
|
||||
#endif
|
||||
((thread->state == STATE_RUNNING) ? '*' : ' '),
|
||||
thread_status_char(thread->state),
|
||||
IF_PRIO(thread->priority,)
|
||||
IF_PRIO(thread->base_priority, thread->priority, )
|
||||
thread_stack_usage(thread), name);
|
||||
|
||||
return buffer;
|
||||
|
|
|
@ -270,7 +270,7 @@ static void init_tagcache(void)
|
|||
|
||||
static void init(void)
|
||||
{
|
||||
init_threads();
|
||||
kernel_init();
|
||||
buffer_init();
|
||||
set_irq_level(0);
|
||||
lcd_init();
|
||||
|
|
|
@ -116,7 +116,7 @@ static bool low_latency_mode = false;
|
|||
static bool pcmbuf_flush;
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
static int codec_thread_priority = 0;
|
||||
static int codec_thread_priority = PRIORITY_PLAYBACK;
|
||||
#endif
|
||||
|
||||
extern struct thread_entry *codec_thread_p;
|
||||
|
@ -256,18 +256,21 @@ static void boost_codec_thread(bool boost)
|
|||
* will starve if the codec thread's priority is boosted. */
|
||||
if (boost)
|
||||
{
|
||||
if (codec_thread_priority == 0)
|
||||
int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
|
||||
/ (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
|
||||
|
||||
if (priority != codec_thread_priority)
|
||||
{
|
||||
codec_thread_priority = thread_set_priority(
|
||||
codec_thread_p, PRIORITY_REALTIME);
|
||||
voice_thread_set_priority(PRIORITY_REALTIME);
|
||||
codec_thread_priority = priority;
|
||||
thread_set_priority(codec_thread_p, priority);
|
||||
voice_thread_set_priority(priority);
|
||||
}
|
||||
}
|
||||
else if (codec_thread_priority != 0)
|
||||
else if (codec_thread_priority != PRIORITY_PLAYBACK)
|
||||
{
|
||||
thread_set_priority(codec_thread_p, codec_thread_priority);
|
||||
voice_thread_set_priority(codec_thread_priority);
|
||||
codec_thread_priority = 0;
|
||||
thread_set_priority(codec_thread_p, PRIORITY_PLAYBACK);
|
||||
voice_thread_set_priority(PRIORITY_PLAYBACK);
|
||||
codec_thread_priority = PRIORITY_PLAYBACK;
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_PRIORITY_SCHEDULING */
|
||||
|
@ -818,7 +821,7 @@ static bool prepare_insert(size_t length)
|
|||
if (low_latency_mode)
|
||||
{
|
||||
/* 1/4s latency. */
|
||||
if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 4
|
||||
if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 2
|
||||
&& pcm_is_playing())
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -2549,9 +2549,7 @@ void audio_init(void)
|
|||
to send messages. Thread creation will be delayed however so nothing
|
||||
starts running until ready if something yields such as talk_init. */
|
||||
queue_init(&audio_queue, true);
|
||||
queue_enable_queue_send(&audio_queue, &audio_queue_sender_list);
|
||||
queue_init(&codec_queue, false);
|
||||
queue_enable_queue_send(&codec_queue, &codec_queue_sender_list);
|
||||
queue_init(&pcmbuf_queue, false);
|
||||
|
||||
pcm_init();
|
||||
|
@ -2587,11 +2585,17 @@ void audio_init(void)
|
|||
codec_thread_name IF_PRIO(, PRIORITY_PLAYBACK)
|
||||
IF_COP(, CPU));
|
||||
|
||||
queue_enable_queue_send(&codec_queue, &codec_queue_sender_list,
|
||||
codec_thread_p);
|
||||
|
||||
audio_thread_p = create_thread(audio_thread, audio_stack,
|
||||
sizeof(audio_stack), CREATE_THREAD_FROZEN,
|
||||
audio_thread_name IF_PRIO(, PRIORITY_SYSTEM)
|
||||
audio_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE)
|
||||
IF_COP(, CPU));
|
||||
|
||||
queue_enable_queue_send(&audio_queue, &audio_queue_sender_list,
|
||||
audio_thread_p);
|
||||
|
||||
#ifdef PLAYBACK_VOICE
|
||||
voice_thread_init();
|
||||
#endif
|
||||
|
|
|
@ -253,15 +253,12 @@ static const struct plugin_api rockbox_api = {
|
|||
/* kernel/ system */
|
||||
PREFIX(sleep),
|
||||
yield,
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
priority_yield,
|
||||
#endif
|
||||
¤t_tick,
|
||||
default_event_handler,
|
||||
default_event_handler_ex,
|
||||
threads,
|
||||
create_thread,
|
||||
remove_thread,
|
||||
thread_exit,
|
||||
thread_wait,
|
||||
#if (CONFIG_CODEC == SWCODEC)
|
||||
mutex_init,
|
||||
|
|
|
@ -119,12 +119,12 @@
|
|||
#define PLUGIN_MAGIC 0x526F634B /* RocK */
|
||||
|
||||
/* increase this every time the api struct changes */
|
||||
#define PLUGIN_API_VERSION 100
|
||||
#define PLUGIN_API_VERSION 101
|
||||
|
||||
/* update this to latest version if a change to the api struct breaks
|
||||
backwards compatibility (and please take the opportunity to sort in any
|
||||
new function which are "waiting" at the end of the function table) */
|
||||
#define PLUGIN_MIN_API_VERSION 100
|
||||
#define PLUGIN_MIN_API_VERSION 101
|
||||
|
||||
/* plugin return codes */
|
||||
enum plugin_status {
|
||||
|
@ -351,19 +351,16 @@ struct plugin_api {
|
|||
/* kernel/ system */
|
||||
void (*PREFIX(sleep))(int ticks);
|
||||
void (*yield)(void);
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
void (*priority_yield)(void);
|
||||
#endif
|
||||
volatile long* current_tick;
|
||||
long (*default_event_handler)(long event);
|
||||
long (*default_event_handler_ex)(long event, void (*callback)(void *), void *parameter);
|
||||
struct thread_entry* threads;
|
||||
struct thread_entry* (*create_thread)(void (*function)(void), void* stack,
|
||||
int stack_size, unsigned flags,
|
||||
size_t stack_size, unsigned flags,
|
||||
const char *name
|
||||
IF_PRIO(, int priority)
|
||||
IF_COP(, unsigned int core));
|
||||
void (*remove_thread)(struct thread_entry *thread);
|
||||
void (*thread_exit)(void);
|
||||
void (*thread_wait)(struct thread_entry *thread);
|
||||
#if CONFIG_CODEC == SWCODEC
|
||||
void (*mutex_init)(struct mutex *m);
|
||||
|
@ -405,7 +402,8 @@ struct plugin_api {
|
|||
int ticks);
|
||||
#if CONFIG_CODEC == SWCODEC
|
||||
void (*queue_enable_queue_send)(struct event_queue *q,
|
||||
struct queue_sender_list *send);
|
||||
struct queue_sender_list *send,
|
||||
struct thread_entry *owner);
|
||||
bool (*queue_empty)(const struct event_queue *q);
|
||||
void (*queue_wait)(struct event_queue *q, struct queue_event *ev);
|
||||
intptr_t (*queue_send)(struct event_queue *q, long id,
|
||||
|
|
|
@ -714,12 +714,14 @@ bool audio_thread_init(void)
|
|||
/* Start the audio thread */
|
||||
audio_str.hdr.q = &audio_str_queue;
|
||||
rb->queue_init(audio_str.hdr.q, false);
|
||||
rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send);
|
||||
|
||||
/* One-up on the priority since the core DSP over-yields internally */
|
||||
audio_str.thread = rb->create_thread(
|
||||
audio_thread, audio_stack, audio_stack_size, 0,
|
||||
"mpgaudio" IF_PRIO(,PRIORITY_PLAYBACK-1) IF_COP(, CPU));
|
||||
"mpgaudio" IF_PRIO(,PRIORITY_PLAYBACK-4) IF_COP(, CPU));
|
||||
|
||||
rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
|
||||
audio_str.thread);
|
||||
|
||||
if (audio_str.thread == NULL)
|
||||
return false;
|
||||
|
|
|
@ -837,7 +837,6 @@ bool disk_buf_init(void)
|
|||
|
||||
disk_buf.q = &disk_buf_queue;
|
||||
rb->queue_init(disk_buf.q, false);
|
||||
rb->queue_enable_queue_send(disk_buf.q, &disk_buf_queue_send);
|
||||
|
||||
disk_buf.state = TSTATE_EOS;
|
||||
disk_buf.status = STREAM_STOPPED;
|
||||
|
@ -886,6 +885,9 @@ bool disk_buf_init(void)
|
|||
disk_buf_thread, disk_buf_stack, sizeof(disk_buf_stack), 0,
|
||||
"mpgbuffer" IF_PRIO(, PRIORITY_BUFFERING) IF_COP(, CPU));
|
||||
|
||||
rb->queue_enable_queue_send(disk_buf.q, &disk_buf_queue_send,
|
||||
disk_buf.thread);
|
||||
|
||||
if (disk_buf.thread == NULL)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -987,7 +987,6 @@ int stream_init(void)
|
|||
|
||||
stream_mgr.q = &stream_mgr_queue;
|
||||
rb->queue_init(stream_mgr.q, false);
|
||||
rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send);
|
||||
|
||||
/* sets audiosize and returns buffer pointer */
|
||||
mem = rb->plugin_get_audio_buffer(&memsize);
|
||||
|
@ -1028,6 +1027,9 @@ int stream_init(void)
|
|||
stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack),
|
||||
0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
|
||||
|
||||
rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send,
|
||||
stream_mgr.thread);
|
||||
|
||||
if (stream_mgr.thread == NULL)
|
||||
{
|
||||
rb->splash(HZ, "Could not create stream manager thread!");
|
||||
|
|
|
@ -955,7 +955,7 @@ static void video_thread(void)
|
|||
else
|
||||
{
|
||||
/* Just a little left - spin and be accurate */
|
||||
rb->priority_yield();
|
||||
rb->yield();
|
||||
if (str_have_msg(&video_str))
|
||||
goto message_wait;
|
||||
}
|
||||
|
@ -998,13 +998,15 @@ bool video_thread_init(void)
|
|||
|
||||
video_str.hdr.q = &video_str_queue;
|
||||
rb->queue_init(video_str.hdr.q, false);
|
||||
rb->queue_enable_queue_send(video_str.hdr.q, &video_str_queue_send);
|
||||
|
||||
/* We put the video thread on another processor for multi-core targets. */
|
||||
video_str.thread = rb->create_thread(
|
||||
video_thread, video_stack, VIDEO_STACKSIZE, 0,
|
||||
"mpgvideo" IF_PRIO(,PRIORITY_PLAYBACK) IF_COP(, COP));
|
||||
|
||||
rb->queue_enable_queue_send(video_str.hdr.q, &video_str_queue_send,
|
||||
video_str.thread);
|
||||
|
||||
if (video_str.thread == NULL)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -424,12 +424,14 @@ void voice_thread_init(void)
|
|||
{
|
||||
logf("Starting voice thread");
|
||||
queue_init(&voice_queue, false);
|
||||
queue_enable_queue_send(&voice_queue, &voice_queue_sender_list);
|
||||
mutex_init(&voice_mutex);
|
||||
event_init(&voice_event, STATE_SIGNALED | EVENT_MANUAL);
|
||||
voice_thread_p = create_thread(voice_thread, voice_stack,
|
||||
sizeof(voice_stack), CREATE_THREAD_FROZEN,
|
||||
voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
|
||||
|
||||
queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
|
||||
voice_thread_p);
|
||||
} /* voice_thread_init */
|
||||
|
||||
/* Unfreeze the voice thread */
|
||||
|
|
|
@ -9,11 +9,11 @@ usb.c
|
|||
#ifdef ROCKBOX_HAS_LOGF
|
||||
logf.c
|
||||
#endif /* ROCKBOX_HAS_LOGF */
|
||||
kernel.c
|
||||
#ifndef SIMULATOR
|
||||
#ifdef RB_PROFILE
|
||||
profile.c
|
||||
#endif /* RB_PROFILE */
|
||||
kernel.c
|
||||
rolo.c
|
||||
thread.c
|
||||
timer.c
|
||||
|
@ -274,6 +274,10 @@ target/sh/archos/descramble.S
|
|||
|
||||
#ifndef SIMULATOR
|
||||
target/coldfire/crt0.S
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
common/ffs.c
|
||||
target/coldfire/ffs-coldfire.S
|
||||
#endif
|
||||
target/coldfire/memcpy-coldfire.S
|
||||
target/coldfire/memmove-coldfire.S
|
||||
target/coldfire/memset-coldfire.S
|
||||
|
@ -299,6 +303,9 @@ common/strlen.c
|
|||
#ifndef SIMULATOR
|
||||
target/arm/memset-arm.S
|
||||
target/arm/memset16-arm.S
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
target/arm/ffs-arm.S
|
||||
#endif
|
||||
#if CONFIG_I2C == I2C_PP5024 || CONFIG_I2C == I2C_PP5020 || CONFIG_I2C == I2C_PP5002
|
||||
target/arm/i2c-pp.c
|
||||
#elif CONFIG_I2C == I2C_PNX0101
|
||||
|
@ -345,6 +352,9 @@ target/arm/crt0.S
|
|||
|
||||
#else
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
common/ffs.c
|
||||
#endif
|
||||
common/memcpy.c
|
||||
common/memmove.c
|
||||
common/memset.c
|
||||
|
|
54
firmware/common/ffs.c
Normal file
54
firmware/common/ffs.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2008 by Michael Sevakis
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
#include "config.h"
|
||||
#include <inttypes.h>
|
||||
|
||||
/* find_first_set_bit() - this is a C version of the ffs algorithm devised
|
||||
* by D.Seal and posted to comp.sys.arm on 16 Feb 1994.
|
||||
*
|
||||
* Find the index of the least significant set bit in the word.
|
||||
* return values:
|
||||
* 0 - bit 0 is set
|
||||
* 1 - bit 1 is set
|
||||
* ...
|
||||
* 31 - bit 31 is set
|
||||
* 32 - no bits set
|
||||
*/
|
||||
|
||||
/* Table shared with assembly code */
|
||||
const uint8_t L_ffs_table[64] ICONST_ATTR =
|
||||
{
|
||||
/* 0 1 2 3 4 5 6 7 */
|
||||
/* ----------------------------------------- */
|
||||
32, 0, 1, 12, 2, 6, 0, 13, /* 0- 7 */
|
||||
3, 0, 7, 0, 0, 0, 0, 14, /* 8-15 */
|
||||
10, 4, 0, 0, 8, 0, 0, 25, /* 16-23 */
|
||||
0, 0, 0, 0, 0, 21, 27, 15, /* 24-31 */
|
||||
31, 11, 5, 0, 0, 0, 0, 0, /* 32-39 */
|
||||
9, 0, 0, 24, 0, 0, 20, 26, /* 40-47 */
|
||||
30, 0, 0, 0, 0, 23, 0, 19, /* 48-55 */
|
||||
29, 0, 22, 18, 28, 17, 16, 0, /* 56-63 */
|
||||
};
|
||||
|
||||
#if !defined(CPU_COLDFIRE)
|
||||
int find_first_set_bit(uint32_t val)
|
||||
{
|
||||
return L_ffs_table[((val & -val)*0x0450fbaf) >> 26];
|
||||
}
|
||||
#endif
|
|
@ -95,52 +95,6 @@ static unsigned short identify_info[SECTOR_SIZE/2];
|
|||
|
||||
#ifdef MAX_PHYS_SECTOR_SIZE
|
||||
|
||||
/** This is temporary **/
|
||||
/* Define the mutex functions to use the special hack object */
|
||||
#define mutex_init ata_spin_init
|
||||
#define mutex_lock ata_spin_lock
|
||||
#define mutex_unlock ata_spin_unlock
|
||||
|
||||
void ata_spin_init(struct mutex *m)
|
||||
{
|
||||
m->thread = NULL;
|
||||
m->locked = 0;
|
||||
m->count = 0;
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
corelock_init(&m->cl);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ata_spin_lock(struct mutex *m)
|
||||
{
|
||||
struct thread_entry *current = thread_get_current();
|
||||
|
||||
if (current == m->thread)
|
||||
{
|
||||
m->count++;
|
||||
return;
|
||||
}
|
||||
|
||||
while (test_and_set(&m->locked, 1, &m->cl))
|
||||
yield();
|
||||
|
||||
m->thread = current;
|
||||
}
|
||||
|
||||
void ata_spin_unlock(struct mutex *m)
|
||||
{
|
||||
if (m->count > 0)
|
||||
{
|
||||
m->count--;
|
||||
return;
|
||||
}
|
||||
|
||||
m->thread = NULL;
|
||||
test_and_set(&m->locked, 0, &m->cl);
|
||||
}
|
||||
|
||||
/****/
|
||||
|
||||
struct sector_cache_entry {
|
||||
bool inuse;
|
||||
unsigned long sectornum; /* logical sector */
|
||||
|
@ -163,7 +117,7 @@ STATICIRAM int wait_for_bsy(void)
|
|||
long timeout = current_tick + HZ*30;
|
||||
while (TIME_BEFORE(current_tick, timeout) && (ATA_STATUS & STATUS_BSY)) {
|
||||
last_disk_activity = current_tick;
|
||||
priority_yield();
|
||||
yield();
|
||||
}
|
||||
|
||||
if (TIME_BEFORE(current_tick, timeout))
|
||||
|
@ -185,7 +139,7 @@ STATICIRAM int wait_for_rdy(void)
|
|||
while (TIME_BEFORE(current_tick, timeout) &&
|
||||
!(ATA_ALT_STATUS & STATUS_RDY)) {
|
||||
last_disk_activity = current_tick;
|
||||
priority_yield();
|
||||
yield();
|
||||
}
|
||||
|
||||
if (TIME_BEFORE(current_tick, timeout))
|
||||
|
|
|
@ -259,6 +259,12 @@ void fat_init(void)
|
|||
mutex_init(&cache_mutex);
|
||||
}
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
/* Disable this because it is dangerous due to the assumption that
|
||||
* mutex_unlock won't yield */
|
||||
mutex_set_preempt(&cache_mutex, false);
|
||||
#endif
|
||||
|
||||
/* mark the FAT cache as unused */
|
||||
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
||||
{
|
||||
|
|
|
@ -371,10 +371,20 @@
|
|||
#endif
|
||||
|
||||
/* define for all cpus from ARM family */
|
||||
#if defined(CPU_PP) || (CONFIG_CPU == PNX0101) || (CONFIG_CPU == S3C2440) \
|
||||
|| (CONFIG_CPU == DSC25) || (CONFIG_CPU == IMX31L) || (CONFIG_CPU == DM320) \
|
||||
|| defined(CPU_TCC77X) || defined(CPU_TCC780X)
|
||||
#if (CONFIG_CPU == IMX31L)
|
||||
#define CPU_ARM
|
||||
#define ARM_ARCH 6 /* ARMv6 */
|
||||
#endif
|
||||
|
||||
#if defined(CPU_TCC77X) || defined(CPU_TCC780X)
|
||||
#define CPU_ARM
|
||||
#define ARM_ARCH 5 /* ARMv5 */
|
||||
#endif
|
||||
|
||||
#if defined(CPU_PP) || (CONFIG_CPU == PNX0101) || (CONFIG_CPU == S3C2440) \
|
||||
|| (CONFIG_CPU == DSC25) || (CONFIG_CPU == DM320)
|
||||
#define CPU_ARM
|
||||
#define ARM_ARCH 4 /* ARMv4 */
|
||||
#endif
|
||||
|
||||
/* Determine if accesses should be strictly long aligned. */
|
||||
|
|
|
@ -76,6 +76,8 @@
|
|||
#define SYS_SCREENDUMP MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 0)
|
||||
#define SYS_CAR_ADAPTER_RESUME MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 1)
|
||||
|
||||
#define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)
|
||||
|
||||
struct queue_event
|
||||
{
|
||||
long id;
|
||||
|
@ -87,68 +89,92 @@ struct queue_sender_list
|
|||
{
|
||||
/* If non-NULL, there is a thread waiting for the corresponding event */
|
||||
/* Must be statically allocated to put in non-cached ram. */
|
||||
struct thread_entry *senders[QUEUE_LENGTH];
|
||||
struct thread_entry *senders[QUEUE_LENGTH]; /* message->thread map */
|
||||
struct thread_entry *list; /* list of senders in map */
|
||||
/* Send info for last message dequeued or NULL if replied or not sent */
|
||||
struct thread_entry *curr_sender;
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
struct blocker blocker;
|
||||
#endif
|
||||
};
|
||||
#endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
#define QUEUE_GET_THREAD(q) \
|
||||
(((q)->send == NULL) ? NULL : (q)->send->blocker.thread)
|
||||
#else
|
||||
/* Queue without priority enabled have no owner provision _at this time_ */
|
||||
#define QUEUE_GET_THREAD(q) \
|
||||
(NULL)
|
||||
#endif
|
||||
|
||||
struct event_queue
|
||||
{
|
||||
struct thread_queue queue; /* Waiter list */
|
||||
struct thread_entry *queue; /* waiter list */
|
||||
struct queue_event events[QUEUE_LENGTH]; /* list of events */
|
||||
unsigned int read; /* head of queue */
|
||||
unsigned int write; /* tail of queue */
|
||||
unsigned int read; /* head of queue */
|
||||
unsigned int write; /* tail of queue */
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
struct queue_sender_list *send; /* list of threads waiting for
|
||||
reply to an event */
|
||||
struct queue_sender_list *send; /* list of threads waiting for
|
||||
reply to an event */
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
struct blocker *blocker_p; /* priority inheritance info
|
||||
for sync message senders */
|
||||
#endif
|
||||
#if NUM_CORES > 1
|
||||
struct corelock cl; /* inter-core sync */
|
||||
#endif
|
||||
IF_COP( struct corelock cl; ) /* multiprocessor sync */
|
||||
};
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
#define MUTEX_SET_THREAD(m, t) ((m)->blocker.thread = (t))
|
||||
#define MUTEX_GET_THREAD(m) ((m)->blocker.thread)
|
||||
#else
|
||||
#define MUTEX_SET_THREAD(m, t) ((m)->thread = (t))
|
||||
#define MUTEX_GET_THREAD(m) ((m)->thread)
|
||||
#endif
|
||||
|
||||
struct mutex
|
||||
{
|
||||
struct thread_entry *queue; /* Waiter list */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock cl; /* inter-core sync */
|
||||
struct thread_entry *queue; /* waiter list */
|
||||
int count; /* lock owner recursion count */
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
struct blocker blocker; /* priority inheritance info
|
||||
for waiters */
|
||||
bool no_preempt; /* don't allow higher-priority thread
|
||||
to be scheduled even if woken */
|
||||
#else
|
||||
struct thread_entry *thread;
|
||||
#endif
|
||||
struct thread_entry *thread; /* thread that owns lock */
|
||||
int count; /* lock owner recursion count */
|
||||
unsigned char locked; /* locked semaphore */
|
||||
IF_COP( struct corelock cl; ) /* multiprocessor sync */
|
||||
unsigned char locked; /* locked semaphore */
|
||||
};
|
||||
|
||||
#if NUM_CORES > 1
|
||||
struct spinlock
|
||||
{
|
||||
struct corelock cl; /* inter-core sync */
|
||||
struct thread_entry *thread; /* lock owner */
|
||||
int count; /* lock owner recursion count */
|
||||
struct thread_entry *thread; /* lock owner */
|
||||
int count; /* lock owner recursion count */
|
||||
struct corelock cl; /* multiprocessor sync */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SEMAPHORE_OBJECTS
|
||||
struct semaphore
|
||||
{
|
||||
struct thread_entry *queue; /* Waiter list */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock cl; /* inter-core sync */
|
||||
#endif
|
||||
int count; /* # of waits remaining before unsignaled */
|
||||
int max; /* maximum # of waits to remain signaled */
|
||||
struct thread_entry *queue; /* Waiter list */
|
||||
int count; /* # of waits remaining before unsignaled */
|
||||
int max; /* maximum # of waits to remain signaled */
|
||||
IF_COP( struct corelock cl; ) /* multiprocessor sync */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_EVENT_OBJECTS
|
||||
struct event
|
||||
{
|
||||
struct thread_entry *queues[2]; /* waiters for each state */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock cl; /* inter-core sync */
|
||||
#endif
|
||||
unsigned char automatic; /* event performs auto-reset */
|
||||
unsigned char state; /* state: 1 = signaled */
|
||||
struct thread_entry *queues[2]; /* waiters for each state */
|
||||
unsigned char automatic; /* event performs auto-reset */
|
||||
unsigned char state; /* state: 1 = signaled */
|
||||
IF_COP( struct corelock cl; ) /* multiprocessor sync */
|
||||
};
|
||||
#endif
|
||||
|
||||
|
@ -208,7 +234,9 @@ extern void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev,
|
|||
int ticks);
|
||||
extern void queue_post(struct event_queue *q, long id, intptr_t data);
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
extern void queue_enable_queue_send(struct event_queue *q, struct queue_sender_list *send);
|
||||
extern void queue_enable_queue_send(struct event_queue *q,
|
||||
struct queue_sender_list *send,
|
||||
struct thread_entry *owner);
|
||||
extern intptr_t queue_send(struct event_queue *q, long id, intptr_t data);
|
||||
extern void queue_reply(struct event_queue *q, intptr_t retval);
|
||||
extern bool queue_in_queue_send(struct event_queue *q);
|
||||
|
@ -223,6 +251,11 @@ extern int queue_broadcast(long id, intptr_t data);
|
|||
extern void mutex_init(struct mutex *m);
|
||||
extern void mutex_lock(struct mutex *m);
|
||||
extern void mutex_unlock(struct mutex *m);
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
/* Temporary function to disable mutex preempting a thread on unlock */
|
||||
static inline void mutex_set_preempt(struct mutex *m, bool preempt)
|
||||
{ m->no_preempt = !preempt; }
|
||||
#endif
|
||||
#if NUM_CORES > 1
|
||||
extern void spinlock_init(struct spinlock *l);
|
||||
extern void spinlock_lock(struct spinlock *l);
|
||||
|
@ -240,6 +273,5 @@ extern void event_init(struct event *e, unsigned int flags);
|
|||
extern void event_wait(struct event *e, unsigned int for_state);
|
||||
extern void event_set_state(struct event *e, unsigned int state);
|
||||
#endif /* HAVE_EVENT_OBJECTS */
|
||||
#define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)
|
||||
|
||||
#endif /* _KERNEL_H_ */
|
||||
|
|
|
@ -159,6 +159,20 @@ int get_cpu_boost_counter(void);
|
|||
#define H_TO_BE32(x) (x)
|
||||
#endif
|
||||
|
||||
/* Get the byte offset of a type's member */
|
||||
#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
|
||||
|
||||
/* Get the type pointer from one of its members */
|
||||
#define TYPE_FROM_MEMBER(type, memberptr, membername) \
|
||||
((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
|
||||
|
||||
/* returns index of first set bit + 1 or 0 if no bits are set */
|
||||
int find_first_set_bit(uint32_t val);
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
uint32_t isolate_first_bit(uint32_t val)
|
||||
{ return val & -val; }
|
||||
|
||||
/* gcc 3.4 changed the format of the constraints */
|
||||
#if (__GNUC__ >= 3) && (__GNUC_MINOR__ > 3) || (__GNUC__ >= 4)
|
||||
#define I_CONSTRAINT "I08"
|
||||
|
|
|
@ -26,21 +26,35 @@
|
|||
|
||||
/* Priority scheduling (when enabled with HAVE_PRIORITY_SCHEDULING) works
|
||||
* by giving high priority threads more CPU time than less priority threads
|
||||
* when they need it.
|
||||
* when they need it. Priority is differential such that the priority
|
||||
* difference between a lower priority runnable thread and the highest priority
|
||||
* runnable thread determines the amount of aging nescessary for the lower
|
||||
* priority thread to be scheduled in order to prevent starvation.
|
||||
*
|
||||
* If software playback codec pcm buffer is going down to critical, codec
|
||||
* can change it own priority to REALTIME to override user interface and
|
||||
* can gradually raise its own priority to override user interface and
|
||||
* prevent playback skipping.
|
||||
*/
|
||||
#define PRIORITY_RESERVED_HIGH 0 /* Reserved */
|
||||
#define PRIORITY_RESERVED_LOW 32 /* Reserved */
|
||||
#define HIGHEST_PRIORITY 1 /* The highest possible thread priority */
|
||||
#define LOWEST_PRIORITY 100 /* The lowest possible thread priority */
|
||||
#define PRIORITY_REALTIME 1
|
||||
#define PRIORITY_USER_INTERFACE 4 /* The main thread */
|
||||
#define PRIORITY_RECORDING 4 /* Recording thread */
|
||||
#define PRIORITY_PLAYBACK 4 /* or REALTIME when needed */
|
||||
#define PRIORITY_BUFFERING 4 /* Codec buffering thread */
|
||||
#define PRIORITY_SYSTEM 6 /* All other firmware threads */
|
||||
#define PRIORITY_BACKGROUND 8 /* Normal application threads */
|
||||
#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 */
|
||||
#define PRIORITY_USER_INTERFACE 16 /* The main thread */
|
||||
#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_BUFFERING 16 /* Codec buffering thread */
|
||||
#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 */
|
||||
|
||||
/* TODO: Only a minor tweak to create_thread would be needed to let
|
||||
* thread slots be caller allocated - no essential threading functionality
|
||||
|
@ -59,80 +73,40 @@
|
|||
|
||||
#define DEFAULT_STACK_SIZE 0x400 /* Bytes */
|
||||
|
||||
/**
|
||||
* "Busy" values that can be swapped into a variable to indicate
|
||||
* that the variable or object pointed to is in use by another processor
|
||||
* core. When accessed, the busy value is swapped-in while the current
|
||||
* value is atomically returned. If the swap returns the busy value,
|
||||
* the processor should retry the operation until some other value is
|
||||
* returned. When modification is finished, the new value should be
|
||||
* written which unlocks it and updates it atomically.
|
||||
*
|
||||
* Procedure:
|
||||
* while ((curr_value = swap(&variable, BUSY_VALUE)) == BUSY_VALUE);
|
||||
*
|
||||
* Modify/examine object at mem location or variable. Create "new_value"
|
||||
* as suitable.
|
||||
*
|
||||
* variable = new_value or curr_value;
|
||||
*
|
||||
* To check a value for busy and perform an operation if not:
|
||||
* curr_value = swap(&variable, BUSY_VALUE);
|
||||
*
|
||||
* if (curr_value != BUSY_VALUE)
|
||||
* {
|
||||
* Modify/examine object at mem location or variable. Create "new_value"
|
||||
* as suitable.
|
||||
* variable = new_value or curr_value;
|
||||
* }
|
||||
* else
|
||||
* {
|
||||
* Do nothing - already busy
|
||||
* }
|
||||
*
|
||||
* Only ever restore when an actual value is returned or else it could leave
|
||||
* the variable locked permanently if another processor unlocked in the
|
||||
* meantime. The next access attempt would deadlock for all processors since
|
||||
* an abandoned busy status would be left behind.
|
||||
*/
|
||||
#define STATE_BUSYuptr ((void*)UINTPTR_MAX)
|
||||
#define STATE_BUSYu8 UINT8_MAX
|
||||
#define STATE_BUSYi INT_MIN
|
||||
|
||||
#ifndef SIMULATOR
|
||||
/* Need to keep structures inside the header file because debug_menu
|
||||
* needs them. */
|
||||
#ifdef CPU_COLDFIRE
|
||||
struct regs
|
||||
{
|
||||
unsigned int macsr; /* 0 - EMAC status register */
|
||||
unsigned int d[6]; /* 4-24 - d2-d7 */
|
||||
unsigned int a[5]; /* 28-44 - a2-a6 */
|
||||
void *sp; /* 48 - Stack pointer (a7) */
|
||||
void *start; /* 52 - Thread start address, or NULL when started */
|
||||
uint32_t macsr; /* 0 - EMAC status register */
|
||||
uint32_t d[6]; /* 4-24 - d2-d7 */
|
||||
uint32_t a[5]; /* 28-44 - a2-a6 */
|
||||
uint32_t sp; /* 48 - Stack pointer (a7) */
|
||||
uint32_t start; /* 52 - Thread start address, or NULL when started */
|
||||
};
|
||||
#elif CONFIG_CPU == SH7034
|
||||
struct regs
|
||||
{
|
||||
unsigned int r[7]; /* 0-24 - Registers r8 thru r14 */
|
||||
void *sp; /* 28 - Stack pointer (r15) */
|
||||
void *pr; /* 32 - Procedure register */
|
||||
void *start; /* 36 - Thread start address, or NULL when started */
|
||||
uint32_t r[7]; /* 0-24 - Registers r8 thru r14 */
|
||||
uint32_t sp; /* 28 - Stack pointer (r15) */
|
||||
uint32_t pr; /* 32 - Procedure register */
|
||||
uint32_t start; /* 36 - Thread start address, or NULL when started */
|
||||
};
|
||||
#elif defined(CPU_ARM)
|
||||
struct regs
|
||||
{
|
||||
unsigned int r[8]; /* 0-28 - Registers r4-r11 */
|
||||
void *sp; /* 32 - Stack pointer (r13) */
|
||||
unsigned int lr; /* 36 - r14 (lr) */
|
||||
void *start; /* 40 - Thread start address, or NULL when started */
|
||||
uint32_t r[8]; /* 0-28 - Registers r4-r11 */
|
||||
uint32_t sp; /* 32 - Stack pointer (r13) */
|
||||
uint32_t lr; /* 36 - r14 (lr) */
|
||||
uint32_t start; /* 40 - Thread start address, or NULL when started */
|
||||
};
|
||||
#endif /* CONFIG_CPU */
|
||||
#else
|
||||
struct regs
|
||||
{
|
||||
void *t; /* Simulator OS thread */
|
||||
void *c; /* Condition for blocking and sync */
|
||||
void *s; /* Semaphore for blocking and wakeup */
|
||||
void (*start)(void); /* Start function */
|
||||
};
|
||||
#endif /* !SIMULATOR */
|
||||
|
@ -154,13 +128,13 @@ enum
|
|||
thread_thaw is called with its ID */
|
||||
THREAD_NUM_STATES,
|
||||
TIMEOUT_STATE_FIRST = STATE_SLEEPING,
|
||||
#if NUM_CORES > 1
|
||||
STATE_BUSY = STATE_BUSYu8, /* Thread slot is being examined */
|
||||
#endif
|
||||
};
|
||||
|
||||
#if NUM_CORES > 1
|
||||
#define THREAD_DESTRUCT ((const char *)0x84905617)
|
||||
/* Pointer value for name field to indicate thread is being killed. Using
|
||||
* an alternate STATE_* won't work since that would interfere with operation
|
||||
* while the thread is still running. */
|
||||
#define THREAD_DESTRUCT ((const char *)~(intptr_t)0)
|
||||
#endif
|
||||
|
||||
/* Link information for lists thread is in */
|
||||
|
@ -188,7 +162,7 @@ void corelock_unlock(struct corelock *cl);
|
|||
/* Use native atomic swap/exchange instruction */
|
||||
struct corelock
|
||||
{
|
||||
unsigned char locked;
|
||||
volatile unsigned char locked;
|
||||
} __attribute__((packed));
|
||||
|
||||
#define corelock_init(cl) \
|
||||
|
@ -207,15 +181,36 @@ struct corelock
|
|||
#define corelock_unlock(cl)
|
||||
#endif /* core locking selection */
|
||||
|
||||
struct thread_queue
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
struct blocker
|
||||
{
|
||||
struct thread_entry *queue; /* list of threads waiting -
|
||||
_must_ be first member */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock cl; /* lock for atomic list operations */
|
||||
#endif
|
||||
struct thread_entry *thread; /* thread blocking other threads
|
||||
(aka. object owner) */
|
||||
int priority; /* highest priority waiter */
|
||||
struct thread_entry * (*wakeup_protocol)(struct thread_entry *thread);
|
||||
};
|
||||
|
||||
/* Choices of wakeup protocol */
|
||||
|
||||
/* For transfer of object ownership by one thread to another thread by
|
||||
* the owning thread itself (mutexes) */
|
||||
struct thread_entry *
|
||||
wakeup_priority_protocol_transfer(struct thread_entry *thread);
|
||||
|
||||
/* For release by owner where ownership doesn't change - other threads,
|
||||
* interrupts, timeouts, etc. (mutex timeout, queues) */
|
||||
struct thread_entry *
|
||||
wakeup_priority_protocol_release(struct thread_entry *thread);
|
||||
|
||||
|
||||
struct priority_distribution
|
||||
{
|
||||
uint8_t hist[NUM_PRIORITIES]; /* Histogram: Frequency for each priority */
|
||||
uint32_t mask; /* Bitmask of hist entries that are not zero */
|
||||
};
|
||||
|
||||
#endif /* HAVE_PRIORITY_SCHEDULING */
|
||||
|
||||
/* Information kept in each thread slot
|
||||
* members are arranged according to size - largest first - in order
|
||||
* to ensure both alignment and packing at the same time.
|
||||
|
@ -224,88 +219,83 @@ struct thread_entry
|
|||
{
|
||||
struct regs context; /* Register context at switch -
|
||||
_must_ be first member */
|
||||
void *stack; /* Pointer to top of stack */
|
||||
uintptr_t *stack; /* Pointer to top of stack */
|
||||
const char *name; /* Thread name */
|
||||
long tmo_tick; /* Tick when thread should be woken from
|
||||
timeout */
|
||||
timeout -
|
||||
states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
|
||||
struct thread_list l; /* Links for blocked/waking/running -
|
||||
circular linkage in both directions */
|
||||
struct thread_list tmo; /* Links for timeout list -
|
||||
Self-pointer-terminated in reverse direction,
|
||||
NULL-terminated in forward direction */
|
||||
struct thread_queue *bqp; /* Pointer to list variable in kernel
|
||||
Circular in reverse direction, NULL-terminated in
|
||||
forward direction -
|
||||
states: STATE_SLEEPING/STATE_BLOCKED_W_TMO */
|
||||
struct thread_entry **bqp; /* Pointer to list variable in kernel
|
||||
object where thread is blocked - used
|
||||
for implicit unblock and explicit wake */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct thread_entry **bqnlp; /* Pointer to list variable in kernel
|
||||
object where thread is blocked - non-locked
|
||||
operations will be used */
|
||||
for implicit unblock and explicit wake
|
||||
states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
|
||||
#if NUM_CORES > 1
|
||||
struct corelock *obj_cl; /* Object corelock where thead is blocked -
|
||||
states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
|
||||
#endif
|
||||
struct thread_entry *queue; /* List of threads waiting for thread to be
|
||||
removed */
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
intptr_t retval; /* Return value from a blocked operation */
|
||||
#define HAVE_WAKEUP_EXT_CB
|
||||
void (*wakeup_ext_cb)(struct thread_entry *thread); /* Callback that
|
||||
performs special steps needed when being
|
||||
forced off of an object's wait queue that
|
||||
go beyond the standard wait queue removal
|
||||
and priority disinheritance */
|
||||
/* Only enabled when using queue_send for now */
|
||||
#endif
|
||||
#if defined(HAVE_EXTENDED_MESSAGING_AND_NAME) || NUM_CORES > 1
|
||||
intptr_t retval; /* Return value from a blocked operation/
|
||||
misc. use */
|
||||
#endif
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
long last_run; /* Last tick when started */
|
||||
/* Priority summary of owned objects that support inheritance */
|
||||
struct blocker *blocker; /* Pointer to blocker when this thread is blocked
|
||||
on an object that supports PIP -
|
||||
states: STATE_BLOCKED/STATE_BLOCKED_W_TMO */
|
||||
struct priority_distribution pdist; /* Priority summary of owned objects
|
||||
that have blocked threads and thread's own
|
||||
base priority */
|
||||
int skip_count; /* Number of times skipped if higher priority
|
||||
thread was running */
|
||||
#endif
|
||||
unsigned short stack_size; /* Size of stack in bytes */
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
unsigned char priority; /* Current priority */
|
||||
unsigned char priority_x; /* Inherited priority - right now just a
|
||||
runtime guarantee flag */
|
||||
unsigned char base_priority; /* Base priority (set explicitly during
|
||||
creation or thread_set_priority) */
|
||||
unsigned char priority; /* Scheduled priority (higher of base or
|
||||
all threads blocked by this one) */
|
||||
#endif
|
||||
unsigned char state; /* Thread slot state (STATE_*) */
|
||||
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
||||
unsigned char cpu_boost; /* CPU frequency boost flag */
|
||||
#endif
|
||||
#if NUM_CORES > 1
|
||||
unsigned char core; /* The core to which thread belongs */
|
||||
#endif
|
||||
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
||||
unsigned char boosted; /* CPU frequency boost flag */
|
||||
#endif
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock cl; /* Corelock to lock thread slot */
|
||||
struct corelock waiter_cl; /* Corelock for thread_wait */
|
||||
struct corelock slot_cl; /* Corelock to lock thread slot */
|
||||
#endif
|
||||
};
|
||||
|
||||
#if NUM_CORES > 1
|
||||
/* Operations to be performed just before stopping a thread and starting
|
||||
a new one if specified before calling switch_thread */
|
||||
#define TBOP_UNLOCK_LIST 0x01 /* Set a pointer variable address var_ptrp */
|
||||
#if CONFIG_CORELOCK == CORELOCK_SWAP
|
||||
#define TBOP_SET_VARi 0x02 /* Set an int at address var_ip */
|
||||
#define TBOP_SET_VARu8 0x03 /* Set an unsigned char at address var_u8p */
|
||||
#define TBOP_VAR_TYPE_MASK 0x03 /* Mask for variable type*/
|
||||
#endif /* CONFIG_CORELOCK */
|
||||
#define TBOP_UNLOCK_CORELOCK 0x04
|
||||
#define TBOP_UNLOCK_THREAD 0x08 /* Unlock a thread's slot */
|
||||
#define TBOP_UNLOCK_CURRENT 0x10 /* Unlock the current thread's slot */
|
||||
#define TBOP_SWITCH_CORE 0x20 /* Call the core switch preparation routine */
|
||||
enum
|
||||
{
|
||||
TBOP_CLEAR = 0, /* No operation to do */
|
||||
TBOP_UNLOCK_CORELOCK, /* Unlock a corelock variable */
|
||||
TBOP_SWITCH_CORE, /* Call the core switch preparation routine */
|
||||
};
|
||||
|
||||
struct thread_blk_ops
|
||||
{
|
||||
#if CONFIG_CORELOCK != SW_CORELOCK
|
||||
union
|
||||
{
|
||||
int var_iv; /* int variable value to set */
|
||||
uint8_t var_u8v; /* unsigned char valur to set */
|
||||
struct thread_entry *list_v; /* list pointer queue value to set */
|
||||
};
|
||||
#endif
|
||||
union
|
||||
{
|
||||
#if CONFIG_CORELOCK != SW_CORELOCK
|
||||
int *var_ip; /* pointer to int variable */
|
||||
uint8_t *var_u8p; /* pointer to unsigned char varuable */
|
||||
#endif
|
||||
struct thread_queue *list_p; /* pointer to list variable */
|
||||
};
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct corelock *cl_p; /* corelock to unlock */
|
||||
struct thread_entry *thread; /* thread to unlock */
|
||||
#elif CONFIG_CORELOCK == CORELOCK_SWAP
|
||||
unsigned char state; /* new thread state (performs unlock) */
|
||||
#endif /* SOFTWARE_CORELOCK */
|
||||
unsigned char flags; /* TBOP_* flags */
|
||||
struct corelock *cl_p; /* pointer to corelock */
|
||||
unsigned char flags; /* TBOP_* flags */
|
||||
};
|
||||
#endif /* NUM_CORES > 1 */
|
||||
|
||||
|
@ -316,28 +306,30 @@ struct core_entry
|
|||
{
|
||||
/* "Active" lists - core is constantly active on these and are never
|
||||
locked and interrupts do not access them */
|
||||
struct thread_entry *running; /* threads that are running */
|
||||
struct thread_entry *running; /* threads that are running (RTR) */
|
||||
struct thread_entry *timeout; /* threads that are on a timeout before
|
||||
running again */
|
||||
/* "Shared" lists - cores interact in a synchronized manner - access
|
||||
is locked between cores and interrupts */
|
||||
struct thread_queue waking; /* intermediate locked list that
|
||||
hold threads other core should wake up
|
||||
on next task switch */
|
||||
struct thread_entry *block_task; /* Task going off running list */
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
struct priority_distribution rtr; /* Summary of running and ready-to-run
|
||||
threads */
|
||||
#endif
|
||||
long next_tmo_check; /* soonest time to check tmo threads */
|
||||
#if NUM_CORES > 1
|
||||
struct thread_blk_ops blk_ops; /* operations to perform when
|
||||
blocking a thread */
|
||||
#endif /* NUM_CORES */
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
unsigned char highest_priority;
|
||||
struct corelock rtr_cl; /* Lock for rtr list */
|
||||
#endif
|
||||
#endif /* NUM_CORES */
|
||||
};
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
#define IF_PRIO(...) __VA_ARGS__
|
||||
#define IFN_PRIO(...)
|
||||
#else
|
||||
#define IF_PRIO(...)
|
||||
#define IFN_PRIO(...) __VA_ARGS__
|
||||
#endif
|
||||
|
||||
/* Macros generate better code than an inline function is this case */
|
||||
|
@ -464,13 +456,18 @@ struct core_entry
|
|||
void core_idle(void);
|
||||
void core_wake(IF_COP_VOID(unsigned int core));
|
||||
|
||||
/* Initialize the scheduler */
|
||||
void init_threads(void);
|
||||
|
||||
/* Allocate a thread in the scheduler */
|
||||
#define CREATE_THREAD_FROZEN 0x00000001 /* Thread is frozen at create time */
|
||||
struct thread_entry*
|
||||
create_thread(void (*function)(void), void* stack, int stack_size,
|
||||
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));
|
||||
|
||||
/* Set and clear the CPU frequency boost flag for the calling thread */
|
||||
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
||||
void trigger_cpu_boost(void);
|
||||
void cancel_cpu_boost(void);
|
||||
|
@ -478,86 +475,52 @@ void cancel_cpu_boost(void);
|
|||
#define trigger_cpu_boost()
|
||||
#define cancel_cpu_boost()
|
||||
#endif
|
||||
/* Make a frozed thread runnable (when started with CREATE_THREAD_FROZEN).
|
||||
* Has no effect on a thread not frozen. */
|
||||
void thread_thaw(struct thread_entry *thread);
|
||||
/* Wait for a thread to exit */
|
||||
void thread_wait(struct thread_entry *thread);
|
||||
/* Exit the current thread */
|
||||
void thread_exit(void);
|
||||
#if defined(DEBUG) || defined(ROCKBOX_HAS_LOGF)
|
||||
#define ALLOW_REMOVE_THREAD
|
||||
/* Remove a thread from the scheduler */
|
||||
void remove_thread(struct thread_entry *thread);
|
||||
void switch_thread(struct thread_entry *old);
|
||||
#endif
|
||||
|
||||
/* Switch to next runnable thread */
|
||||
void switch_thread(void);
|
||||
/* Blocks a thread for at least the specified number of ticks (0 = wait until
|
||||
* next tick) */
|
||||
void sleep_thread(int ticks);
|
||||
/* Indefinitely blocks the current thread on a thread queue */
|
||||
void block_thread(struct thread_entry *current);
|
||||
/* Blocks the current thread on a thread queue until explicitely woken or
|
||||
* the timeout is reached */
|
||||
void block_thread_w_tmo(struct thread_entry *current, int timeout);
|
||||
|
||||
/**
|
||||
* Setup to allow using thread queues as locked or non-locked without speed
|
||||
* sacrifices in both core locking types.
|
||||
*
|
||||
* The blocking/waking function inline two different version of the real
|
||||
* function into the stubs when a software or other separate core locking
|
||||
* mechanism is employed.
|
||||
*
|
||||
* When a simple test-and-set or similar instruction is available, locking
|
||||
* has no cost and so one version is used and the internal worker is called
|
||||
* directly.
|
||||
*
|
||||
* CORELOCK_NONE is treated the same as when an atomic instruction can be
|
||||
* used.
|
||||
*/
|
||||
/* Return bit flags for thread wakeup */
|
||||
#define THREAD_NONE 0x0 /* No thread woken up (exclusive) */
|
||||
#define THREAD_OK 0x1 /* A thread was woken up */
|
||||
#define THREAD_SWITCH 0x2 /* Task switch recommended (one or more of
|
||||
higher priority than current were woken) */
|
||||
|
||||
/* Blocks the current thread on a thread queue */
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
void block_thread(struct thread_queue *tq);
|
||||
void block_thread_no_listlock(struct thread_entry **list);
|
||||
#else
|
||||
void _block_thread(struct thread_queue *tq);
|
||||
static inline void block_thread(struct thread_queue *tq)
|
||||
{ _block_thread(tq); }
|
||||
static inline void block_thread_no_listlock(struct thread_entry **list)
|
||||
{ _block_thread((struct thread_queue *)list); }
|
||||
#endif /* CONFIG_CORELOCK */
|
||||
|
||||
/* Blocks the current thread on a thread queue for a max amount of time
|
||||
* There is no "_no_listlock" version because timeout blocks without sync on
|
||||
* the blocking queues is not permitted since either core could access the
|
||||
* list at any time to do an implicit wake. In other words, objects with
|
||||
* timeout support require lockable queues. */
|
||||
void block_thread_w_tmo(struct thread_queue *tq, int timeout);
|
||||
|
||||
/* Wakes up the thread at the head of the queue */
|
||||
#define THREAD_WAKEUP_NONE ((struct thread_entry *)NULL)
|
||||
#define THREAD_WAKEUP_MISSING ((struct thread_entry *)(NULL+1))
|
||||
#if CONFIG_CORELOCK == SW_CORELOCK
|
||||
struct thread_entry * wakeup_thread(struct thread_queue *tq);
|
||||
struct thread_entry * wakeup_thread_no_listlock(struct thread_entry **list);
|
||||
#else
|
||||
struct thread_entry * _wakeup_thread(struct thread_queue *list);
|
||||
static inline struct thread_entry * wakeup_thread(struct thread_queue *tq)
|
||||
{ return _wakeup_thread(tq); }
|
||||
static inline struct thread_entry * wakeup_thread_no_listlock(struct thread_entry **list)
|
||||
{ return _wakeup_thread((struct thread_queue *)list); }
|
||||
#endif /* CONFIG_CORELOCK */
|
||||
|
||||
/* Initialize a thread_queue object. */
|
||||
static inline void thread_queue_init(struct thread_queue *tq)
|
||||
{ tq->queue = NULL; IF_SWCL(corelock_init(&tq->cl);) }
|
||||
/* A convenience function for waking an entire queue of threads. */
|
||||
static inline void thread_queue_wake(struct thread_queue *tq)
|
||||
{ while (wakeup_thread(tq) != NULL); }
|
||||
/* The no-listlock version of thread_queue_wake() */
|
||||
static inline void thread_queue_wake_no_listlock(struct thread_entry **list)
|
||||
{ while (wakeup_thread_no_listlock(list) != NULL); }
|
||||
unsigned int thread_queue_wake(struct thread_entry **list);
|
||||
|
||||
/* Wakeup a thread at the head of a list */
|
||||
unsigned int wakeup_thread(struct thread_entry **list);
|
||||
|
||||
#ifdef HAVE_PRIORITY_SCHEDULING
|
||||
int thread_set_priority(struct thread_entry *thread, int priority);
|
||||
int thread_get_priority(struct thread_entry *thread);
|
||||
/* Yield that guarantees thread execution once per round regardless of
|
||||
thread's scheduler priority - basically a transient realtime boost
|
||||
without altering the scheduler's thread precedence. */
|
||||
void priority_yield(void);
|
||||
#else
|
||||
#define priority_yield yield
|
||||
#endif /* HAVE_PRIORITY_SCHEDULING */
|
||||
#if NUM_CORES > 1
|
||||
unsigned int switch_core(unsigned int new_core);
|
||||
#endif
|
||||
struct thread_entry * thread_get_current(void);
|
||||
void init_threads(void);
|
||||
|
||||
/* Debugging info - only! */
|
||||
int thread_stack_usage(const struct thread_entry *thread);
|
||||
#if NUM_CORES > 1
|
||||
int idle_stack_usage(unsigned int core);
|
||||
|
|
1505
firmware/kernel.c
1505
firmware/kernel.c
File diff suppressed because it is too large
Load diff
|
@ -361,11 +361,12 @@ unsigned long pcm_rec_sample_rate(void)
|
|||
void pcm_rec_init(void)
|
||||
{
|
||||
queue_init(&pcmrec_queue, true);
|
||||
queue_enable_queue_send(&pcmrec_queue, &pcmrec_queue_send);
|
||||
pcmrec_thread_p =
|
||||
create_thread(pcmrec_thread, pcmrec_stack, sizeof(pcmrec_stack),
|
||||
0, pcmrec_thread_name IF_PRIO(, PRIORITY_RECORDING)
|
||||
IF_COP(, CPU));
|
||||
queue_enable_queue_send(&pcmrec_queue, &pcmrec_queue_send,
|
||||
pcmrec_thread_p);
|
||||
} /* pcm_rec_init */
|
||||
|
||||
/** audio_* group **/
|
||||
|
@ -874,9 +875,9 @@ static void pcmrec_flush(unsigned flush_num)
|
|||
logf("pcmrec: boost (%s)",
|
||||
num >= flood_watermark ? "num" : "time");
|
||||
prio_pcmrec = thread_set_priority(NULL,
|
||||
thread_get_priority(NULL) - 1);
|
||||
thread_get_priority(NULL) - 4);
|
||||
prio_codec = thread_set_priority(codec_thread_p,
|
||||
thread_get_priority(codec_thread_p) - 1);
|
||||
thread_get_priority(codec_thread_p) - 4);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
74
firmware/target/arm/ffs-arm.S
Normal file
74
firmware/target/arm/ffs-arm.S
Normal file
|
@ -0,0 +1,74 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2008 by Michael Sevakis
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
#include "config.h"
|
||||
|
||||
/****************************************************************************
|
||||
* int find_first_set_bit(uint32_t val);
|
||||
*
|
||||
* Find the index of the least significant set bit in the 32-bit word.
|
||||
*
|
||||
* return values:
|
||||
* 0 - bit 0 is set
|
||||
* 1 - bit 1 is set
|
||||
* ...
|
||||
* 31 - bit 31 is set
|
||||
* 32 - no bits set
|
||||
****************************************************************************/
|
||||
.align 2
|
||||
.global find_first_set_bit
|
||||
.type find_first_set_bit,%function
|
||||
find_first_set_bit:
|
||||
@ Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry
|
||||
rsb r2, r0, #0 @ r1 = r0 & -r0
|
||||
ands r1, r0, r2 @
|
||||
|
||||
@ now r1 has at most one set bit, call this X
|
||||
|
||||
#if ARM_ARCH >= 5
|
||||
clz r0, r1 @ Get lead 0's count
|
||||
rsbne r0, r0, #31 @ lead 0's -> bit index
|
||||
bx lr @
|
||||
#else
|
||||
@ this is the ffs algorithm devised by D.Seal and posted to
|
||||
@ comp.sys.arm on 16 Feb 1994.
|
||||
@
|
||||
@ Output modified to suit Rockbox purposes.
|
||||
|
||||
adr r2, L_ffs_table
|
||||
orrne r1, r1, r1, lsl #4 @ r1 = X * 0x11
|
||||
orrne r1, r1, r1, lsl #6 @ r1 = X * 0x451
|
||||
rsbne r1, r1, r1, lsl #16 @ r1 = X * 0x0450fbaf
|
||||
|
||||
@ now lookup in table indexed on top 6 bits of r1
|
||||
ldrb r0, [ r2, r1, lsr #26 ] @
|
||||
bx lr @
|
||||
|
||||
L_ffs_table:
|
||||
@ 0 1 2 3 4 5 6 7
|
||||
@----------------------------------------------
|
||||
.byte 32, 0, 1, 12, 2, 6, 0, 13 @ 0- 7
|
||||
.byte 3, 0, 7, 0, 0, 0, 0, 14 @ 8-15
|
||||
.byte 10, 4, 0, 0, 8, 0, 0, 25 @ 16-23
|
||||
.byte 0, 0, 0, 0, 0, 21, 27, 15 @ 24-31
|
||||
.byte 31, 11, 5, 0, 0, 0, 0, 0 @ 32-39
|
||||
.byte 9, 0, 0, 24, 0, 0, 20, 26 @ 40-47
|
||||
.byte 30, 0, 0, 0, 0, 23, 0, 19 @ 48-55
|
||||
.byte 29, 0, 22, 18, 28, 17, 16, 0 @ 56-63
|
||||
#endif
|
||||
.size find_first_set_bit, .-find_first_set_bit
|
|
@ -45,7 +45,7 @@ static int pp_i2c_wait_not_busy(void)
|
|||
if (!(I2C_STATUS & I2C_BUSY)) {
|
||||
return 0;
|
||||
}
|
||||
priority_yield();
|
||||
yield();
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
|
|
@ -128,7 +128,7 @@ void copy_read_sectors(unsigned char* buf, int wordcount)
|
|||
|
||||
/* Wait for transfer to complete */
|
||||
while((DSTAT0 & 0x000fffff))
|
||||
priority_yield();
|
||||
yield();
|
||||
/* Dump cache for the buffer */
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -198,7 +198,7 @@ static bool sd_poll_status(unsigned int trigger, long timeout)
|
|||
if (TIME_AFTER(time, next_yield))
|
||||
{
|
||||
long ty = USEC_TIMER;
|
||||
priority_yield();
|
||||
yield();
|
||||
timeout += USEC_TIMER - ty;
|
||||
next_yield = ty + MIN_YIELD_PERIOD;
|
||||
}
|
||||
|
@ -317,7 +317,7 @@ static int sd_wait_for_state(unsigned int state, int id)
|
|||
us = USEC_TIMER;
|
||||
if (TIME_AFTER(us, next_yield))
|
||||
{
|
||||
priority_yield();
|
||||
yield();
|
||||
timeout += USEC_TIMER - us;
|
||||
next_yield = us + MIN_YIELD_PERIOD;
|
||||
}
|
||||
|
|
62
firmware/target/coldfire/ffs-coldfire.S
Normal file
62
firmware/target/coldfire/ffs-coldfire.S
Normal file
|
@ -0,0 +1,62 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2008 by Michael Sevakis
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
#include "config.h"
|
||||
|
||||
/****************************************************************************
|
||||
* int find_first_set_bit(uint32_t val);
|
||||
*
|
||||
* Find the index of the least significant set bit in the 32-bit word.
|
||||
*
|
||||
* return values:
|
||||
* 0 - bit 0 is set
|
||||
* 1 - bit 1 is set
|
||||
* ...
|
||||
* 31 - bit 31 is set
|
||||
* 32 - no bits set
|
||||
****************************************************************************/
|
||||
.text
|
||||
.align 2
|
||||
.global find_first_set_bit
|
||||
.type find_first_set_bit,@function
|
||||
find_first_set_bit:
|
||||
| this is a coldfire version of the ffs algorithm devised by D.Seal
|
||||
| and posted to comp.sys.arm on 16 Feb 1994.
|
||||
|
|
||||
| Output modified to suit rockbox purposes.
|
||||
|
||||
| Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry
|
||||
move.l 4(%sp), %d1 | %d1 = %d1 & -%d1
|
||||
lea.l L_ffs_table, %a0 | %a0 = table address
|
||||
move.l %d1, %d0 |
|
||||
neg.l %d1 |
|
||||
and.l %d0, %d1 |
|
||||
|
||||
| now %d1 has at most one set bit, call this X
|
||||
|
||||
move.l #0x0450fbaf, %d0 | %d0 = multiplier
|
||||
mulu.l %d0, %d1 | %d1 = X * 0x0450fbaf
|
||||
|
||||
| now lookup in table indexed on top 6 bits of %d0
|
||||
moveq.l #26, %d0 | %d0 = final shift count
|
||||
lsr.l %d0, %d1 |
|
||||
clr.l %d0 |
|
||||
move.b (%a0, %d1.l), %d0 |
|
||||
rts |
|
||||
|
||||
.size find_first_set_bit, .-find_first_set_bit
|
2733
firmware/thread.c
2733
firmware/thread.c
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,5 @@
|
|||
button.c
|
||||
kernel.c
|
||||
kernel-sdl.c
|
||||
#ifdef HAVE_LCD_BITMAP
|
||||
lcd-bitmap.c
|
||||
#elif defined(HAVE_LCD_CHARCELLS)
|
||||
|
|
168
uisimulator/sdl/kernel-sdl.c
Normal file
168
uisimulator/sdl/kernel-sdl.c
Normal file
|
@ -0,0 +1,168 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2002 by Felix Arends
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <SDL.h>
|
||||
#include <SDL_thread.h>
|
||||
#include "memory.h"
|
||||
#include "system-sdl.h"
|
||||
#include "uisdl.h"
|
||||
#include "kernel.h"
|
||||
#include "thread-sdl.h"
|
||||
#include "thread.h"
|
||||
#include "debug.h"
|
||||
|
||||
static SDL_TimerID tick_timer_id;
|
||||
long start_tick;
|
||||
|
||||
/* Condition to signal that "interrupts" may proceed */
|
||||
static SDL_cond *sim_thread_cond;
|
||||
/* Mutex to serialize changing levels and exclude other threads while
|
||||
* inside a handler */
|
||||
static SDL_mutex *sim_irq_mtx;
|
||||
static int interrupt_level = HIGHEST_IRQ_LEVEL;
|
||||
static int handlers_pending = 0;
|
||||
static int status_reg = 0;
|
||||
|
||||
extern void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
|
||||
|
||||
/* Nescessary logic:
|
||||
* 1) All threads must pass unblocked
|
||||
* 2) Current handler must always pass unblocked
|
||||
* 3) Threads must be excluded when irq routine is running
|
||||
* 4) No more than one handler routine should execute at a time
|
||||
*/
|
||||
int set_irq_level(int level)
|
||||
{
|
||||
SDL_LockMutex(sim_irq_mtx);
|
||||
|
||||
int oldlevel = interrupt_level;
|
||||
|
||||
if (status_reg == 0 && level == 0 && oldlevel != 0)
|
||||
{
|
||||
/* Not in a handler and "interrupts" are being reenabled */
|
||||
if (handlers_pending > 0)
|
||||
SDL_CondSignal(sim_thread_cond);
|
||||
}
|
||||
|
||||
interrupt_level = level; /* save new level */
|
||||
|
||||
SDL_UnlockMutex(sim_irq_mtx);
|
||||
return oldlevel;
|
||||
}
|
||||
|
||||
void sim_enter_irq_handler(void)
|
||||
{
|
||||
SDL_LockMutex(sim_irq_mtx);
|
||||
handlers_pending++;
|
||||
|
||||
if(interrupt_level != 0)
|
||||
{
|
||||
/* "Interrupts" are disabled. Wait for reenable */
|
||||
SDL_CondWait(sim_thread_cond, sim_irq_mtx);
|
||||
}
|
||||
|
||||
status_reg = 1;
|
||||
}
|
||||
|
||||
void sim_exit_irq_handler(void)
|
||||
{
|
||||
if (--handlers_pending > 0)
|
||||
SDL_CondSignal(sim_thread_cond);
|
||||
|
||||
status_reg = 0;
|
||||
SDL_UnlockMutex(sim_irq_mtx);
|
||||
}
|
||||
|
||||
bool sim_kernel_init(void)
|
||||
{
|
||||
sim_irq_mtx = SDL_CreateMutex();
|
||||
if (sim_irq_mtx == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot create sim_handler_mtx\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
sim_thread_cond = SDL_CreateCond();
|
||||
if (sim_thread_cond == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot create sim_thread_cond\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sim_kernel_shutdown(void)
|
||||
{
|
||||
SDL_RemoveTimer(tick_timer_id);
|
||||
SDL_DestroyMutex(sim_irq_mtx);
|
||||
SDL_DestroyCond(sim_thread_cond);
|
||||
}
|
||||
|
||||
Uint32 tick_timer(Uint32 interval, void *param)
|
||||
{
|
||||
long new_tick;
|
||||
|
||||
(void) interval;
|
||||
(void) param;
|
||||
|
||||
new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
|
||||
|
||||
if(new_tick != current_tick)
|
||||
{
|
||||
long t;
|
||||
for(t = new_tick - current_tick; t > 0; t--)
|
||||
{
|
||||
int i;
|
||||
|
||||
sim_enter_irq_handler();
|
||||
|
||||
/* Run through the list of tick tasks */
|
||||
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
||||
{
|
||||
if(tick_funcs[i])
|
||||
{
|
||||
tick_funcs[i]();
|
||||
}
|
||||
}
|
||||
|
||||
sim_exit_irq_handler();
|
||||
}
|
||||
|
||||
current_tick = new_tick;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void tick_start(unsigned int interval_in_ms)
|
||||
{
|
||||
if (tick_timer_id != NULL)
|
||||
{
|
||||
SDL_RemoveTimer(tick_timer_id);
|
||||
tick_timer_id = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
start_tick = SDL_GetTicks();
|
||||
}
|
||||
|
||||
tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL);
|
||||
}
|
|
@ -1,739 +0,0 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2002 by Felix Arends
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <SDL.h>
|
||||
#include <SDL_thread.h>
|
||||
#include "memory.h"
|
||||
#include "system-sdl.h"
|
||||
#include "uisdl.h"
|
||||
#include "kernel.h"
|
||||
#include "thread-sdl.h"
|
||||
#include "thread.h"
|
||||
#include "debug.h"
|
||||
|
||||
/* Condition to signal that "interrupts" may proceed */
|
||||
static SDL_cond *sim_thread_cond;
|
||||
/* Mutex to serialize changing levels and exclude other threads while
|
||||
* inside a handler */
|
||||
static SDL_mutex *sim_irq_mtx;
|
||||
static int interrupt_level = HIGHEST_IRQ_LEVEL;
|
||||
static int handlers_pending = 0;
|
||||
static int status_reg = 0;
|
||||
|
||||
extern struct core_entry cores[NUM_CORES];
|
||||
|
||||
/* Nescessary logic:
|
||||
* 1) All threads must pass unblocked
|
||||
* 2) Current handler must always pass unblocked
|
||||
* 3) Threads must be excluded when irq routine is running
|
||||
* 4) No more than one handler routine should execute at a time
|
||||
*/
|
||||
int set_irq_level(int level)
|
||||
{
|
||||
SDL_LockMutex(sim_irq_mtx);
|
||||
|
||||
int oldlevel = interrupt_level;
|
||||
|
||||
if (status_reg == 0 && level == 0 && oldlevel != 0)
|
||||
{
|
||||
/* Not in a handler and "interrupts" are being reenabled */
|
||||
if (handlers_pending > 0)
|
||||
SDL_CondSignal(sim_thread_cond);
|
||||
}
|
||||
|
||||
interrupt_level = level; /* save new level */
|
||||
|
||||
SDL_UnlockMutex(sim_irq_mtx);
|
||||
return oldlevel;
|
||||
}
|
||||
|
||||
void sim_enter_irq_handler(void)
|
||||
{
|
||||
SDL_LockMutex(sim_irq_mtx);
|
||||
handlers_pending++;
|
||||
|
||||
if(interrupt_level != 0)
|
||||
{
|
||||
/* "Interrupts" are disabled. Wait for reenable */
|
||||
SDL_CondWait(sim_thread_cond, sim_irq_mtx);
|
||||
}
|
||||
|
||||
status_reg = 1;
|
||||
}
|
||||
|
||||
void sim_exit_irq_handler(void)
|
||||
{
|
||||
if (--handlers_pending > 0)
|
||||
SDL_CondSignal(sim_thread_cond);
|
||||
|
||||
status_reg = 0;
|
||||
SDL_UnlockMutex(sim_irq_mtx);
|
||||
}
|
||||
|
||||
bool sim_kernel_init(void)
|
||||
{
|
||||
sim_irq_mtx = SDL_CreateMutex();
|
||||
if (sim_irq_mtx == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot create sim_handler_mtx\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
sim_thread_cond = SDL_CreateCond();
|
||||
if (sim_thread_cond == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot create sim_thread_cond\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sim_kernel_shutdown(void)
|
||||
{
|
||||
SDL_DestroyMutex(sim_irq_mtx);
|
||||
SDL_DestroyCond(sim_thread_cond);
|
||||
}
|
||||
|
||||
volatile long current_tick = 0;
|
||||
static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
|
||||
|
||||
/* This array holds all queues that are initiated. It is used for broadcast. */
|
||||
static struct event_queue *all_queues[MAX_NUM_QUEUES];
|
||||
static int num_queues = 0;
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
/* Moves waiting thread's descriptor to the current sender when a
|
||||
message is dequeued */
|
||||
static void queue_fetch_sender(struct queue_sender_list *send,
|
||||
unsigned int i)
|
||||
{
|
||||
struct thread_entry **spp = &send->senders[i];
|
||||
|
||||
if(*spp)
|
||||
{
|
||||
send->curr_sender = *spp;
|
||||
*spp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Puts the specified return value in the waiting thread's return value
|
||||
and wakes the thread - a sender should be confirmed to exist first */
|
||||
static void queue_release_sender(struct thread_entry **sender,
|
||||
intptr_t retval)
|
||||
{
|
||||
(*sender)->retval = retval;
|
||||
wakeup_thread_no_listlock(sender);
|
||||
if(*sender != NULL)
|
||||
{
|
||||
fprintf(stderr, "queue->send slot ovf: %p\n", *sender);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Releases any waiting threads that are queued with queue_send -
|
||||
reply with NULL */
|
||||
static void queue_release_all_senders(struct event_queue *q)
|
||||
{
|
||||
if(q->send)
|
||||
{
|
||||
unsigned int i;
|
||||
for(i = q->read; i != q->write; i++)
|
||||
{
|
||||
struct thread_entry **spp =
|
||||
&q->send->senders[i & QUEUE_LENGTH_MASK];
|
||||
if(*spp)
|
||||
{
|
||||
queue_release_sender(spp, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Enables queue_send on the specified queue - caller allocates the extra
|
||||
data structure */
|
||||
void queue_enable_queue_send(struct event_queue *q,
|
||||
struct queue_sender_list *send)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
q->send = NULL;
|
||||
if(send)
|
||||
{
|
||||
q->send = send;
|
||||
memset(send, 0, sizeof(*send));
|
||||
}
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
#endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
|
||||
|
||||
void queue_init(struct event_queue *q, bool register_queue)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
q->read = 0;
|
||||
q->write = 0;
|
||||
thread_queue_init(&q->queue);
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
q->send = NULL; /* No message sending by default */
|
||||
#endif
|
||||
|
||||
if(register_queue)
|
||||
{
|
||||
if(num_queues >= MAX_NUM_QUEUES)
|
||||
{
|
||||
fprintf(stderr, "queue_init->out of queues");
|
||||
exit(-1);
|
||||
}
|
||||
/* Add it to the all_queues array */
|
||||
all_queues[num_queues++] = q;
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void queue_delete(struct event_queue *q)
|
||||
{
|
||||
int i;
|
||||
bool found = false;
|
||||
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
/* Find the queue to be deleted */
|
||||
for(i = 0;i < num_queues;i++)
|
||||
{
|
||||
if(all_queues[i] == q)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(found)
|
||||
{
|
||||
/* Move the following queues up in the list */
|
||||
for(;i < num_queues-1;i++)
|
||||
{
|
||||
all_queues[i] = all_queues[i+1];
|
||||
}
|
||||
|
||||
num_queues--;
|
||||
}
|
||||
|
||||
/* Release threads waiting on queue head */
|
||||
thread_queue_wake(&q->queue);
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
/* Release waiting threads and reply to any dequeued message
|
||||
waiting for one. */
|
||||
queue_release_all_senders(q);
|
||||
queue_reply(q, 0);
|
||||
#endif
|
||||
|
||||
q->read = 0;
|
||||
q->write = 0;
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void queue_wait(struct event_queue *q, struct queue_event *ev)
|
||||
{
|
||||
unsigned int rd;
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if (q->send && q->send->curr_sender)
|
||||
{
|
||||
/* auto-reply */
|
||||
queue_release_sender(&q->send->curr_sender, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (q->read == q->write)
|
||||
{
|
||||
do
|
||||
{
|
||||
block_thread(&q->queue);
|
||||
oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
}
|
||||
while (q->read == q->write);
|
||||
}
|
||||
|
||||
rd = q->read++ & QUEUE_LENGTH_MASK;
|
||||
*ev = q->events[rd];
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if(q->send && q->send->senders[rd])
|
||||
{
|
||||
/* Get data for a waiting thread if one */
|
||||
queue_fetch_sender(q->send, rd);
|
||||
}
|
||||
#endif
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev, int ticks)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if (q->send && q->send->curr_sender)
|
||||
{
|
||||
/* auto-reply */
|
||||
queue_release_sender(&q->send->curr_sender, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (q->read == q->write && ticks > 0)
|
||||
{
|
||||
block_thread_w_tmo(&q->queue, ticks);
|
||||
oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
}
|
||||
|
||||
if(q->read != q->write)
|
||||
{
|
||||
unsigned int rd = q->read++ & QUEUE_LENGTH_MASK;
|
||||
*ev = q->events[rd];
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if(q->send && q->send->senders[rd])
|
||||
{
|
||||
/* Get data for a waiting thread if one */
|
||||
queue_fetch_sender(q->send, rd);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
ev->id = SYS_TIMEOUT;
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void queue_post(struct event_queue *q, long id, intptr_t data)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
unsigned int wr = q->write++ & QUEUE_LENGTH_MASK;
|
||||
|
||||
q->events[wr].id = id;
|
||||
q->events[wr].data = data;
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if(q->send)
|
||||
{
|
||||
struct thread_entry **spp = &q->send->senders[wr];
|
||||
|
||||
if(*spp)
|
||||
{
|
||||
/* overflow protect - unblock any thread waiting at this index */
|
||||
queue_release_sender(spp, 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
wakeup_thread(&q->queue);
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
intptr_t queue_send(struct event_queue *q, long id, intptr_t data)
|
||||
{
|
||||
int oldlevel = set_irq_level(oldlevel);
|
||||
|
||||
unsigned int wr = q->write++ & QUEUE_LENGTH_MASK;
|
||||
|
||||
q->events[wr].id = id;
|
||||
q->events[wr].data = data;
|
||||
|
||||
if(q->send)
|
||||
{
|
||||
struct thread_entry **spp = &q->send->senders[wr];
|
||||
|
||||
if(*spp)
|
||||
{
|
||||
/* overflow protect - unblock any thread waiting at this index */
|
||||
queue_release_sender(spp, 0);
|
||||
}
|
||||
|
||||
wakeup_thread(&q->queue);
|
||||
|
||||
block_thread_no_listlock(spp);
|
||||
return thread_get_current()->retval;
|
||||
}
|
||||
|
||||
/* Function as queue_post if sending is not enabled */
|
||||
wakeup_thread(&q->queue);
|
||||
set_irq_level(oldlevel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0 /* not used now but probably will be later */
|
||||
/* Query if the last message dequeued was added by queue_send or not */
|
||||
bool queue_in_queue_send(struct event_queue *q)
|
||||
{
|
||||
return q->send && q->send->curr_sender;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Replies with retval to any dequeued message sent with queue_send */
|
||||
void queue_reply(struct event_queue *q, intptr_t retval)
|
||||
{
|
||||
if(q->send && q->send->curr_sender)
|
||||
{
|
||||
queue_release_sender(&q->send->curr_sender, retval);
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
|
||||
|
||||
bool queue_empty(const struct event_queue* q)
|
||||
{
|
||||
return ( q->read == q->write );
|
||||
}
|
||||
|
||||
bool queue_peek(struct event_queue *q, struct queue_event *ev)
|
||||
{
|
||||
if (q->read == q->write)
|
||||
return false;
|
||||
|
||||
bool have_msg = false;
|
||||
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
if (q->read != q->write)
|
||||
{
|
||||
*ev = q->events[q->read & QUEUE_LENGTH_MASK];
|
||||
have_msg = true;
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
|
||||
return have_msg;
|
||||
}
|
||||
|
||||
void queue_clear(struct event_queue* q)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
/* fixme: This is potentially unsafe in case we do interrupt-like processing */
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
/* Release all thread waiting in the queue for a reply -
|
||||
dequeued sent message will be handled by owning thread */
|
||||
queue_release_all_senders(q);
|
||||
#endif
|
||||
q->read = 0;
|
||||
q->write = 0;
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void queue_remove_from_head(struct event_queue *q, long id)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
while(q->read != q->write)
|
||||
{
|
||||
unsigned int rd = q->read & QUEUE_LENGTH_MASK;
|
||||
|
||||
if(q->events[rd].id != id)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
||||
if(q->send)
|
||||
{
|
||||
struct thread_entry **spp = &q->send->senders[rd];
|
||||
|
||||
if(*spp)
|
||||
{
|
||||
/* Release any thread waiting on this message */
|
||||
queue_release_sender(spp, 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
q->read++;
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
int queue_count(const struct event_queue *q)
|
||||
{
|
||||
return q->write - q->read;
|
||||
}
|
||||
|
||||
int queue_broadcast(long id, intptr_t data)
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
int i;
|
||||
|
||||
for(i = 0;i < num_queues;i++)
|
||||
{
|
||||
queue_post(all_queues[i], id, data);
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
return num_queues;
|
||||
}
|
||||
|
||||
void yield(void)
|
||||
{
|
||||
switch_thread(NULL);
|
||||
}
|
||||
|
||||
void sleep(int ticks)
|
||||
{
|
||||
sleep_thread(ticks);
|
||||
}
|
||||
|
||||
void sim_tick_tasks(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Run through the list of tick tasks */
|
||||
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
||||
{
|
||||
if(tick_funcs[i])
|
||||
{
|
||||
tick_funcs[i]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int tick_add_task(void (*f)(void))
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
int i;
|
||||
|
||||
/* Add a task if there is room */
|
||||
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
||||
{
|
||||
if(tick_funcs[i] == NULL)
|
||||
{
|
||||
tick_funcs[i] = f;
|
||||
set_irq_level(oldlevel);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "Error! tick_add_task(): out of tasks");
|
||||
exit(-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int tick_remove_task(void (*f)(void))
|
||||
{
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
int i;
|
||||
|
||||
/* Remove a task if it is there */
|
||||
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
||||
{
|
||||
if(tick_funcs[i] == f)
|
||||
{
|
||||
tick_funcs[i] = NULL;
|
||||
set_irq_level(oldlevel);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Very simple mutex simulation - won't work with pre-emptive
|
||||
multitasking, but is better than nothing at all */
|
||||
void mutex_init(struct mutex *m)
|
||||
{
|
||||
m->queue = NULL;
|
||||
m->thread = NULL;
|
||||
m->count = 0;
|
||||
m->locked = 0;
|
||||
}
|
||||
|
||||
void mutex_lock(struct mutex *m)
|
||||
{
|
||||
struct thread_entry *const thread = thread_get_current();
|
||||
|
||||
if(thread == m->thread)
|
||||
{
|
||||
m->count++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!test_and_set(&m->locked, 1))
|
||||
{
|
||||
m->thread = thread;
|
||||
return;
|
||||
}
|
||||
|
||||
block_thread_no_listlock(&m->queue);
|
||||
}
|
||||
|
||||
void mutex_unlock(struct mutex *m)
|
||||
{
|
||||
/* unlocker not being the owner is an unlocking violation */
|
||||
if(m->thread != thread_get_current())
|
||||
{
|
||||
fprintf(stderr, "mutex_unlock->wrong thread");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (m->count > 0)
|
||||
{
|
||||
/* this thread still owns lock */
|
||||
m->count--;
|
||||
return;
|
||||
}
|
||||
|
||||
m->thread = wakeup_thread_no_listlock(&m->queue);
|
||||
|
||||
if (m->thread == NULL)
|
||||
{
|
||||
/* release lock */
|
||||
m->locked = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_SEMAPHORE_OBJECTS
|
||||
void semaphore_init(struct semaphore *s, int max, int start)
|
||||
{
|
||||
if(max <= 0 || start < 0 || start > max)
|
||||
{
|
||||
fprintf(stderr, "semaphore_init->inv arg");
|
||||
exit(-1);
|
||||
}
|
||||
s->queue = NULL;
|
||||
s->max = max;
|
||||
s->count = start;
|
||||
}
|
||||
|
||||
void semaphore_wait(struct semaphore *s)
|
||||
{
|
||||
if(--s->count >= 0)
|
||||
return;
|
||||
block_thread_no_listlock(&s->queue);
|
||||
}
|
||||
|
||||
void semaphore_release(struct semaphore *s)
|
||||
{
|
||||
if(s->count < s->max)
|
||||
{
|
||||
if(++s->count <= 0)
|
||||
{
|
||||
if(s->queue == NULL)
|
||||
{
|
||||
/* there should be threads in this queue */
|
||||
fprintf(stderr, "semaphore->wakeup");
|
||||
exit(-1);
|
||||
}
|
||||
/* a thread was queued - wake it up */
|
||||
wakeup_thread_no_listlock(&s->queue);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_SEMAPHORE_OBJECTS */
|
||||
|
||||
#ifdef HAVE_EVENT_OBJECTS
|
||||
void event_init(struct event *e, unsigned int flags)
|
||||
{
|
||||
e->queues[STATE_NONSIGNALED] = NULL;
|
||||
e->queues[STATE_SIGNALED] = NULL;
|
||||
e->state = flags & STATE_SIGNALED;
|
||||
e->automatic = (flags & EVENT_AUTOMATIC) ? 1 : 0;
|
||||
}
|
||||
|
||||
void event_wait(struct event *e, unsigned int for_state)
|
||||
{
|
||||
unsigned int last_state = e->state;
|
||||
|
||||
if(e->automatic != 0)
|
||||
{
|
||||
/* wait for false always satisfied by definition
|
||||
or if it just changed to false */
|
||||
if(last_state == STATE_SIGNALED || for_state == STATE_NONSIGNALED)
|
||||
{
|
||||
/* automatic - unsignal */
|
||||
e->state = STATE_NONSIGNALED;
|
||||
return;
|
||||
}
|
||||
/* block until state matches */
|
||||
}
|
||||
else if(for_state == last_state)
|
||||
{
|
||||
/* the state being waited for is the current state */
|
||||
return;
|
||||
}
|
||||
|
||||
/* current state does not match wait-for state */
|
||||
block_thread_no_listlock(&e->queues[for_state]);
|
||||
}
|
||||
|
||||
void event_set_state(struct event *e, unsigned int state)
|
||||
{
|
||||
unsigned int last_state = e->state;
|
||||
|
||||
if(last_state == state)
|
||||
{
|
||||
/* no change */
|
||||
return;
|
||||
}
|
||||
|
||||
if(state == STATE_SIGNALED)
|
||||
{
|
||||
if(e->automatic != 0)
|
||||
{
|
||||
struct thread_entry *thread;
|
||||
|
||||
if(e->queues[STATE_NONSIGNALED] != NULL)
|
||||
{
|
||||
/* no thread should have ever blocked for nonsignaled */
|
||||
fprintf(stderr, "set_event_state->queue[NS]:S");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* pass to next thread and keep unsignaled - "pulse" */
|
||||
thread = wakeup_thread_no_listlock(&e->queues[STATE_SIGNALED]);
|
||||
e->state = thread != NULL ? STATE_NONSIGNALED : STATE_SIGNALED;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* release all threads waiting for signaled */
|
||||
thread_queue_wake_no_listlock(&e->queues[STATE_SIGNALED]);
|
||||
e->state = STATE_SIGNALED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* release all threads waiting for unsignaled */
|
||||
if(e->queues[STATE_NONSIGNALED] != NULL && e->automatic != 0)
|
||||
{
|
||||
/* no thread should have ever blocked */
|
||||
fprintf(stderr, "set_event_state->queue[NS]:NS");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
thread_queue_wake_no_listlock(&e->queues[STATE_NONSIGNALED]);
|
||||
e->state = STATE_NONSIGNALED;
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_EVENT_OBJECTS */
|
|
@ -29,4 +29,6 @@ void sim_exit_irq_handler(void);
|
|||
bool sim_kernel_init(void);
|
||||
void sim_kernel_shutdown(void);
|
||||
|
||||
extern long start_tick;
|
||||
|
||||
#endif /* _SYSTEM_SDL_H_ */
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include <setjmp.h>
|
||||
#include "system-sdl.h"
|
||||
#include "thread-sdl.h"
|
||||
#include "system.h"
|
||||
#include "kernel.h"
|
||||
#include "thread.h"
|
||||
#include "debug.h"
|
||||
|
@ -37,7 +38,7 @@
|
|||
#define THREAD_SDL_DEBUGF(...) DEBUGF(__VA_ARGS__)
|
||||
static char __name[32];
|
||||
#define THREAD_SDL_GET_NAME(thread) \
|
||||
({ thread_get_name(__name, sizeof(__name)/sizeof(__name[0]), thread); __name; })
|
||||
({ thread_get_name(__name, ARRAYLEN(__name), thread); __name; })
|
||||
#else
|
||||
#define THREAD_SDL_DEBUGF(...)
|
||||
#define THREAD_SDL_GET_NAME(thread)
|
||||
|
@ -54,7 +55,6 @@ struct thread_entry threads[MAXTHREADS];
|
|||
* way to get them back in there so they may exit */
|
||||
static jmp_buf thread_jmpbufs[MAXTHREADS];
|
||||
static SDL_mutex *m;
|
||||
static struct thread_entry *running;
|
||||
static bool threads_exit = false;
|
||||
|
||||
extern long start_tick;
|
||||
|
@ -78,7 +78,7 @@ void thread_sdl_shutdown(void)
|
|||
{
|
||||
/* Signal thread on delay or block */
|
||||
SDL_Thread *t = thread->context.t;
|
||||
SDL_CondSignal(thread->context.c);
|
||||
SDL_SemPost(thread->context.s);
|
||||
SDL_UnlockMutex(m);
|
||||
/* Wait for it to finish */
|
||||
SDL_WaitThread(t, NULL);
|
||||
|
@ -98,7 +98,7 @@ extern void app_main(void *param);
|
|||
static int thread_sdl_app_main(void *param)
|
||||
{
|
||||
SDL_LockMutex(m);
|
||||
running = &threads[0];
|
||||
cores[CURRENT_CORE].running = &threads[0];
|
||||
|
||||
/* Set the jump address for return */
|
||||
if (setjmp(thread_jmpbufs[0]) == 0)
|
||||
|
@ -116,6 +116,8 @@ static int thread_sdl_app_main(void *param)
|
|||
/* Initialize SDL threading */
|
||||
bool thread_sdl_init(void *param)
|
||||
{
|
||||
struct thread_entry *thread;
|
||||
memset(cores, 0, sizeof(cores));
|
||||
memset(threads, 0, sizeof(threads));
|
||||
|
||||
m = SDL_CreateMutex();
|
||||
|
@ -129,28 +131,30 @@ bool thread_sdl_init(void *param)
|
|||
/* Slot 0 is reserved for the main thread - initialize it here and
|
||||
then create the SDL thread - it is possible to have a quick, early
|
||||
shutdown try to access the structure. */
|
||||
running = &threads[0];
|
||||
running->stack = " ";
|
||||
running->stack_size = 8;
|
||||
running->name = "main";
|
||||
running->state = STATE_RUNNING;
|
||||
running->context.c = SDL_CreateCond();
|
||||
thread = &threads[0];
|
||||
thread->stack = (uintptr_t *)" ";
|
||||
thread->stack_size = 8;
|
||||
thread->name = "main";
|
||||
thread->state = STATE_RUNNING;
|
||||
thread->context.s = SDL_CreateSemaphore(0);
|
||||
cores[CURRENT_CORE].running = thread;
|
||||
|
||||
if (running->context.c == NULL)
|
||||
if (thread->context.s == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to create main condition variable\n");
|
||||
fprintf(stderr, "Failed to create main semaphore\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
running->context.t = SDL_CreateThread(thread_sdl_app_main, param);
|
||||
thread->context.t = SDL_CreateThread(thread_sdl_app_main, param);
|
||||
|
||||
if (running->context.t == NULL)
|
||||
if (thread->context.t == NULL)
|
||||
{
|
||||
SDL_DestroySemaphore(thread->context.s);
|
||||
fprintf(stderr, "Failed to create main thread\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
THREAD_SDL_DEBUGF("Main thread: %p\n", running);
|
||||
THREAD_SDL_DEBUGF("Main thread: %p\n", thread);
|
||||
|
||||
SDL_UnlockMutex(m);
|
||||
return true;
|
||||
|
@ -160,21 +164,22 @@ bool thread_sdl_init(void *param)
|
|||
void thread_sdl_thread_lock(void *me)
|
||||
{
|
||||
SDL_LockMutex(m);
|
||||
running = (struct thread_entry *)me;
|
||||
cores[CURRENT_CORE].running = (struct thread_entry *)me;
|
||||
|
||||
if (threads_exit)
|
||||
remove_thread(NULL);
|
||||
thread_exit();
|
||||
}
|
||||
|
||||
void * thread_sdl_thread_unlock(void)
|
||||
{
|
||||
struct thread_entry *current = running;
|
||||
struct thread_entry *current = cores[CURRENT_CORE].running;
|
||||
SDL_UnlockMutex(m);
|
||||
return current;
|
||||
}
|
||||
|
||||
static int find_empty_thread_slot(void)
|
||||
static struct thread_entry * find_empty_thread_slot(void)
|
||||
{
|
||||
struct thread_entry *thread = NULL;
|
||||
int n;
|
||||
|
||||
for (n = 0; n < MAXTHREADS; n++)
|
||||
|
@ -182,10 +187,13 @@ static int find_empty_thread_slot(void)
|
|||
int state = threads[n].state;
|
||||
|
||||
if (state == STATE_KILLED)
|
||||
{
|
||||
thread = &threads[n];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
return thread;
|
||||
}
|
||||
|
||||
static void add_to_list_l(struct thread_entry **list,
|
||||
|
@ -229,64 +237,163 @@ static void remove_from_list_l(struct thread_entry **list,
|
|||
thread->l.next->l.prev = thread->l.prev;
|
||||
}
|
||||
|
||||
static inline void run_blocking_ops(void)
|
||||
{
|
||||
set_irq_level(0);
|
||||
}
|
||||
|
||||
struct thread_entry *thread_get_current(void)
|
||||
{
|
||||
return running;
|
||||
return cores[CURRENT_CORE].running;
|
||||
}
|
||||
|
||||
void switch_thread(struct thread_entry *old)
|
||||
void switch_thread(void)
|
||||
{
|
||||
struct thread_entry *current = running;
|
||||
struct thread_entry *current = cores[CURRENT_CORE].running;
|
||||
|
||||
SDL_UnlockMutex(m);
|
||||
/* Any other thread waiting already will get it first */
|
||||
SDL_LockMutex(m);
|
||||
running = current;
|
||||
set_irq_level(0);
|
||||
|
||||
switch (current->state)
|
||||
{
|
||||
case STATE_RUNNING:
|
||||
{
|
||||
SDL_UnlockMutex(m);
|
||||
/* Any other thread waiting already will get it first */
|
||||
SDL_LockMutex(m);
|
||||
break;
|
||||
} /* STATE_RUNNING: */
|
||||
|
||||
case STATE_BLOCKED:
|
||||
{
|
||||
int oldlevel;
|
||||
|
||||
SDL_UnlockMutex(m);
|
||||
SDL_SemWait(current->context.s);
|
||||
SDL_LockMutex(m);
|
||||
|
||||
oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
current->state = STATE_RUNNING;
|
||||
set_irq_level(oldlevel);
|
||||
break;
|
||||
} /* STATE_BLOCKED: */
|
||||
|
||||
case STATE_BLOCKED_W_TMO:
|
||||
{
|
||||
int result, oldlevel;
|
||||
|
||||
SDL_UnlockMutex(m);
|
||||
result = SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
|
||||
SDL_LockMutex(m);
|
||||
|
||||
oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
if (current->state == STATE_BLOCKED_W_TMO)
|
||||
{
|
||||
/* Timed out */
|
||||
remove_from_list_l(current->bqp, current);
|
||||
|
||||
#ifdef HAVE_WAKEUP_EXT_CB
|
||||
if (current->wakeup_ext_cb != NULL)
|
||||
current->wakeup_ext_cb(current);
|
||||
#endif
|
||||
current->state = STATE_RUNNING;
|
||||
}
|
||||
|
||||
if (result == SDL_MUTEX_TIMEDOUT)
|
||||
{
|
||||
/* Other signals from an explicit wake could have been made before
|
||||
* arriving here if we timed out waiting for the semaphore. Make
|
||||
* sure the count is reset. */
|
||||
while (SDL_SemValue(current->context.s) > 0)
|
||||
SDL_SemTryWait(current->context.s);
|
||||
}
|
||||
|
||||
set_irq_level(oldlevel);
|
||||
break;
|
||||
} /* STATE_BLOCKED_W_TMO: */
|
||||
|
||||
case STATE_SLEEPING:
|
||||
{
|
||||
SDL_UnlockMutex(m);
|
||||
SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
|
||||
SDL_LockMutex(m);
|
||||
current->state = STATE_RUNNING;
|
||||
break;
|
||||
} /* STATE_SLEEPING: */
|
||||
}
|
||||
|
||||
cores[CURRENT_CORE].running = current;
|
||||
|
||||
if (threads_exit)
|
||||
remove_thread(NULL);
|
||||
|
||||
(void)old;
|
||||
thread_exit();
|
||||
}
|
||||
|
||||
void sleep_thread(int ticks)
|
||||
{
|
||||
struct thread_entry *current;
|
||||
struct thread_entry *current = cores[CURRENT_CORE].running;
|
||||
int rem;
|
||||
|
||||
current = running;
|
||||
current->state = STATE_SLEEPING;
|
||||
|
||||
rem = (SDL_GetTicks() - start_tick) % (1000/HZ);
|
||||
if (rem < 0)
|
||||
rem = 0;
|
||||
|
||||
rem = (1000/HZ) * ticks + ((1000/HZ)-1) - rem;
|
||||
current->tmo_tick = (1000/HZ) * ticks + ((1000/HZ)-1) - rem;
|
||||
}
|
||||
|
||||
if (rem == 0)
|
||||
void block_thread(struct thread_entry *current)
|
||||
{
|
||||
current->state = STATE_BLOCKED;
|
||||
add_to_list_l(current->bqp, current);
|
||||
}
|
||||
|
||||
void block_thread_w_tmo(struct thread_entry *current, int ticks)
|
||||
{
|
||||
current->state = STATE_BLOCKED_W_TMO;
|
||||
current->tmo_tick = (1000/HZ)*ticks;
|
||||
add_to_list_l(current->bqp, current);
|
||||
}
|
||||
|
||||
unsigned int wakeup_thread(struct thread_entry **list)
|
||||
{
|
||||
struct thread_entry *thread = *list;
|
||||
|
||||
if (thread != NULL)
|
||||
{
|
||||
/* Unlock and give up rest of quantum */
|
||||
SDL_UnlockMutex(m);
|
||||
SDL_Delay(0);
|
||||
SDL_LockMutex(m);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* These sleeps must be signalable for thread exit */
|
||||
SDL_CondWaitTimeout(current->context.c, m, rem);
|
||||
switch (thread->state)
|
||||
{
|
||||
case STATE_BLOCKED:
|
||||
case STATE_BLOCKED_W_TMO:
|
||||
remove_from_list_l(list, thread);
|
||||
thread->state = STATE_RUNNING;
|
||||
SDL_SemPost(thread->context.s);
|
||||
return THREAD_OK;
|
||||
}
|
||||
}
|
||||
|
||||
running = current;
|
||||
return THREAD_NONE;
|
||||
}
|
||||
|
||||
current->state = STATE_RUNNING;
|
||||
unsigned int thread_queue_wake(struct thread_entry **list)
|
||||
{
|
||||
unsigned int result = THREAD_NONE;
|
||||
|
||||
if (threads_exit)
|
||||
remove_thread(NULL);
|
||||
for (;;)
|
||||
{
|
||||
unsigned int rc = wakeup_thread(list);
|
||||
|
||||
if (rc == THREAD_NONE)
|
||||
break;
|
||||
|
||||
result |= rc;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void thread_thaw(struct thread_entry *thread)
|
||||
{
|
||||
if (thread->state == STATE_FROZEN)
|
||||
{
|
||||
thread->state = STATE_RUNNING;
|
||||
SDL_SemPost(thread->context.s);
|
||||
}
|
||||
}
|
||||
|
||||
int runthread(void *data)
|
||||
|
@ -297,9 +404,9 @@ int runthread(void *data)
|
|||
/* Cannot access thread variables before locking the mutex as the
|
||||
data structures may not be filled-in yet. */
|
||||
SDL_LockMutex(m);
|
||||
running = (struct thread_entry *)data;
|
||||
current = running;
|
||||
current_jmpbuf = &thread_jmpbufs[running - threads];
|
||||
cores[CURRENT_CORE].running = (struct thread_entry *)data;
|
||||
current = cores[CURRENT_CORE].running;
|
||||
current_jmpbuf = &thread_jmpbufs[current - threads];
|
||||
|
||||
/* Setup jump for exit */
|
||||
if (setjmp(*current_jmpbuf) == 0)
|
||||
|
@ -307,9 +414,10 @@ int runthread(void *data)
|
|||
/* Run the thread routine */
|
||||
if (current->state == STATE_FROZEN)
|
||||
{
|
||||
SDL_CondWait(current->context.c, m);
|
||||
running = current;
|
||||
|
||||
SDL_UnlockMutex(m);
|
||||
SDL_SemWait(current->context.s);
|
||||
SDL_LockMutex(m);
|
||||
cores[CURRENT_CORE].running = current;
|
||||
}
|
||||
|
||||
if (!threads_exit)
|
||||
|
@ -320,7 +428,7 @@ int runthread(void *data)
|
|||
/* Thread routine returned - suicide */
|
||||
}
|
||||
|
||||
remove_thread(NULL);
|
||||
thread_exit();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -332,131 +440,59 @@ int runthread(void *data)
|
|||
}
|
||||
|
||||
struct thread_entry*
|
||||
create_thread(void (*function)(void), void* stack, int stack_size,
|
||||
create_thread(void (*function)(void), void* stack, size_t stack_size,
|
||||
unsigned flags, const char *name)
|
||||
{
|
||||
/** Avoid compiler warnings */
|
||||
struct thread_entry *thread;
|
||||
SDL_Thread* t;
|
||||
SDL_cond *cond;
|
||||
int slot;
|
||||
SDL_sem *s;
|
||||
|
||||
THREAD_SDL_DEBUGF("Creating thread: (%s)\n", name ? name : "");
|
||||
|
||||
slot = find_empty_thread_slot();
|
||||
if (slot >= MAXTHREADS)
|
||||
thread = find_empty_thread_slot();
|
||||
if (thread == NULL)
|
||||
{
|
||||
DEBUGF("Failed to find thread slot\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cond = SDL_CreateCond();
|
||||
if (cond == NULL)
|
||||
s = SDL_CreateSemaphore(0);
|
||||
if (s == NULL)
|
||||
{
|
||||
DEBUGF("Failed to create condition variable\n");
|
||||
DEBUGF("Failed to create semaphore\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
t = SDL_CreateThread(runthread, &threads[slot]);
|
||||
t = SDL_CreateThread(runthread, thread);
|
||||
if (t == NULL)
|
||||
{
|
||||
DEBUGF("Failed to create SDL thread\n");
|
||||
SDL_DestroyCond(cond);
|
||||
SDL_DestroySemaphore(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
threads[slot].stack = stack;
|
||||
threads[slot].stack_size = stack_size;
|
||||
threads[slot].name = name;
|
||||
threads[slot].state = (flags & CREATE_THREAD_FROZEN) ?
|
||||
thread->stack = stack;
|
||||
thread->stack_size = stack_size;
|
||||
thread->name = name;
|
||||
thread->state = (flags & CREATE_THREAD_FROZEN) ?
|
||||
STATE_FROZEN : STATE_RUNNING;
|
||||
threads[slot].context.start = function;
|
||||
threads[slot].context.t = t;
|
||||
threads[slot].context.c = cond;
|
||||
thread->context.start = function;
|
||||
thread->context.t = t;
|
||||
thread->context.s = s;
|
||||
|
||||
THREAD_SDL_DEBUGF("New Thread: %d (%s)\n",
|
||||
slot, THREAD_SDL_GET_NAME(&threads[slot]));
|
||||
thread - threads, THREAD_SDL_GET_NAME(thread));
|
||||
|
||||
return &threads[slot];
|
||||
}
|
||||
|
||||
void _block_thread(struct thread_queue *tq)
|
||||
{
|
||||
struct thread_entry *thread = running;
|
||||
|
||||
thread->state = STATE_BLOCKED;
|
||||
thread->bqp = tq;
|
||||
add_to_list_l(&tq->queue, thread);
|
||||
|
||||
run_blocking_ops();
|
||||
|
||||
SDL_CondWait(thread->context.c, m);
|
||||
running = thread;
|
||||
|
||||
if (threads_exit)
|
||||
remove_thread(NULL);
|
||||
}
|
||||
|
||||
void block_thread_w_tmo(struct thread_queue *tq, int ticks)
|
||||
{
|
||||
struct thread_entry *thread = running;
|
||||
|
||||
thread->state = STATE_BLOCKED_W_TMO;
|
||||
thread->bqp = tq;
|
||||
add_to_list_l(&tq->queue, thread);
|
||||
|
||||
run_blocking_ops();
|
||||
|
||||
SDL_CondWaitTimeout(thread->context.c, m, (1000/HZ) * ticks);
|
||||
running = thread;
|
||||
|
||||
if (thread->state == STATE_BLOCKED_W_TMO)
|
||||
{
|
||||
/* Timed out */
|
||||
remove_from_list_l(&tq->queue, thread);
|
||||
thread->state = STATE_RUNNING;
|
||||
}
|
||||
|
||||
if (threads_exit)
|
||||
remove_thread(NULL);
|
||||
}
|
||||
|
||||
struct thread_entry * _wakeup_thread(struct thread_queue *tq)
|
||||
{
|
||||
struct thread_entry *thread = tq->queue;
|
||||
|
||||
if (thread == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (thread->state)
|
||||
{
|
||||
case STATE_BLOCKED:
|
||||
case STATE_BLOCKED_W_TMO:
|
||||
remove_from_list_l(&tq->queue, thread);
|
||||
thread->state = STATE_RUNNING;
|
||||
SDL_CondSignal(thread->context.c);
|
||||
return thread;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void thread_thaw(struct thread_entry *thread)
|
||||
{
|
||||
if (thread->state == STATE_FROZEN)
|
||||
{
|
||||
thread->state = STATE_RUNNING;
|
||||
SDL_CondSignal(thread->context.c);
|
||||
}
|
||||
return thread;
|
||||
}
|
||||
|
||||
void init_threads(void)
|
||||
{
|
||||
/* Main thread is already initialized */
|
||||
if (running != &threads[0])
|
||||
if (cores[CURRENT_CORE].running != &threads[0])
|
||||
{
|
||||
THREAD_PANICF("Wrong main thread in init_threads: %p\n", running);
|
||||
THREAD_PANICF("Wrong main thread in init_threads: %p\n",
|
||||
cores[CURRENT_CORE].running);
|
||||
}
|
||||
|
||||
THREAD_SDL_DEBUGF("First Thread: %d (%s)\n",
|
||||
|
@ -465,9 +501,9 @@ void init_threads(void)
|
|||
|
||||
void remove_thread(struct thread_entry *thread)
|
||||
{
|
||||
struct thread_entry *current = running;
|
||||
struct thread_entry *current = cores[CURRENT_CORE].running;
|
||||
SDL_Thread *t;
|
||||
SDL_cond *c;
|
||||
SDL_sem *s;
|
||||
|
||||
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
||||
|
||||
|
@ -477,7 +513,7 @@ void remove_thread(struct thread_entry *thread)
|
|||
}
|
||||
|
||||
t = thread->context.t;
|
||||
c = thread->context.c;
|
||||
s = thread->context.s;
|
||||
thread->context.t = NULL;
|
||||
|
||||
if (thread != current)
|
||||
|
@ -487,20 +523,25 @@ void remove_thread(struct thread_entry *thread)
|
|||
case STATE_BLOCKED:
|
||||
case STATE_BLOCKED_W_TMO:
|
||||
/* Remove thread from object it's waiting on */
|
||||
remove_from_list_l(&thread->bqp->queue, thread);
|
||||
remove_from_list_l(thread->bqp, thread);
|
||||
|
||||
#ifdef HAVE_WAKEUP_EXT_CB
|
||||
if (thread->wakeup_ext_cb != NULL)
|
||||
thread->wakeup_ext_cb(thread);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_CondSignal(c);
|
||||
SDL_SemPost(s);
|
||||
}
|
||||
|
||||
THREAD_SDL_DEBUGF("Removing thread: %d (%s)\n",
|
||||
thread - threads, THREAD_SDL_GET_NAME(thread));
|
||||
|
||||
thread_queue_wake_no_listlock(&thread->queue);
|
||||
thread->state = STATE_KILLED;
|
||||
thread_queue_wake(&thread->queue);
|
||||
|
||||
SDL_DestroyCond(c);
|
||||
SDL_DestroySemaphore(s);
|
||||
|
||||
if (thread == current)
|
||||
{
|
||||
|
@ -514,14 +555,23 @@ void remove_thread(struct thread_entry *thread)
|
|||
set_irq_level(oldlevel);
|
||||
}
|
||||
|
||||
void thread_exit(void)
|
||||
{
|
||||
remove_thread(NULL);
|
||||
}
|
||||
|
||||
void thread_wait(struct thread_entry *thread)
|
||||
{
|
||||
struct thread_entry *current = cores[CURRENT_CORE].running;
|
||||
|
||||
if (thread == NULL)
|
||||
thread = running;
|
||||
thread = current;
|
||||
|
||||
if (thread->state != STATE_KILLED)
|
||||
{
|
||||
block_thread_no_listlock(&thread->queue);
|
||||
current->bqp = &thread->queue;
|
||||
block_thread(current);
|
||||
switch_thread();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,19 +40,13 @@
|
|||
#include "SDL_thread.h"
|
||||
|
||||
/* extern functions */
|
||||
extern void app_main (void *); /* mod entry point */
|
||||
extern void new_key(int key);
|
||||
extern void sim_tick_tasks(void);
|
||||
extern bool sim_io_init(void);
|
||||
extern void sim_io_shutdown(void);
|
||||
extern void new_key(int key);
|
||||
|
||||
void button_event(int key, bool pressed);
|
||||
|
||||
SDL_Surface *gui_surface;
|
||||
bool background = false; /* Don't use backgrounds by default */
|
||||
|
||||
SDL_TimerID tick_timer_id;
|
||||
|
||||
bool lcd_display_redraw = true; /* Used for player simulator */
|
||||
char having_new_lcd = true; /* Used for player simulator */
|
||||
bool sim_alarm_wakeup = false;
|
||||
|
@ -63,31 +57,6 @@ bool debug_audio = false;
|
|||
bool debug_wps = false;
|
||||
int wps_verbose_level = 3;
|
||||
|
||||
long start_tick;
|
||||
|
||||
Uint32 tick_timer(Uint32 interval, void *param)
|
||||
{
|
||||
long new_tick;
|
||||
|
||||
(void) interval;
|
||||
(void) param;
|
||||
|
||||
new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
|
||||
|
||||
if (new_tick != current_tick) {
|
||||
long i;
|
||||
for (i = new_tick - current_tick; i > 0; i--)
|
||||
{
|
||||
sim_enter_irq_handler();
|
||||
sim_tick_tasks();
|
||||
sim_exit_irq_handler();
|
||||
}
|
||||
current_tick = new_tick;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void gui_message_loop(void)
|
||||
{
|
||||
SDL_Event event;
|
||||
|
@ -181,8 +150,6 @@ bool gui_startup(void)
|
|||
SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
start_tick = SDL_GetTicks();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -191,7 +158,6 @@ bool gui_shutdown(void)
|
|||
/* Order here is relevent to prevent deadlocks and use of destroyed
|
||||
sync primitives by kernel threads */
|
||||
thread_sdl_shutdown();
|
||||
SDL_RemoveTimer(tick_timer_id);
|
||||
sim_kernel_shutdown();
|
||||
return true;
|
||||
}
|
||||
|
@ -287,8 +253,6 @@ int main(int argc, char *argv[])
|
|||
return -1;
|
||||
}
|
||||
|
||||
tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
|
||||
|
||||
gui_message_loop();
|
||||
|
||||
return gui_shutdown();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue