diff options
Diffstat (limited to 'uisimulator/sdl/thread.c')
-rw-r--r-- | uisimulator/sdl/thread.c | 62 |
1 files changed, 29 insertions, 33 deletions
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 | */ |
48 | static void update_tick_thread() | 51 | static 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 | ||
74 | pthread_mutex_t mp; | 79 | SDL_mutex * mp; |
75 | 80 | ||
76 | void init_threads(void) | 81 | void 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 | ||
104 | void yield(void) | 101 | void 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 | ||
111 | void newfunc(void (*func)(void)) | 108 | void 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 | ||
119 | int create_thread(void (*fp)(void), void* sp, int stk_size) | 116 | int 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 | ||
142 | void sim_sleep(int ticks) | 138 | void 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 | ||