Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / uisimulator / sdl / kernel-sdl.c
blob3a1d82bf241ef5f95e069c34eb5eafceb2d0742d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Felix Arends
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdlib.h>
23 #include <SDL.h>
24 #include <SDL_thread.h>
25 #include "memory.h"
26 #include "system-sdl.h"
27 #include "uisdl.h"
28 #include "kernel.h"
29 #include "thread-sdl.h"
30 #include "thread.h"
31 #include "debug.h"
33 static SDL_TimerID tick_timer_id;
34 long start_tick;
36 /* Condition to signal that "interrupts" may proceed */
37 static SDL_cond *sim_thread_cond;
38 /* Mutex to serialize changing levels and exclude other threads while
39 * inside a handler */
40 static SDL_mutex *sim_irq_mtx;
41 static int interrupt_level = HIGHEST_IRQ_LEVEL;
42 static int handlers_pending = 0;
43 static int status_reg = 0;
45 extern void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
47 /* Nescessary logic:
48 * 1) All threads must pass unblocked
49 * 2) Current handler must always pass unblocked
50 * 3) Threads must be excluded when irq routine is running
51 * 4) No more than one handler routine should execute at a time
53 int set_irq_level(int level)
55 SDL_LockMutex(sim_irq_mtx);
57 int oldlevel = interrupt_level;
59 if (status_reg == 0 && level == 0 && oldlevel != 0)
61 /* Not in a handler and "interrupts" are being reenabled */
62 if (handlers_pending > 0)
63 SDL_CondSignal(sim_thread_cond);
66 interrupt_level = level; /* save new level */
68 SDL_UnlockMutex(sim_irq_mtx);
69 return oldlevel;
72 void sim_enter_irq_handler(void)
74 SDL_LockMutex(sim_irq_mtx);
75 handlers_pending++;
77 if(interrupt_level != 0)
79 /* "Interrupts" are disabled. Wait for reenable */
80 SDL_CondWait(sim_thread_cond, sim_irq_mtx);
83 status_reg = 1;
86 void sim_exit_irq_handler(void)
88 if (--handlers_pending > 0)
89 SDL_CondSignal(sim_thread_cond);
91 status_reg = 0;
92 SDL_UnlockMutex(sim_irq_mtx);
95 bool sim_kernel_init(void)
97 sim_irq_mtx = SDL_CreateMutex();
98 if (sim_irq_mtx == NULL)
100 fprintf(stderr, "Cannot create sim_handler_mtx\n");
101 return false;
104 sim_thread_cond = SDL_CreateCond();
105 if (sim_thread_cond == NULL)
107 fprintf(stderr, "Cannot create sim_thread_cond\n");
108 return false;
111 return true;
114 void sim_kernel_shutdown(void)
116 SDL_RemoveTimer(tick_timer_id);
117 SDL_DestroyMutex(sim_irq_mtx);
118 SDL_DestroyCond(sim_thread_cond);
121 Uint32 tick_timer(Uint32 interval, void *param)
123 long new_tick;
125 (void) interval;
126 (void) param;
128 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
130 if(new_tick != current_tick)
132 long t;
133 for(t = new_tick - current_tick; t > 0; t--)
135 int i;
137 sim_enter_irq_handler();
139 /* Run through the list of tick tasks */
140 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
142 if(tick_funcs[i])
144 tick_funcs[i]();
148 sim_exit_irq_handler();
151 current_tick = new_tick;
154 return 1;
157 void tick_start(unsigned int interval_in_ms)
159 if (tick_timer_id != NULL)
161 SDL_RemoveTimer(tick_timer_id);
162 tick_timer_id = NULL;
164 else
166 start_tick = SDL_GetTicks();
169 tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL);