diff options
Diffstat (limited to 'uisimulator/sdl/thread.c')
-rw-r--r-- | uisimulator/sdl/thread.c | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/uisimulator/sdl/thread.c b/uisimulator/sdl/thread.c deleted file mode 100644 index cb109b5f56..0000000000 --- a/uisimulator/sdl/thread.c +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Daniel Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "autoconf.h" | ||
21 | |||
22 | #include <stdio.h> | ||
23 | |||
24 | /* SDL threading wrapper */ | ||
25 | #include <SDL.h> | ||
26 | #include <SDL_thread.h> | ||
27 | |||
28 | #include "kernel.h" | ||
29 | #include <sys/time.h> | ||
30 | |||
31 | #ifdef ROCKBOX_HAS_SIMSOUND | ||
32 | #include "sound.h" | ||
33 | #endif | ||
34 | |||
35 | long current_tick = 0; | ||
36 | extern void sim_tick_tasks(void); | ||
37 | |||
38 | static void msleep(int msec) | ||
39 | { | ||
40 | struct timeval delay; | ||
41 | |||
42 | delay.tv_sec = msec / 1000; | ||
43 | delay.tv_usec = (msec % 1000) * 1000; | ||
44 | select(0, NULL, NULL, NULL, &delay); /* portable sub-second sleep */ | ||
45 | } | ||
46 | |||
47 | /* | ||
48 | * This is not a target thread, so it does not fall under the 1 thread at a | ||
49 | * time thing. | ||
50 | */ | ||
51 | static int update_tick_thread(void* p) | ||
52 | { | ||
53 | struct timeval start, now; | ||
54 | long new_tick; | ||
55 | |||
56 | (void)p; | ||
57 | |||
58 | gettimeofday(&start, NULL); | ||
59 | while (1) | ||
60 | { | ||
61 | msleep(5); /* check twice per simulated target tick */ | ||
62 | gettimeofday(&now, NULL); | ||
63 | new_tick = (now.tv_sec - start.tv_sec) * HZ | ||
64 | + (now.tv_usec - start.tv_usec) / (1000000/HZ); | ||
65 | if (new_tick > current_tick) | ||
66 | { | ||
67 | sim_tick_tasks(); | ||
68 | current_tick = new_tick; | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | /* | ||
74 | * We emulate the target threads by using SDL threads. We have a mutex | ||
75 | * that only allows one thread at a time to execute. It forces each | ||
76 | * thread to yield() for the other(s) to run. | ||
77 | */ | ||
78 | |||
79 | SDL_mutex * mp; | ||
80 | |||
81 | void init_threads(void) | ||
82 | { | ||
83 | SDL_Thread *tick_tid; | ||
84 | |||
85 | mp=SDL_CreateMutex(); | ||
86 | /* get mutex to only allow one thread running at a time */ | ||
87 | SDL_mutexP(mp); | ||
88 | |||
89 | /* start a tick thread */ | ||
90 | tick_tid=SDL_CreateThread(update_tick_thread, NULL); | ||
91 | |||
92 | #ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */ | ||
93 | { | ||
94 | SDL_Thread *sound_tid; | ||
95 | sound_tid = SDL_CreateThread(sound_playback_thread, NULL); | ||
96 | } | ||
97 | #endif | ||
98 | |||
99 | } | ||
100 | |||
101 | void yield(void) | ||
102 | { | ||
103 | SDL_mutexV(mp); /* return */ | ||
104 | msleep(1); /* prevent busy loop */ | ||
105 | SDL_mutexP(mp); /* get it again */ | ||
106 | } | ||
107 | |||
108 | void newfunc(void (*func)(void)) | ||
109 | { | ||
110 | SDL_mutexP(mp); | ||
111 | func(); | ||
112 | SDL_mutexV(mp); | ||
113 | } | ||
114 | |||
115 | |||
116 | int create_thread(void (*fp)(void), void* sp, int stk_size) | ||
117 | { | ||
118 | SDL_Thread * tid; | ||
119 | int i; | ||
120 | int error; | ||
121 | |||
122 | /* we really don't care about these arguments */ | ||
123 | (void)sp; | ||
124 | (void)stk_size; | ||
125 | tid = SDL_CreateThread( | ||
126 | (int(*)(void *))newfunc, /* function to start */ | ||
127 | fp /* start argument */); | ||
128 | if(0 == tid) /* don't really have an error number here. */ | ||
129 | fprintf(stderr, "Couldn't run thread number %d\n", i); | ||
130 | else | ||
131 | fprintf(stderr, "Thread %d is running\n", (int)SDL_GetThreadID(tid)); | ||
132 | |||
133 | yield(); | ||
134 | |||
135 | return error; | ||
136 | } | ||
137 | |||
138 | void sim_sleep(int ticks) | ||
139 | { | ||
140 | SDL_mutexV(mp); /* return */ | ||
141 | msleep((1000/HZ) * ticks); | ||
142 | SDL_mutexP(mp); /* get it again */ | ||
143 | } | ||
144 | |||