summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2006-01-13 13:38:31 +0000
committerDave Chapman <dave@dchapman.com>2006-01-13 13:38:31 +0000
commitbae4e2acdde5c563249e452d22c4fd58ea7433a4 (patch)
treec5977000dca32ac1008ad23848d8835c419ec6c0
parentb855a9aeb68dfa8da6b760cba4e816e71d53b050 (diff)
downloadrockbox-bae4e2acdde5c563249e452d22c4fd58ea7433a4.tar.gz
rockbox-bae4e2acdde5c563249e452d22c4fd58ea7433a4.zip
Use SDL's thread wrappers instead of pthreads to increase portability in the SDL sim. Patch by Andrew Pilley with some changes by me.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8347 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--docs/CREDITS1
-rw-r--r--uisimulator/sdl/sound.c4
-rw-r--r--uisimulator/sdl/sound.h2
-rw-r--r--uisimulator/sdl/thread.c62
4 files changed, 34 insertions, 35 deletions
diff --git a/docs/CREDITS b/docs/CREDITS
index f388d1452b..78be75b1a6 100644
--- a/docs/CREDITS
+++ b/docs/CREDITS
@@ -158,3 +158,4 @@ Sebastian Henriksen
158Martin Scarratt 158Martin Scarratt
159Karl Kurbjun 159Karl Kurbjun
160Tomasz Malesinski 160Tomasz Malesinski
161Andrew Pilley
diff --git a/uisimulator/sdl/sound.c b/uisimulator/sdl/sound.c
index 6068fb3863..388b188448 100644
--- a/uisimulator/sdl/sound.c
+++ b/uisimulator/sdl/sound.c
@@ -88,12 +88,14 @@ int sim_sound_init(void)
88 //SDL_CloseAudio(); 88 //SDL_CloseAudio();
89} 89}
90 90
91void sound_playback_thread(void) 91int sound_playback_thread(void* p)
92{ 92{
93 int sndret = sim_sound_init(); 93 int sndret = sim_sound_init();
94 unsigned char *buf; 94 unsigned char *buf;
95 long size; 95 long size;
96 96
97 (void)p;
98
97 while(sndret) 99 while(sndret)
98 sleep(100000); /* wait forever, can't play sound! */ 100 sleep(100000); /* wait forever, can't play sound! */
99 101
diff --git a/uisimulator/sdl/sound.h b/uisimulator/sdl/sound.h
index 87499cac8e..4392991832 100644
--- a/uisimulator/sdl/sound.h
+++ b/uisimulator/sdl/sound.h
@@ -17,6 +17,6 @@
17 * 17 *
18 ****************************************************************************/ 18 ****************************************************************************/
19 19
20void sound_playback_thread(void); 20int sound_playback_thread(void* p);
21 21
22extern void (*sound_get_pcm)(unsigned char** start, long* size); 22extern void (*sound_get_pcm)(unsigned char** start, long* size);
diff --git a/uisimulator/sdl/thread.c b/uisimulator/sdl/thread.c
index 6d9139c35d..cb109b5f56 100644
--- a/uisimulator/sdl/thread.c
+++ b/uisimulator/sdl/thread.c
@@ -20,7 +20,10 @@
20#include "autoconf.h" 20#include "autoconf.h"
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <pthread.h> 23
24/* SDL threading wrapper */
25#include <SDL.h>
26#include <SDL_thread.h>
24 27
25#include "kernel.h" 28#include "kernel.h"
26#include <sys/time.h> 29#include <sys/time.h>
@@ -45,11 +48,13 @@ static void msleep(int msec)
45 * This is not a target thread, so it does not fall under the 1 thread at a 48 * This is not a target thread, so it does not fall under the 1 thread at a
46 * time thing. 49 * time thing.
47 */ 50 */
48static void update_tick_thread() 51static int update_tick_thread(void* p)
49{ 52{
50 struct timeval start, now; 53 struct timeval start, now;
51 long new_tick; 54 long new_tick;
52 55
56 (void)p;
57
53 gettimeofday(&start, NULL); 58 gettimeofday(&start, NULL);
54 while (1) 59 while (1)
55 { 60 {
@@ -66,73 +71,64 @@ static void update_tick_thread()
66} 71}
67 72
68/* 73/*
69 * We emulate the target threads by using pthreads. We have a mutex that only 74 * We emulate the target threads by using SDL threads. We have a mutex
70 * allows one thread at a time to execute. It forces each thread to yield() 75 * that only allows one thread at a time to execute. It forces each
71 * for the other(s) to run. 76 * thread to yield() for the other(s) to run.
72 */ 77 */
73 78
74pthread_mutex_t mp; 79SDL_mutex * mp;
75 80
76void init_threads(void) 81void init_threads(void)
77{ 82{
78 pthread_t tick_tid; 83 SDL_Thread *tick_tid;
79 84
80 pthread_mutex_init(&mp, NULL); 85 mp=SDL_CreateMutex();
81 /* get mutex to only allow one thread running at a time */ 86 /* get mutex to only allow one thread running at a time */
82 pthread_mutex_lock(&mp); 87 SDL_mutexP(mp);
83 88
84 /* start a tick thread */ 89 /* start a tick thread */
85 pthread_create(&tick_tid, NULL, (void *(*)(void *)) update_tick_thread, 90 tick_tid=SDL_CreateThread(update_tick_thread, NULL);
86 NULL);
87 91
88#ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */ 92#ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */
89 { 93 {
90 pthread_t sound_tid; 94 SDL_Thread *sound_tid;
91 pthread_create(&sound_tid, NULL, 95 sound_tid = SDL_CreateThread(sound_playback_thread, NULL);
92 (void *(*)(void *)) sound_playback_thread,
93 NULL);
94 } 96 }
95#endif 97#endif
96 98
97} 99}
98/*
99 int pthread_create(pthread_t *new_thread_ID,
100 const pthread_attr_t *attr,
101 void * (*start_func)(void *), void *arg);
102*/
103 100
104void yield(void) 101void yield(void)
105{ 102{
106 pthread_mutex_unlock(&mp); /* return */ 103 SDL_mutexV(mp); /* return */
107 msleep(1); /* prevent busy loop */ 104 msleep(1); /* prevent busy loop */
108 pthread_mutex_lock(&mp); /* get it again */ 105 SDL_mutexP(mp); /* get it again */
109} 106}
110 107
111void newfunc(void (*func)(void)) 108void newfunc(void (*func)(void))
112{ 109{
113 pthread_mutex_lock(&mp); 110 SDL_mutexP(mp);
114 func(); 111 func();
115 pthread_mutex_unlock(&mp); 112 SDL_mutexV(mp);
116} 113}
117 114
118 115
119int create_thread(void (*fp)(void), void* sp, int stk_size) 116int create_thread(void (*fp)(void), void* sp, int stk_size)
120{ 117{
121 pthread_t tid; 118 SDL_Thread * tid;
122 int i; 119 int i;
123 int error; 120 int error;
124 121
125 /* we really don't care about these arguments */ 122 /* we really don't care about these arguments */
126 (void)sp; 123 (void)sp;
127 (void)stk_size; 124 (void)stk_size;
128 error = pthread_create(&tid, 125 tid = SDL_CreateThread(
129 NULL, /* default attributes please */ 126 (int(*)(void *))newfunc, /* function to start */
130 (void *(*)(void *)) newfunc, /* function to start */
131 fp /* start argument */); 127 fp /* start argument */);
132 if(0 != error) 128 if(0 == tid) /* don't really have an error number here. */
133 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); 129 fprintf(stderr, "Couldn't run thread number %d\n", i);
134 else 130 else
135 fprintf(stderr, "Thread %ld is running\n", (long)tid); 131 fprintf(stderr, "Thread %d is running\n", (int)SDL_GetThreadID(tid));
136 132
137 yield(); 133 yield();
138 134
@@ -141,8 +137,8 @@ int create_thread(void (*fp)(void), void* sp, int stk_size)
141 137
142void sim_sleep(int ticks) 138void sim_sleep(int ticks)
143{ 139{
144 pthread_mutex_unlock(&mp); /* return */ 140 SDL_mutexV(mp); /* return */
145 msleep((1000/HZ) * ticks); 141 msleep((1000/HZ) * ticks);
146 pthread_mutex_lock(&mp); /* get it again */ 142 SDL_mutexP(mp); /* get it again */
147} 143}
148 144