diff options
Diffstat (limited to 'uisimulator/sdl/thread-sdl.c')
-rw-r--r-- | uisimulator/sdl/thread-sdl.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/uisimulator/sdl/thread-sdl.c b/uisimulator/sdl/thread-sdl.c index aac8e2fbb9..8769b5d84f 100644 --- a/uisimulator/sdl/thread-sdl.c +++ b/uisimulator/sdl/thread-sdl.c | |||
@@ -48,6 +48,8 @@ static SDL_mutex *m; | |||
48 | static SDL_sem *s; | 48 | static SDL_sem *s; |
49 | static struct thread_entry *running; | 49 | static struct thread_entry *running; |
50 | 50 | ||
51 | extern long start_tick; | ||
52 | |||
51 | void kill_sim_threads(void) | 53 | void kill_sim_threads(void) |
52 | { | 54 | { |
53 | int i; | 55 | int i; |
@@ -62,6 +64,7 @@ void kill_sim_threads(void) | |||
62 | SDL_SemPost(s); | 64 | SDL_SemPost(s); |
63 | else | 65 | else |
64 | SDL_CondSignal(thread->context.c); | 66 | SDL_CondSignal(thread->context.c); |
67 | SDL_Delay(10); | ||
65 | SDL_KillThread(thread->context.t); | 68 | SDL_KillThread(thread->context.t); |
66 | SDL_DestroyCond(thread->context.c); | 69 | SDL_DestroyCond(thread->context.c); |
67 | } | 70 | } |
@@ -129,6 +132,16 @@ struct thread_entry *thread_get_current(void) | |||
129 | return running; | 132 | return running; |
130 | } | 133 | } |
131 | 134 | ||
135 | void thread_sdl_lock(void) | ||
136 | { | ||
137 | SDL_LockMutex(m); | ||
138 | } | ||
139 | |||
140 | void thread_sdl_unlock(void) | ||
141 | { | ||
142 | SDL_UnlockMutex(m); | ||
143 | } | ||
144 | |||
132 | void switch_thread(bool save_context, struct thread_entry **blocked_list) | 145 | void switch_thread(bool save_context, struct thread_entry **blocked_list) |
133 | { | 146 | { |
134 | struct thread_entry *current = running; | 147 | struct thread_entry *current = running; |
@@ -137,6 +150,8 @@ void switch_thread(bool save_context, struct thread_entry **blocked_list) | |||
137 | 150 | ||
138 | SDL_SemWait(s); | 151 | SDL_SemWait(s); |
139 | 152 | ||
153 | SDL_Delay(0); | ||
154 | |||
140 | SDL_LockMutex(m); | 155 | SDL_LockMutex(m); |
141 | running = current; | 156 | running = current; |
142 | 157 | ||
@@ -148,11 +163,19 @@ void switch_thread(bool save_context, struct thread_entry **blocked_list) | |||
148 | void sleep_thread(int ticks) | 163 | void sleep_thread(int ticks) |
149 | { | 164 | { |
150 | struct thread_entry *current; | 165 | struct thread_entry *current; |
166 | int rem; | ||
151 | 167 | ||
152 | current = running; | 168 | current = running; |
153 | current->statearg = STATE_SLEEPING; | 169 | current->statearg = STATE_SLEEPING; |
154 | 170 | ||
155 | SDL_CondWaitTimeout(current->context.c, m, (1000/HZ) * ticks + (500/HZ)); | 171 | rem = (SDL_GetTicks() - start_tick) % (1000/HZ); |
172 | if (rem < 0) | ||
173 | rem = 0; | ||
174 | |||
175 | SDL_UnlockMutex(m); | ||
176 | SDL_Delay((1000/HZ) * ticks + ((1000/HZ)-1) - rem); | ||
177 | SDL_LockMutex(m); | ||
178 | |||
156 | running = current; | 179 | running = current; |
157 | 180 | ||
158 | current->statearg = STATE_RUNNING; | 181 | current->statearg = STATE_RUNNING; |