Mercurial > sdl-ios-xcode
annotate src/thread/os2/SDL_syssem.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | 173c063d4f55 |
children | 3692456e7b0f |
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 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #define INCL_DOS | |
28 #define INCL_DOSERRORS | |
29 #define INCL_DOSSEMAPHORES | |
30 #include <os2.h> | |
31 | |
32 #include "SDL_error.h" | |
33 #include "SDL_thread.h" | |
34 #include "SDL_timer.h" | |
35 | |
36 | |
37 struct SDL_semaphore { | |
38 HMTX id; | |
39 HEV changed; | |
40 Uint32 value; | |
41 }; | |
42 | |
43 | |
44 /* Create a semaphore */ | |
45 DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value) | |
46 { | |
47 SDL_sem *sem; | |
48 ULONG ulrc; | |
49 | |
50 /* Allocate sem memory */ | |
51 sem = (SDL_sem *)malloc(sizeof(*sem)); | |
52 if ( sem ) { | |
53 /* Create the mutex semaphore */ | |
54 ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE); | |
55 if ( ulrc ) { | |
56 SDL_SetError("Couldn't create semaphore"); | |
57 free(sem); | |
58 sem = NULL; | |
59 } else | |
60 { | |
61 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE); | |
62 sem->value = initial_value; | |
63 DosReleaseMutexSem(sem->id); | |
64 } | |
65 } else { | |
66 SDL_OutOfMemory(); | |
67 } | |
68 return(sem); | |
69 } | |
70 | |
71 /* Free the semaphore */ | |
72 DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem) | |
73 { | |
74 if ( sem ) { | |
75 if ( sem->id ) { | |
76 DosCloseEventSem(sem->changed); | |
77 DosCloseMutexSem(sem->id); | |
78 sem->id = 0; | |
79 } | |
80 free(sem); | |
81 } | |
82 } | |
83 | |
84 DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
85 { | |
86 ULONG ulrc; | |
87 | |
88 if ( ! sem ) { | |
89 SDL_SetError("Passed a NULL sem"); | |
90 return -1; | |
91 } | |
92 | |
93 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
94 while (1) { | |
95 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
96 if (ulrc) { | |
97 /* if error waiting mutex */ | |
98 SDL_SetError("DosRequestMutexSem() failed"); | |
99 return -1; | |
100 } else if (sem->value) { | |
101 sem->value--; | |
102 DosReleaseMutexSem(sem->id); | |
103 return 0; | |
104 } else { | |
105 ULONG ulPostCount; | |
106 DosResetEventSem(sem->changed, &ulPostCount); | |
107 DosReleaseMutexSem(sem->id); | |
108 /* continue waiting until somebody posts the semaphore */ | |
109 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT); | |
110 } | |
111 } | |
112 } else | |
113 if ( timeout == 0 ) | |
114 { | |
115 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
116 if (ulrc==NO_ERROR) | |
117 { | |
118 if (sem->value) | |
119 { | |
120 sem->value--; | |
121 DosReleaseMutexSem(sem->id); | |
122 return 0; | |
123 } else | |
124 { | |
125 DosReleaseMutexSem(sem->id); | |
126 return SDL_MUTEX_TIMEDOUT; | |
127 } | |
128 } else | |
129 { | |
130 SDL_SetError("DosRequestMutexSem() failed"); | |
131 return -1; | |
132 } | |
133 } else { | |
134 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
135 if (ulrc) { | |
136 /* if error waiting mutex */ | |
137 SDL_SetError("DosRequestMutexSem() failed"); | |
138 return -1; | |
139 } else | |
140 if (sem->value) { | |
141 sem->value--; | |
142 DosReleaseMutexSem(sem->id); | |
143 return 0; | |
144 } else { | |
145 ULONG ulPostCount; | |
146 DosResetEventSem(sem->changed, &ulPostCount); | |
147 DosReleaseMutexSem(sem->id); | |
148 /* continue waiting until somebody posts the semaphore */ | |
149 ulrc = DosWaitEventSem(sem->changed, timeout); | |
150 if (ulrc==NO_ERROR) | |
151 return 0; | |
152 else | |
153 return SDL_MUTEX_TIMEDOUT; | |
154 } | |
155 } | |
156 /* never reached */ | |
157 return -1; | |
158 } | |
159 | |
160 DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem) | |
161 { | |
162 return SDL_SemWaitTimeout(sem, 0); | |
163 } | |
164 | |
165 DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem) | |
166 { | |
167 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
168 } | |
169 | |
170 /* Returns the current count of the semaphore */ | |
171 DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem) | |
172 { | |
173 if ( ! sem ) { | |
174 SDL_SetError("Passed a NULL sem"); | |
175 return 0; | |
176 } | |
177 return sem->value; | |
178 } | |
179 | |
180 DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem) | |
181 { | |
182 if ( ! sem ) { | |
183 SDL_SetError("Passed a NULL sem"); | |
184 return -1; | |
185 } | |
186 if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) { | |
187 SDL_SetError("DosRequestMutexSem() failed"); | |
188 return -1; | |
189 } | |
190 sem->value++; | |
191 DosPostEventSem(sem->changed); | |
192 DosReleaseMutexSem(sem->id); | |
193 return 0; | |
194 } |