1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 #include <SDL_thread.h>
27 #include "system-sdl.h"
28 #include "thread-sdl.h"
34 static SDL_TimerID tick_timer_id
;
37 #ifndef HAVE_SDL_THREADS
38 /* for the wait_for_interrupt function */
40 static SDL_cond
*wfi_cond
;
41 static SDL_mutex
*wfi_mutex
;
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
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;
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
);
86 void sim_enter_irq_handler(void)
88 SDL_LockMutex(sim_irq_mtx
);
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
95 while (interrupt_level
!= 0)
96 SDL_CondWait(sim_thread_cond
, sim_irq_mtx
);
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
);
108 SDL_UnlockMutex(sim_irq_mtx
);
109 #ifndef HAVE_SDL_THREADS
110 SDL_CondSignal(wfi_cond
);
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");
123 sim_thread_cond
= SDL_CreateCond();
124 if (sim_thread_cond
== NULL
)
126 panicf("Cannot create sim_thread_cond\n");
129 #ifndef HAVE_SDL_THREADS
130 wfi_cond
= SDL_CreateCond();
131 if (wfi_cond
== NULL
)
133 panicf("Cannot create wfi\n");
136 wfi_mutex
= SDL_CreateMutex();
137 if (wfi_mutex
== NULL
)
139 panicf("Cannot create wfi mutex\n");
146 void sim_kernel_shutdown(void)
148 SDL_RemoveTimer(tick_timer_id
);
149 #ifndef HAVE_SDL_THREADS
151 SDL_CondSignal(wfi_cond
);
154 SDL_DestroyMutex(sim_irq_mtx
);
155 SDL_DestroyCond(sim_thread_cond
);
158 Uint32
tick_timer(Uint32 interval
, 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. */
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!");
189 if (tick_timer_id
!= NULL
)
191 SDL_RemoveTimer(tick_timer_id
);
192 tick_timer_id
= NULL
;
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
);
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
);
217 void wait_for_interrupt(void)
219 /* the exit may come at any time, during the CondWait or before,
220 * so check it twice */
222 SDL_CondWait(wfi_cond
, wfi_mutex
);