FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / firmware / target / hosted / sdl / kernel-sdl.c
blob4fb1aede0a7e5f3f2d07af5003ca23ad2130e9fb
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 <stdio.h>
24 #include <SDL.h>
25 #include <SDL_thread.h>
26 #include <inttypes.h>
27 #include "system-sdl.h"
28 #include "thread-sdl.h"
29 #include "kernel.h"
30 #include "thread.h"
31 #include "panic.h"
32 #include "debug.h"
34 static SDL_TimerID tick_timer_id;
35 long start_tick;
37 #ifndef HAVE_SDL_THREADS
38 /* for the wait_for_interrupt function */
39 static bool do_exit;
40 static SDL_cond *wfi_cond;
41 static SDL_mutex *wfi_mutex;
42 #else
43 #define do_exit false
44 #endif
45 /* Condition to signal that "interrupts" may proceed */
46 static SDL_cond *sim_thread_cond;
47 /* Mutex to serialize changing levels and exclude other threads while
48 * inside a handler */
49 static SDL_mutex *sim_irq_mtx;
50 /* Level: 0 = enabled, not 0 = disabled */
51 static int volatile interrupt_level = HIGHEST_IRQ_LEVEL;
52 /* How many handers waiting? Not strictly needed because CondSignal is a
53 * noop if no threads were waiting but it filters-out calls to functions
54 * with higher overhead and provides info when debugging. */
55 static int handlers_pending = 0;
56 /* 1 = executing a handler; prevents CondSignal calls in set_irq_level
57 * while in a handler */
58 static int status_reg = 0;
60 /* Nescessary logic:
61 * 1) All threads must pass unblocked
62 * 2) Current handler must always pass unblocked
63 * 3) Threads must be excluded when irq routine is running
64 * 4) No more than one handler routine should execute at a time
66 int set_irq_level(int level)
68 SDL_LockMutex(sim_irq_mtx);
70 int oldlevel = interrupt_level;
72 if (status_reg == 0 && level == 0 && oldlevel != 0)
74 /* Not in a handler and "interrupts" are going from disabled to
75 * enabled; signal any pending handlers still waiting */
76 if (handlers_pending > 0)
77 SDL_CondSignal(sim_thread_cond);
80 interrupt_level = level; /* save new level */
82 SDL_UnlockMutex(sim_irq_mtx);
83 return oldlevel;
86 void sim_enter_irq_handler(void)
88 SDL_LockMutex(sim_irq_mtx);
89 handlers_pending++;
91 /* Check each time before proceeding: disabled->enabled->...->disabled
92 * is possible on an app thread before a handler thread is ever granted
93 * the mutex; a handler can also leave "interrupts" disabled during
94 * its execution */
95 while (interrupt_level != 0)
96 SDL_CondWait(sim_thread_cond, sim_irq_mtx);
98 status_reg = 1;
101 void sim_exit_irq_handler(void)
103 /* If any others are waiting, give the signal */
104 if (--handlers_pending > 0)
105 SDL_CondSignal(sim_thread_cond);
107 status_reg = 0;
108 SDL_UnlockMutex(sim_irq_mtx);
109 #ifndef HAVE_SDL_THREADS
110 SDL_CondSignal(wfi_cond);
111 #endif
114 static bool sim_kernel_init(void)
116 sim_irq_mtx = SDL_CreateMutex();
117 if (sim_irq_mtx == NULL)
119 panicf("Cannot create sim_handler_mtx\n");
120 return false;
123 sim_thread_cond = SDL_CreateCond();
124 if (sim_thread_cond == NULL)
126 panicf("Cannot create sim_thread_cond\n");
127 return false;
129 #ifndef HAVE_SDL_THREADS
130 wfi_cond = SDL_CreateCond();
131 if (wfi_cond == NULL)
133 panicf("Cannot create wfi\n");
134 return false;
136 wfi_mutex = SDL_CreateMutex();
137 if (wfi_mutex == NULL)
139 panicf("Cannot create wfi mutex\n");
140 return false;
142 #endif
143 return true;
146 void sim_kernel_shutdown(void)
148 SDL_RemoveTimer(tick_timer_id);
149 #ifndef HAVE_SDL_THREADS
150 do_exit = true;
151 SDL_CondSignal(wfi_cond);
152 #endif
153 disable_irq();
154 SDL_DestroyMutex(sim_irq_mtx);
155 SDL_DestroyCond(sim_thread_cond);
158 Uint32 tick_timer(Uint32 interval, void *param)
160 long new_tick;
162 (void) interval;
163 (void) param;
165 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
167 while(new_tick != current_tick && !do_exit)
169 sim_enter_irq_handler();
171 /* Run through the list of tick tasks - increments tick
172 * on each iteration. */
173 call_tick_tasks();
175 sim_exit_irq_handler();
178 return do_exit ? 0 : interval;
181 void tick_start(unsigned int interval_in_ms)
183 if (!sim_kernel_init())
185 panicf("Could not initialize kernel!");
186 exit(-1);
189 if (tick_timer_id != NULL)
191 SDL_RemoveTimer(tick_timer_id);
192 tick_timer_id = NULL;
194 else
196 start_tick = SDL_GetTicks();
199 tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL);
200 #ifndef HAVE_SDL_THREADS
201 SDL_LockMutex(wfi_mutex);
202 #endif
205 #ifndef HAVE_SDL_THREADS
206 static void check_exit(void)
208 if (UNLIKELY(do_exit))
210 SDL_DestroyCond(wfi_cond);
211 SDL_UnlockMutex(wfi_mutex);
212 SDL_DestroyMutex(wfi_mutex);
213 sim_do_exit();
217 void wait_for_interrupt(void)
219 /* the exit may come at any time, during the CondWait or before,
220 * so check it twice */
221 check_exit();
222 SDL_CondWait(wfi_cond, wfi_mutex);
223 check_exit();
225 #endif