Mercurial > sdl-ios-xcode
annotate src/thread/os2/SDL_syssem.c @ 1338:604d73db6802
Removed uses of stdlib.h and string.h
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 09:29:18 +0000 |
parents | 3692456e7b0f |
children | c71e05b4dc2e |
rev | line source |
---|---|
1190 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
1190 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
1190 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
1190 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
1190 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1190 | 18 |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 | |
23 /* Semaphore functions using the OS/2 API */ | |
24 | |
25 #define INCL_DOS | |
26 #define INCL_DOSERRORS | |
27 #define INCL_DOSSEMAPHORES | |
28 #include <os2.h> | |
29 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
30 #include "SDL_stdlib.h" |
1190 | 31 #include "SDL_error.h" |
32 #include "SDL_thread.h" | |
33 #include "SDL_timer.h" | |
34 | |
35 | |
36 struct SDL_semaphore { | |
37 HMTX id; | |
38 HEV changed; | |
39 Uint32 value; | |
40 }; | |
41 | |
42 | |
43 /* Create a semaphore */ | |
44 DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value) | |
45 { | |
46 SDL_sem *sem; | |
47 ULONG ulrc; | |
48 | |
49 /* Allocate sem memory */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
50 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); |
1190 | 51 if ( sem ) { |
52 /* Create the mutex semaphore */ | |
53 ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE); | |
54 if ( ulrc ) { | |
55 SDL_SetError("Couldn't create semaphore"); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
56 SDL_free(sem); |
1190 | 57 sem = NULL; |
58 } else | |
59 { | |
60 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE); | |
61 sem->value = initial_value; | |
62 DosReleaseMutexSem(sem->id); | |
63 } | |
64 } else { | |
65 SDL_OutOfMemory(); | |
66 } | |
67 return(sem); | |
68 } | |
69 | |
70 /* Free the semaphore */ | |
71 DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem) | |
72 { | |
73 if ( sem ) { | |
74 if ( sem->id ) { | |
75 DosCloseEventSem(sem->changed); | |
76 DosCloseMutexSem(sem->id); | |
77 sem->id = 0; | |
78 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
79 SDL_free(sem); |
1190 | 80 } |
81 } | |
82 | |
83 DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
84 { | |
85 ULONG ulrc; | |
86 | |
87 if ( ! sem ) { | |
88 SDL_SetError("Passed a NULL sem"); | |
89 return -1; | |
90 } | |
91 | |
92 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
93 while (1) { | |
94 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
95 if (ulrc) { | |
96 /* if error waiting mutex */ | |
97 SDL_SetError("DosRequestMutexSem() failed"); | |
98 return -1; | |
99 } else if (sem->value) { | |
100 sem->value--; | |
101 DosReleaseMutexSem(sem->id); | |
102 return 0; | |
103 } else { | |
104 ULONG ulPostCount; | |
105 DosResetEventSem(sem->changed, &ulPostCount); | |
106 DosReleaseMutexSem(sem->id); | |
107 /* continue waiting until somebody posts the semaphore */ | |
108 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT); | |
109 } | |
110 } | |
111 } else | |
112 if ( timeout == 0 ) | |
113 { | |
114 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
115 if (ulrc==NO_ERROR) | |
116 { | |
117 if (sem->value) | |
118 { | |
119 sem->value--; | |
120 DosReleaseMutexSem(sem->id); | |
121 return 0; | |
122 } else | |
123 { | |
124 DosReleaseMutexSem(sem->id); | |
125 return SDL_MUTEX_TIMEDOUT; | |
126 } | |
127 } else | |
128 { | |
129 SDL_SetError("DosRequestMutexSem() failed"); | |
130 return -1; | |
131 } | |
132 } else { | |
133 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
134 if (ulrc) { | |
135 /* if error waiting mutex */ | |
136 SDL_SetError("DosRequestMutexSem() failed"); | |
137 return -1; | |
138 } else | |
139 if (sem->value) { | |
140 sem->value--; | |
141 DosReleaseMutexSem(sem->id); | |
142 return 0; | |
143 } else { | |
144 ULONG ulPostCount; | |
145 DosResetEventSem(sem->changed, &ulPostCount); | |
146 DosReleaseMutexSem(sem->id); | |
147 /* continue waiting until somebody posts the semaphore */ | |
148 ulrc = DosWaitEventSem(sem->changed, timeout); | |
149 if (ulrc==NO_ERROR) | |
150 return 0; | |
151 else | |
152 return SDL_MUTEX_TIMEDOUT; | |
153 } | |
154 } | |
155 /* never reached */ | |
156 return -1; | |
157 } | |
158 | |
159 DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem) | |
160 { | |
161 return SDL_SemWaitTimeout(sem, 0); | |
162 } | |
163 | |
164 DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem) | |
165 { | |
166 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
167 } | |
168 | |
169 /* Returns the current count of the semaphore */ | |
170 DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem) | |
171 { | |
172 if ( ! sem ) { | |
173 SDL_SetError("Passed a NULL sem"); | |
174 return 0; | |
175 } | |
176 return sem->value; | |
177 } | |
178 | |
179 DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem) | |
180 { | |
181 if ( ! sem ) { | |
182 SDL_SetError("Passed a NULL sem"); | |
183 return -1; | |
184 } | |
185 if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) { | |
186 SDL_SetError("DosRequestMutexSem() failed"); | |
187 return -1; | |
188 } | |
189 sem->value++; | |
190 DosPostEventSem(sem->changed); | |
191 DosReleaseMutexSem(sem->id); | |
192 return 0; | |
193 } |