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
Karl Kurbjun
Tomasz Malesinski
Andrew Pilley

View file

@ -88,12 +88,14 @@ int sim_sound_init(void)
//SDL_CloseAudio();
}
void sound_playback_thread(void)
int sound_playback_thread(void* p)
{
int sndret = sim_sound_init();
unsigned char *buf;
long size;
(void)p;
while(sndret)
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);

View file

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