From 2054627caa7f71c564ce35b4525b6281fcd2cc39 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Mon, 5 Jan 2009 10:31:19 +0000 Subject: Have the codec thread do callbacks instead of messing with the stack which is much simpler and safer. Remove threads array from plugin API since it now serves no purpose. Up minimum API version and sort. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19684 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/mpegplayer/audio_thread.c | 75 ++++++++-------------------------- apps/plugins/test_codec.c | 60 ++------------------------- 2 files changed, 20 insertions(+), 115 deletions(-) (limited to 'apps/plugins') diff --git a/apps/plugins/mpegplayer/audio_thread.c b/apps/plugins/mpegplayer/audio_thread.c index 2fb46efd56..f6552218b0 100644 --- a/apps/plugins/mpegplayer/audio_thread.c +++ b/apps/plugins/mpegplayer/audio_thread.c @@ -38,14 +38,7 @@ struct audio_thread_data struct dsp_config *dsp; /* The DSP we're using */ }; -/* The audio stack is stolen from the core codec thread (but not in uisim) */ -/* Used for stealing codec thread's stack */ -static uint32_t* audio_stack; -static size_t audio_stack_size; /* Keep gcc happy and init */ -#define AUDIO_STACKSIZE (9*1024) -#ifndef SIMULATOR -static uint32_t codec_stack_copy[AUDIO_STACKSIZE / sizeof(uint32_t)]; -#endif +/* The audio thread is stolen from the core codec thread */ static struct event_queue audio_str_queue SHAREDBSS_ATTR; static struct queue_sender_list audio_str_queue_send SHAREDBSS_ATTR; struct stream audio_str IBSS_ATTR; @@ -473,6 +466,11 @@ static void audio_thread_msg(struct audio_thread_data *td) static void audio_thread(void) { struct audio_thread_data td; +#ifdef HAVE_PRIORITY_SCHEDULING + /* Up the priority since the core DSP over-yields internally */ + int old_priority = rb->thread_set_priority(THREAD_ID_CURRENT, + PRIORITY_PLAYBACK-4); +#endif rb->memset(&td, 0, sizeof (td)); td.status = STREAM_STOPPED; @@ -512,7 +510,13 @@ static void audio_thread(void) case TSTATE_RENDER_WAIT: goto render_wait; case TSTATE_RENDER_WAIT_END: goto render_wait_end; /* Anything else is interpreted as an exit */ - default: return; + default: + { +#ifdef HAVE_PRIORITY_SCHEDULING + rb->thread_set_priority(THREAD_ID_CURRENT, old_priority); +#endif + return; + } } } @@ -677,43 +681,6 @@ static void audio_thread(void) /* Initializes the audio thread resources and starts the thread */ bool audio_thread_init(void) { - int i; -#ifdef SIMULATOR - /* The simulator thread implementation doesn't have stack buffers, and - these parameters are ignored. */ - (void)i; /* Keep gcc happy */ - audio_stack = NULL; - audio_stack_size = 0; -#else - /* Borrow the codec thread's stack (in IRAM on most targets) */ - audio_stack = NULL; - for (i = 0; i < MAXTHREADS; i++) - { - if (rb->strcmp(rb->threads[i].name, "codec") == 0) - { - /* Wait to ensure the codec thread has blocked */ - while (rb->threads[i].state != STATE_BLOCKED) - rb->yield(); - - /* Now we can steal the stack */ - audio_stack = rb->threads[i].stack; - audio_stack_size = rb->threads[i].stack_size; - - /* Backup the codec thread's stack */ - rb->memcpy(codec_stack_copy, audio_stack, audio_stack_size); - break; - } - } - - if (audio_stack == NULL) - { - /* This shouldn't happen, but deal with it anyway by using - the copy instead */ - audio_stack = codec_stack_copy; - audio_stack_size = AUDIO_STACKSIZE; - } -#endif - /* Initialise the encoded audio buffer and its descriptors */ audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE, MPEG_ALLOC_AUDIOBUF); @@ -724,17 +691,12 @@ bool audio_thread_init(void) audio_str.hdr.q = &audio_str_queue; rb->queue_init(audio_str.hdr.q, false); - /* 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-4) IF_COP(, CPU)); + /* We steal the codec thread for audio */ + rb->codec_thread_do_callback(audio_thread, &audio_str.thread); rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send, audio_str.thread); - if (audio_str.thread == 0) - return false; - /* Wait for thread to initialize */ str_send_msg(&audio_str, STREAM_NULL, 0); @@ -747,12 +709,7 @@ void audio_thread_exit(void) if (audio_str.thread != 0) { str_post_msg(&audio_str, STREAM_QUIT, 0); - rb->thread_wait(audio_str.thread); + rb->codec_thread_do_callback(NULL, NULL); audio_str.thread = 0; } - -#ifndef SIMULATOR - /* Restore the codec thread's stack */ - rb->memcpy(audio_stack, codec_stack_copy, audio_stack_size); -#endif } diff --git a/apps/plugins/test_codec.c b/apps/plugins/test_codec.c index 17effd8dfd..20a1febd4f 100644 --- a/apps/plugins/test_codec.c +++ b/apps/plugins/test_codec.c @@ -513,9 +513,6 @@ static void codec_thread(void) codec_playing = false; } -static uintptr_t* codec_stack; -static size_t codec_stack_size; - static enum plugin_status test_track(const char* filename) { size_t n; @@ -525,7 +522,6 @@ static enum plugin_status test_track(const char* filename) long ticks; unsigned long speed; unsigned long duration; - unsigned int codecthread_id; const char* ch; /* Display filename (excluding any path)*/ @@ -588,13 +584,7 @@ static enum plugin_status test_track(const char* filename) codec_playing = true; - if ((codecthread_id = rb->create_thread(codec_thread, - codec_stack, codec_stack_size, 0, "testcodec" - IF_PRIO(,PRIORITY_PLAYBACK) IF_COP(, CPU))) == 0) - { - log_text("Cannot create codec thread!",true); - goto exit; - } + rb->codec_thread_do_callback(codec_thread, NULL); /* Wait for codec thread to die */ while (codec_playing) @@ -606,7 +596,7 @@ static enum plugin_status test_track(const char* filename) ticks = endtick - starttick; /* Be sure it is done */ - rb->thread_wait(codecthread_id); + rb->codec_thread_do_callback(NULL, NULL); log_text(str,true); @@ -656,11 +646,9 @@ exit: /* plugin entry point */ enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) { - uintptr_t* codec_stack_copy; int result, selection = 0; enum plugin_status res = PLUGIN_OK; int scandir; - int i; struct dirent *entry; DIR* dir; char* ch; @@ -676,43 +664,8 @@ enum plugin_status plugin_start(const struct plugin_api* api, const void* parame } codec_mallocbuf = rb->plugin_get_audio_buffer(&audiosize); - -#ifdef SIMULATOR - /* The simulator thread implementation doesn't have stack buffers */ - (void)i; - codec_stack_size = 0; -#else - /* Borrow the codec thread's stack (in IRAM on most targets) */ - codec_stack = NULL; - for (i = 0; i < MAXTHREADS; i++) - { - if (rb->strcmp(rb->threads[i].name,"codec")==0) - { - /* Wait to ensure the codec thread has blocked */ - while (rb->threads[i].state!=STATE_BLOCKED) - rb->yield(); - - codec_stack = rb->threads[i].stack; - codec_stack_size = rb->threads[i].stack_size; - break; - } - } - - if (codec_stack == NULL) - { - rb->splash(HZ*2, "No codec thread!"); - return PLUGIN_ERROR; - } -#endif - - codec_stack_copy = codec_mallocbuf + CODEC_SIZE; - audiobuf = SKIPBYTES(codec_stack_copy, codec_stack_size); - audiosize -= CODEC_SIZE + codec_stack_size; - -#ifndef SIMULATOR - /* Backup the codec thread's stack */ - rb->memcpy(codec_stack_copy,codec_stack,codec_stack_size); -#endif + audiobuf = SKIPBYTES(codec_mallocbuf, CODEC_SIZE); + audiosize -= CODEC_SIZE; #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(true); @@ -803,11 +756,6 @@ enum plugin_status plugin_start(const struct plugin_api* api, const void* parame exit: log_close(); -#ifndef SIMULATOR - /* Restore the codec thread's stack */ - rb->memcpy(codec_stack, codec_stack_copy, codec_stack_size); -#endif - #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(false); #endif -- cgit v1.2.3