Mercurial > sdl-ios-xcode
comparison src/thread/dc/SDL_syssem.c @ 509:dad72daf44b3
Added initial support for Dreamcast (thanks HERO!)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 05 Oct 2002 16:50:56 +0000 |
parents | |
children | b8d311d90021 |
comparison
equal
deleted
inserted
replaced
508:9ff7e90aaa94 | 509:dad72daf44b3 |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 BERO | |
20 bero@geocities.co.jp | |
21 | |
22 based on generic/SDL_syssem.c | |
23 | |
24 Sam Lantinga | |
25 slouken@libsdl.org | |
26 */ | |
27 | |
28 #ifdef SAVE_RCSID | |
29 static char rcsid = | |
30 "@(#) $Id$"; | |
31 #endif | |
32 | |
33 /* An implementation of semaphores using mutexes and condition variables */ | |
34 | |
35 #include <stdlib.h> | |
36 | |
37 #include "SDL_error.h" | |
38 #include "SDL_timer.h" | |
39 #include "SDL_thread.h" | |
40 #include "SDL_systhread_c.h" | |
41 | |
42 | |
43 #ifdef DISABLE_THREADS | |
44 | |
45 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
46 { | |
47 SDL_SetError("SDL not configured with thread support"); | |
48 return (SDL_sem *)0; | |
49 } | |
50 | |
51 void SDL_DestroySemaphore(SDL_sem *sem) | |
52 { | |
53 return; | |
54 } | |
55 | |
56 int SDL_SemTryWait(SDL_sem *sem) | |
57 { | |
58 SDL_SetError("SDL not configured with thread support"); | |
59 return -1; | |
60 } | |
61 | |
62 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
63 { | |
64 SDL_SetError("SDL not configured with thread support"); | |
65 return -1; | |
66 } | |
67 | |
68 int SDL_SemWait(SDL_sem *sem) | |
69 { | |
70 SDL_SetError("SDL not configured with thread support"); | |
71 return -1; | |
72 } | |
73 | |
74 Uint32 SDL_SemValue(SDL_sem *sem) | |
75 { | |
76 return 0; | |
77 } | |
78 | |
79 int SDL_SemPost(SDL_sem *sem) | |
80 { | |
81 SDL_SetError("SDL not configured with thread support"); | |
82 return -1; | |
83 } | |
84 | |
85 #else | |
86 | |
87 #include <kos/sem.h> | |
88 | |
89 struct SDL_semaphore | |
90 { | |
91 semaphore_t sem; | |
92 }; | |
93 | |
94 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
95 { | |
96 return (SDL_sem *)sem_create(initial_value); | |
97 } | |
98 | |
99 /* WARNING: | |
100 You cannot call this function when another thread is using the semaphore. | |
101 */ | |
102 void SDL_DestroySemaphore(SDL_sem *sem) | |
103 { | |
104 if ( ! sem ) { | |
105 SDL_SetError("Passed a NULL semaphore"); | |
106 return; | |
107 } | |
108 | |
109 sem_destroy(&sem->sem); | |
110 } | |
111 | |
112 int SDL_SemTryWait(SDL_sem *sem) | |
113 { | |
114 int retval; | |
115 | |
116 if ( ! sem ) { | |
117 SDL_SetError("Passed a NULL semaphore"); | |
118 return -1; | |
119 } | |
120 | |
121 retval = sem_trywait(&sem->sem); | |
122 if (retval==0) return 0; | |
123 else return SDL_MUTEX_TIMEDOUT; | |
124 | |
125 return retval; | |
126 } | |
127 | |
128 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
129 { | |
130 int retval; | |
131 | |
132 if ( ! sem ) { | |
133 SDL_SetError("Passed a NULL semaphore"); | |
134 return -1; | |
135 } | |
136 | |
137 /* A timeout of 0 is an easy case */ | |
138 if ( timeout == 0 ) { | |
139 return SDL_SemTryWait(sem); | |
140 } | |
141 | |
142 retval = sem_wait_timed(&sem->sem,timeout); | |
143 if (retval==-1) retval= SDL_MUTEX_TIMEDOUT; | |
144 | |
145 return retval; | |
146 } | |
147 | |
148 int SDL_SemWait(SDL_sem *sem) | |
149 { | |
150 if ( ! sem ) { | |
151 SDL_SetError("Passed a NULL semaphore"); | |
152 return -1; | |
153 } | |
154 | |
155 sem_wait(&sem->sem); | |
156 return 0; | |
157 } | |
158 | |
159 Uint32 SDL_SemValue(SDL_sem *sem) | |
160 { | |
161 if ( ! sem ) { | |
162 SDL_SetError("Passed a NULL semaphore"); | |
163 return -1; | |
164 } | |
165 | |
166 return sem_count(&sem->sem); | |
167 } | |
168 | |
169 int SDL_SemPost(SDL_sem *sem) | |
170 { | |
171 if ( ! sem ) { | |
172 SDL_SetError("Passed a NULL semaphore"); | |
173 return -1; | |
174 } | |
175 | |
176 sem_signal(&sem->sem); | |
177 return 0; | |
178 } | |
179 | |
180 #endif /* DISABLE_THREADS */ |