diff options
Diffstat (limited to 'uisimulator/sdl/thread-sdl.c')
-rw-r--r-- | uisimulator/sdl/thread-sdl.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/uisimulator/sdl/thread-sdl.c b/uisimulator/sdl/thread-sdl.c new file mode 100644 index 0000000000..41a60c9bc5 --- /dev/null +++ b/uisimulator/sdl/thread-sdl.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006 Dan Everton | ||
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 <time.h> | ||
21 | #include <SDL.h> | ||
22 | #include <SDL_thread.h> | ||
23 | #include <stdlib.h> | ||
24 | #include "thread-sdl.h" | ||
25 | #include "kernel.h" | ||
26 | #include "debug.h" | ||
27 | |||
28 | SDL_Thread *threads[256]; | ||
29 | int threadCount = 0; | ||
30 | long current_tick = 0; | ||
31 | SDL_mutex *m; | ||
32 | |||
33 | void yield(void) | ||
34 | { | ||
35 | SDL_mutexV(m); | ||
36 | SDL_Delay(1); | ||
37 | SDL_mutexP(m); | ||
38 | } | ||
39 | |||
40 | void sim_sleep(int ticks) | ||
41 | { | ||
42 | SDL_mutexV(m); | ||
43 | SDL_Delay((1000/HZ) * ticks); | ||
44 | SDL_mutexP(m); | ||
45 | } | ||
46 | |||
47 | int runthread(void *data) | ||
48 | { | ||
49 | SDL_mutexV(m); | ||
50 | ((void(*)())data) (); | ||
51 | SDL_mutexV(m); | ||
52 | return 0; | ||
53 | } | ||
54 | |||
55 | int create_thread(void (*fp)(void), void* sp, int stk_size) | ||
56 | { | ||
57 | /** Avoid compiler warnings */ | ||
58 | (void)sp; | ||
59 | (void)stk_size; | ||
60 | |||
61 | if (threadCount == 256) { | ||
62 | return -1; | ||
63 | } | ||
64 | |||
65 | threads[threadCount++] = SDL_CreateThread(runthread, fp); | ||
66 | |||
67 | return 0; | ||
68 | } | ||
69 | |||
70 | void init_threads(void) | ||
71 | { | ||
72 | m = SDL_CreateMutex(); | ||
73 | |||
74 | if (SDL_mutexP(m) == -1) { | ||
75 | fprintf(stderr, "Couldn't lock mutex\n"); | ||
76 | exit(-1); | ||
77 | } | ||
78 | } | ||