Mercurial > sdl-ios-xcode
comparison src/thread/os2/SDL_syssem.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | d910939febfa |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
30 | 30 |
31 #include "SDL_thread.h" | 31 #include "SDL_thread.h" |
32 #include "SDL_timer.h" | 32 #include "SDL_timer.h" |
33 | 33 |
34 | 34 |
35 struct SDL_semaphore { | 35 struct SDL_semaphore |
36 HMTX id; | 36 { |
37 HEV changed; | 37 HMTX id; |
38 Uint32 value; | 38 HEV changed; |
39 Uint32 value; | |
39 }; | 40 }; |
40 | 41 |
41 | 42 |
42 /* Create a semaphore */ | 43 /* Create a semaphore */ |
43 DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value) | 44 DECLSPEC SDL_sem *SDLCALL |
45 SDL_CreateSemaphore(Uint32 initial_value) | |
44 { | 46 { |
45 SDL_sem *sem; | 47 SDL_sem *sem; |
46 ULONG ulrc; | 48 ULONG ulrc; |
47 | 49 |
48 /* Allocate sem memory */ | 50 /* Allocate sem memory */ |
49 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); | 51 sem = (SDL_sem *) SDL_malloc(sizeof(*sem)); |
50 if ( sem ) { | 52 if (sem) { |
51 /* Create the mutex semaphore */ | 53 /* Create the mutex semaphore */ |
52 ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE); | 54 ulrc = DosCreateMutexSem(NULL, &(sem->id), 0, TRUE); |
53 if ( ulrc ) { | 55 if (ulrc) { |
54 SDL_SetError("Couldn't create semaphore"); | 56 SDL_SetError("Couldn't create semaphore"); |
55 SDL_free(sem); | 57 SDL_free(sem); |
56 sem = NULL; | 58 sem = NULL; |
57 } else | |
58 { | |
59 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE); | |
60 sem->value = initial_value; | |
61 DosReleaseMutexSem(sem->id); | |
62 } | |
63 } else { | 59 } else { |
64 SDL_OutOfMemory(); | 60 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE); |
61 sem->value = initial_value; | |
62 DosReleaseMutexSem(sem->id); | |
65 } | 63 } |
66 return(sem); | 64 } else { |
65 SDL_OutOfMemory(); | |
66 } | |
67 return (sem); | |
67 } | 68 } |
68 | 69 |
69 /* Free the semaphore */ | 70 /* Free the semaphore */ |
70 DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem) | 71 DECLSPEC void SDLCALL |
72 SDL_DestroySemaphore(SDL_sem * sem) | |
71 { | 73 { |
72 if ( sem ) { | 74 if (sem) { |
73 if ( sem->id ) { | 75 if (sem->id) { |
74 DosCloseEventSem(sem->changed); | 76 DosCloseEventSem(sem->changed); |
75 DosCloseMutexSem(sem->id); | 77 DosCloseMutexSem(sem->id); |
76 sem->id = 0; | 78 sem->id = 0; |
77 } | |
78 SDL_free(sem); | |
79 } | 79 } |
80 SDL_free(sem); | |
81 } | |
80 } | 82 } |
81 | 83 |
82 DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | 84 DECLSPEC int SDLCALL |
85 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) | |
83 { | 86 { |
84 ULONG ulrc; | 87 ULONG ulrc; |
85 | 88 |
86 if ( ! sem ) { | 89 if (!sem) { |
87 SDL_SetError("Passed a NULL sem"); | 90 SDL_SetError("Passed a NULL sem"); |
88 return -1; | 91 return -1; |
89 } | 92 } |
90 | 93 |
91 if ( timeout == SDL_MUTEX_MAXWAIT ) { | 94 if (timeout == SDL_MUTEX_MAXWAIT) { |
92 while (1) { | 95 while (1) { |
93 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
94 if (ulrc) { | |
95 /* if error waiting mutex */ | |
96 SDL_SetError("DosRequestMutexSem() failed"); | |
97 return -1; | |
98 } else if (sem->value) { | |
99 sem->value--; | |
100 DosReleaseMutexSem(sem->id); | |
101 return 0; | |
102 } else { | |
103 ULONG ulPostCount; | |
104 DosResetEventSem(sem->changed, &ulPostCount); | |
105 DosReleaseMutexSem(sem->id); | |
106 /* continue waiting until somebody posts the semaphore */ | |
107 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT); | |
108 } | |
109 } | |
110 } else | |
111 if ( timeout == 0 ) | |
112 { | |
113 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | 96 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); |
114 if (ulrc==NO_ERROR) | 97 if (ulrc) { |
115 { | 98 /* if error waiting mutex */ |
116 if (sem->value) | |
117 { | |
118 sem->value--; | |
119 DosReleaseMutexSem(sem->id); | |
120 return 0; | |
121 } else | |
122 { | |
123 DosReleaseMutexSem(sem->id); | |
124 return SDL_MUTEX_TIMEDOUT; | |
125 } | |
126 } else | |
127 { | |
128 SDL_SetError("DosRequestMutexSem() failed"); | 99 SDL_SetError("DosRequestMutexSem() failed"); |
129 return -1; | 100 return -1; |
130 } | 101 } else if (sem->value) { |
131 } else { | |
132 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
133 if (ulrc) { | |
134 /* if error waiting mutex */ | |
135 SDL_SetError("DosRequestMutexSem() failed"); | |
136 return -1; | |
137 } else | |
138 if (sem->value) { | |
139 sem->value--; | 102 sem->value--; |
140 DosReleaseMutexSem(sem->id); | 103 DosReleaseMutexSem(sem->id); |
141 return 0; | 104 return 0; |
142 } else { | 105 } else { |
143 ULONG ulPostCount; | 106 ULONG ulPostCount; |
144 DosResetEventSem(sem->changed, &ulPostCount); | 107 DosResetEventSem(sem->changed, &ulPostCount); |
145 DosReleaseMutexSem(sem->id); | 108 DosReleaseMutexSem(sem->id); |
146 /* continue waiting until somebody posts the semaphore */ | 109 /* continue waiting until somebody posts the semaphore */ |
147 ulrc = DosWaitEventSem(sem->changed, timeout); | 110 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT); |
148 if (ulrc==NO_ERROR) | 111 } |
149 return 0; | |
150 else | |
151 return SDL_MUTEX_TIMEDOUT; | |
152 } | |
153 } | 112 } |
154 /* never reached */ | 113 } else if (timeout == 0) { |
155 return -1; | 114 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); |
115 if (ulrc == NO_ERROR) { | |
116 if (sem->value) { | |
117 sem->value--; | |
118 DosReleaseMutexSem(sem->id); | |
119 return 0; | |
120 } else { | |
121 DosReleaseMutexSem(sem->id); | |
122 return SDL_MUTEX_TIMEDOUT; | |
123 } | |
124 } else { | |
125 SDL_SetError("DosRequestMutexSem() failed"); | |
126 return -1; | |
127 } | |
128 } else { | |
129 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT); | |
130 if (ulrc) { | |
131 /* if error waiting mutex */ | |
132 SDL_SetError("DosRequestMutexSem() failed"); | |
133 return -1; | |
134 } else if (sem->value) { | |
135 sem->value--; | |
136 DosReleaseMutexSem(sem->id); | |
137 return 0; | |
138 } else { | |
139 ULONG ulPostCount; | |
140 DosResetEventSem(sem->changed, &ulPostCount); | |
141 DosReleaseMutexSem(sem->id); | |
142 /* continue waiting until somebody posts the semaphore */ | |
143 ulrc = DosWaitEventSem(sem->changed, timeout); | |
144 if (ulrc == NO_ERROR) | |
145 return 0; | |
146 else | |
147 return SDL_MUTEX_TIMEDOUT; | |
148 } | |
149 } | |
150 /* never reached */ | |
151 return -1; | |
156 } | 152 } |
157 | 153 |
158 DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem) | 154 DECLSPEC int SDLCALL |
155 SDL_SemTryWait(SDL_sem * sem) | |
159 { | 156 { |
160 return SDL_SemWaitTimeout(sem, 0); | 157 return SDL_SemWaitTimeout(sem, 0); |
161 } | 158 } |
162 | 159 |
163 DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem) | 160 DECLSPEC int SDLCALL |
161 SDL_SemWait(SDL_sem * sem) | |
164 { | 162 { |
165 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | 163 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); |
166 } | 164 } |
167 | 165 |
168 /* Returns the current count of the semaphore */ | 166 /* Returns the current count of the semaphore */ |
169 DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem) | 167 DECLSPEC Uint32 SDLCALL |
168 SDL_SemValue(SDL_sem * sem) | |
170 { | 169 { |
171 if ( ! sem ) { | 170 if (!sem) { |
172 SDL_SetError("Passed a NULL sem"); | 171 SDL_SetError("Passed a NULL sem"); |
173 return 0; | 172 return 0; |
174 } | 173 } |
175 return sem->value; | 174 return sem->value; |
176 } | 175 } |
177 | 176 |
178 DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem) | 177 DECLSPEC int SDLCALL |
178 SDL_SemPost(SDL_sem * sem) | |
179 { | 179 { |
180 if ( ! sem ) { | 180 if (!sem) { |
181 SDL_SetError("Passed a NULL sem"); | 181 SDL_SetError("Passed a NULL sem"); |
182 return -1; | 182 return -1; |
183 } | 183 } |
184 if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) { | 184 if (DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT)) { |
185 SDL_SetError("DosRequestMutexSem() failed"); | 185 SDL_SetError("DosRequestMutexSem() failed"); |
186 return -1; | 186 return -1; |
187 } | 187 } |
188 sem->value++; | 188 sem->value++; |
189 DosPostEventSem(sem->changed); | 189 DosPostEventSem(sem->changed); |
190 DosReleaseMutexSem(sem->id); | 190 DosReleaseMutexSem(sem->id); |
191 return 0; | 191 return 0; |
192 } | 192 } |
193 | |
194 /* vi: set ts=4 sw=4 expandtab: */ |