1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Dave Chapman 2006-01-13 13:38:31 +00:00
parent b855a9aeb6
commit bae4e2acdd
4 changed files with 34 additions and 35 deletions

View file

@ -158,3 +158,4 @@ Sebastian Henriksen
Martin Scarratt Martin Scarratt
Karl Kurbjun Karl Kurbjun
Tomasz Malesinski Tomasz Malesinski
Andrew Pilley

View file

@ -88,12 +88,14 @@ int sim_sound_init(void)
//SDL_CloseAudio(); //SDL_CloseAudio();
} }
void sound_playback_thread(void) int sound_playback_thread(void* p)
{ {
int sndret = sim_sound_init(); int sndret = sim_sound_init();
unsigned char *buf; unsigned char *buf;
long size; long size;
(void)p;
while(sndret) while(sndret)
sleep(100000); /* wait forever, can't play sound! */ sleep(100000); /* wait forever, can't play sound! */

View file

@ -17,6 +17,6 @@
* *
****************************************************************************/ ****************************************************************************/
void sound_playback_thread(void); int sound_playback_thread(void* p);
extern void (*sound_get_pcm)(unsigned char** start, long* size); extern void (*sound_get_pcm)(unsigned char** start, long* size);

View file

@ -20,7 +20,10 @@
#include "autoconf.h" #include "autoconf.h"
#include <stdio.h> #include <stdio.h>
#include <pthread.h>
/* SDL threading wrapper */
#include <SDL.h>
#include <SDL_thread.h>
#include "kernel.h" #include "kernel.h"
#include <sys/time.h> #include <sys/time.h>
@ -45,11 +48,13 @@ static void msleep(int msec)
* This is not a target thread, so it does not fall under the 1 thread at a * This is not a target thread, so it does not fall under the 1 thread at a
* time thing. * time thing.
*/ */
static void update_tick_thread() static int update_tick_thread(void* p)
{ {
struct timeval start, now; struct timeval start, now;
long new_tick; long new_tick;
(void)p;
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
while (1) while (1)
{ {
@ -66,73 +71,64 @@ static void update_tick_thread()
} }
/* /*
* We emulate the target threads by using pthreads. We have a mutex that only * We emulate the target threads by using SDL threads. We have a mutex
* allows one thread at a time to execute. It forces each thread to yield() * that only allows one thread at a time to execute. It forces each
* for the other(s) to run. * thread to yield() for the other(s) to run.
*/ */
pthread_mutex_t mp; SDL_mutex * mp;
void init_threads(void) void init_threads(void)
{ {
pthread_t tick_tid; SDL_Thread *tick_tid;
pthread_mutex_init(&mp, NULL); mp=SDL_CreateMutex();
/* get mutex to only allow one thread running at a time */ /* get mutex to only allow one thread running at a time */
pthread_mutex_lock(&mp); SDL_mutexP(mp);
/* start a tick thread */ /* start a tick thread */
pthread_create(&tick_tid, NULL, (void *(*)(void *)) update_tick_thread, tick_tid=SDL_CreateThread(update_tick_thread, NULL);
NULL);
#ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */ #ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */
{ {
pthread_t sound_tid; SDL_Thread *sound_tid;
pthread_create(&sound_tid, NULL, sound_tid = SDL_CreateThread(sound_playback_thread, NULL);
(void *(*)(void *)) sound_playback_thread,
NULL);
} }
#endif #endif
} }
/*
int pthread_create(pthread_t *new_thread_ID,
const pthread_attr_t *attr,
void * (*start_func)(void *), void *arg);
*/
void yield(void) void yield(void)
{ {
pthread_mutex_unlock(&mp); /* return */ SDL_mutexV(mp); /* return */
msleep(1); /* prevent busy loop */ msleep(1); /* prevent busy loop */
pthread_mutex_lock(&mp); /* get it again */ SDL_mutexP(mp); /* get it again */
} }
void newfunc(void (*func)(void)) void newfunc(void (*func)(void))
{ {
pthread_mutex_lock(&mp); SDL_mutexP(mp);
func(); func();
pthread_mutex_unlock(&mp); SDL_mutexV(mp);
} }
int create_thread(void (*fp)(void), void* sp, int stk_size) int create_thread(void (*fp)(void), void* sp, int stk_size)
{ {
pthread_t tid; SDL_Thread * tid;
int i; int i;
int error; int error;
/* we really don't care about these arguments */ /* we really don't care about these arguments */
(void)sp; (void)sp;
(void)stk_size; (void)stk_size;
error = pthread_create(&tid, tid = SDL_CreateThread(
NULL, /* default attributes please */ (int(*)(void *))newfunc, /* function to start */
(void *(*)(void *)) newfunc, /* function to start */
fp /* start argument */); fp /* start argument */);
if(0 != error) if(0 == tid) /* don't really have an error number here. */
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); fprintf(stderr, "Couldn't run thread number %d\n", i);
else else
fprintf(stderr, "Thread %ld is running\n", (long)tid); fprintf(stderr, "Thread %d is running\n", (int)SDL_GetThreadID(tid));
yield(); yield();
@ -141,8 +137,8 @@ int create_thread(void (*fp)(void), void* sp, int stk_size)
void sim_sleep(int ticks) void sim_sleep(int ticks)
{ {
pthread_mutex_unlock(&mp); /* return */ SDL_mutexV(mp); /* return */
msleep((1000/HZ) * ticks); msleep((1000/HZ) * ticks);
pthread_mutex_lock(&mp); /* get it again */ SDL_mutexP(mp); /* get it again */
} }