interrupt_level should've been volatile as well when I changed this file. Obtuseness...
[kugel-rb.git] / firmware / target / hosted / sdl / kernel-sdl.c
blobd933b9097ef3a924c1ee4cee70461f83b4fd36f0
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 /* Condition to signal that "interrupts" may proceed */
38 static SDL_cond *sim_thread_cond;
39 /* Mutex to serialize changing levels and exclude other threads while
40 * inside a handler */
41 static SDL_mutex *sim_irq_mtx;
42 /* Level: 0 = enabled, not 0 = disabled */
43 static int volatile interrupt_level = HIGHEST_IRQ_LEVEL;
44 /* How many handers waiting? Not strictly needed because CondSignal is a
45 * noop if no threads were waiting but it filters-out calls to functions
46 * with higher overhead and provides info when debugging. */
47 static int handlers_pending = 0;
48 /* 1 = executing a handler; prevents CondSignal calls in set_irq_level
49 * while in a handler */
50 static int status_reg = 0;
52 /* Nescessary logic:
53 * 1) All threads must pass unblocked
54 * 2) Current handler must always pass unblocked
55 * 3) Threads must be excluded when irq routine is running
56 * 4) No more than one handler routine should execute at a time
58 int set_irq_level(int level)
60 SDL_LockMutex(sim_irq_mtx);
62 int oldlevel = interrupt_level;
64 if (status_reg == 0 && level == 0 && oldlevel != 0)
66 /* Not in a handler and "interrupts" are going from disabled to
67 * enabled; signal any pending handlers still waiting */
68 if (handlers_pending > 0)
69 SDL_CondSignal(sim_thread_cond);
72 interrupt_level = level; /* save new level */
74 SDL_UnlockMutex(sim_irq_mtx);
75 return oldlevel;
78 void sim_enter_irq_handler(void)
80 SDL_LockMutex(sim_irq_mtx);
81 handlers_pending++;
83 /* Check each time before proceeding: disabled->enabled->...->disabled
84 * is possible on an app thread before a handler thread is ever granted
85 * the mutex; a handler can also leave "interrupts" disabled during
86 * its execution */
87 while (interrupt_level != 0)
88 SDL_CondWait(sim_thread_cond, sim_irq_mtx);
90 status_reg = 1;
93 void sim_exit_irq_handler(void)
95 /* If any others are waiting, give the signal */
96 if (--handlers_pending > 0)
97 SDL_CondSignal(sim_thread_cond);
99 status_reg = 0;
100 SDL_UnlockMutex(sim_irq_mtx);
103 static bool sim_kernel_init(void)
105 sim_irq_mtx = SDL_CreateMutex();
106 if (sim_irq_mtx == NULL)
108 panicf("Cannot create sim_handler_mtx\n");
109 return false;
112 sim_thread_cond = SDL_CreateCond();
113 if (sim_thread_cond == NULL)
115 panicf("Cannot create sim_thread_cond\n");
116 return false;
119 return true;
122 void sim_kernel_shutdown(void)
124 SDL_RemoveTimer(tick_timer_id);
125 SDL_DestroyMutex(sim_irq_mtx);
126 SDL_DestroyCond(sim_thread_cond);
129 Uint32 tick_timer(Uint32 interval, void *param)
131 long new_tick;
133 (void) interval;
134 (void) param;
136 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
138 while(new_tick != current_tick)
140 sim_enter_irq_handler();
142 /* Run through the list of tick tasks - increments tick
143 * on each iteration. */
144 call_tick_tasks();
146 sim_exit_irq_handler();
149 return interval;
152 void tick_start(unsigned int interval_in_ms)
154 if (!sim_kernel_init())
156 panicf("Could not initialize kernel!");
157 exit(-1);
160 if (tick_timer_id != NULL)
162 SDL_RemoveTimer(tick_timer_id);
163 tick_timer_id = NULL;
165 else
167 start_tick = SDL_GetTicks();
170 tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL);