summaryrefslogtreecommitdiffstats
path: root/uisimulator
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2008-03-25 02:34:12 +0000
committerMichael Sevakis <jethead71@rockbox.org>2008-03-25 02:34:12 +0000
commit27cf67733936abd75fcb1f8da765977cd75906ee (patch)
treef894211a8a0c77b402dd3250b2bee2d17dcfe13f /uisimulator
parentbc2f8fd8f38a3e010cd67bbac358f6e9991153c6 (diff)
downloadrockbox-27cf67733936abd75fcb1f8da765977cd75906ee.tar.gz
rockbox-27cf67733936abd75fcb1f8da765977cd75906ee.zip
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
Diffstat (limited to 'uisimulator')
-rw-r--r--uisimulator/sdl/SOURCES2
-rw-r--r--uisimulator/sdl/kernel-sdl.c168
-rw-r--r--uisimulator/sdl/kernel.c739
-rw-r--r--uisimulator/sdl/system-sdl.h2
-rw-r--r--uisimulator/sdl/thread-sdl.c372
-rw-r--r--uisimulator/sdl/uisdl.c38
6 files changed, 383 insertions, 938 deletions
diff --git a/uisimulator/sdl/SOURCES b/uisimulator/sdl/SOURCES
index 7971c57163..1d5b498248 100644
--- a/uisimulator/sdl/SOURCES
+++ b/uisimulator/sdl/SOURCES
@@ -1,5 +1,5 @@
button.c
-kernel.c
+kernel-sdl.c
#ifdef HAVE_LCD_BITMAP
lcd-bitmap.c
#elif defined(HAVE_LCD_CHARCELLS)
diff --git a/uisimulator/sdl/kernel-sdl.c b/uisimulator/sdl/kernel-sdl.c
new file mode 100644
index 0000000000..b6e6a34551
--- /dev/null
+++ b/uisimulator/sdl/kernel-sdl.c
@@ -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);
+}
diff --git a/uisimulator/sdl/kernel.c b/uisimulator/sdl/kernel.c
deleted file mode 100644
index d67fb2b9f1..0000000000
--- a/uisimulator/sdl/kernel.c
+++ /dev/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 */
diff --git a/uisimulator/sdl/system-sdl.h b/uisimulator/sdl/system-sdl.h
index 2197a014c3..c5e7d40560 100644
--- a/uisimulator/sdl/system-sdl.h
+++ b/uisimulator/sdl/system-sdl.h
@@ -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_ */
diff --git a/uisimulator/sdl/thread-sdl.c b/uisimulator/sdl/thread-sdl.c
index d1a8e60d01..78a66f72a7 100644
--- a/uisimulator/sdl/thread-sdl.c
+++ b/uisimulator/sdl/thread-sdl.c
@@ -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);
- if (threads_exit)
- remove_thread(NULL);
+ 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;
+ }
- (void)old;
+ 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)
+ 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;
+}
+
+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 (rem == 0)
+ if (thread != NULL)
{
- /* Unlock and give up rest of quantum */
- SDL_UnlockMutex(m);
- SDL_Delay(0);
- SDL_LockMutex(m);
+ 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;
+ }
}
- else
+
+ return THREAD_NONE;
+}
+
+unsigned int thread_queue_wake(struct thread_entry **list)
+{
+ unsigned int result = THREAD_NONE;
+
+ for (;;)
{
- /* These sleeps must be signalable for thread exit */
- SDL_CondWaitTimeout(current->context.c, m, rem);
- }
+ unsigned int rc = wakeup_thread(list);
- running = current;
+ if (rc == THREAD_NONE)
+ break;
- current->state = STATE_RUNNING;
+ result |= rc;
+ }
- if (threads_exit)
- remove_thread(NULL);
+ 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();
}
}
diff --git a/uisimulator/sdl/uisdl.c b/uisimulator/sdl/uisdl.c
index e0a449ed48..09210926b5 100644
--- a/uisimulator/sdl/uisdl.c
+++ b/uisimulator/sdl/uisdl.c
@@ -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();