Mercurial > sdl-ios-xcode
comparison src/thread/win32/SDL_syssem.c @ 36:13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Wed, 23 May 2001 23:35:10 +0000 |
parents | 74212992fb08 |
children | e8157fcb3114 |
comparison
equal
deleted
inserted
replaced
35:d3bc792e136d | 36:13ee9f4834ea |
---|---|
31 #include <stdlib.h> | 31 #include <stdlib.h> |
32 #include <windows.h> | 32 #include <windows.h> |
33 | 33 |
34 #include "SDL_error.h" | 34 #include "SDL_error.h" |
35 #include "SDL_thread.h" | 35 #include "SDL_thread.h" |
36 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
37 #include "win_ce_semaphore.h" | |
38 #endif | |
36 | 39 |
37 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
38 | |
39 /* No semaphores on Windows CE earlier than 3.0, hmm... */ | |
40 | |
41 /* Create a semaphore */ | |
42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
43 { | |
44 SDL_SetError("Semaphores not supported on WinCE"); | |
45 return(NULL); | |
46 } | |
47 | |
48 /* Free the semaphore */ | |
49 void SDL_DestroySemaphore(SDL_sem *sem) | |
50 { | |
51 return; | |
52 } | |
53 | |
54 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
55 { | |
56 SDL_SetError("Semaphores not supported on WinCE"); | |
57 return(-1); | |
58 } | |
59 | |
60 int SDL_SemTryWait(SDL_sem *sem) | |
61 { | |
62 return SDL_SemWaitTimeout(sem, 0); | |
63 } | |
64 | |
65 int SDL_SemWait(SDL_sem *sem) | |
66 { | |
67 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
68 } | |
69 | |
70 /* Returns the current count of the semaphore */ | |
71 Uint32 SDL_SemValue(SDL_sem *sem) | |
72 { | |
73 return(0); | |
74 } | |
75 | |
76 int SDL_SemPost(SDL_sem *sem) | |
77 { | |
78 SDL_SetError("Semaphores not supported on WinCE"); | |
79 return(-1); | |
80 } | |
81 | |
82 #else | |
83 | 40 |
84 struct SDL_semaphore { | 41 struct SDL_semaphore { |
42 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
43 SYNCHHANDLE id; | |
44 #else | |
85 HANDLE id; | 45 HANDLE id; |
46 #endif | |
86 Uint32 volatile count; | 47 Uint32 volatile count; |
87 }; | 48 }; |
88 | 49 |
89 | 50 |
90 /* Create a semaphore */ | 51 /* Create a semaphore */ |
94 | 55 |
95 /* Allocate sem memory */ | 56 /* Allocate sem memory */ |
96 sem = (SDL_sem *)malloc(sizeof(*sem)); | 57 sem = (SDL_sem *)malloc(sizeof(*sem)); |
97 if ( sem ) { | 58 if ( sem ) { |
98 /* Create the semaphore, with max value 32K */ | 59 /* Create the semaphore, with max value 32K */ |
60 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
61 sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL); | |
62 #else | |
99 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL); | 63 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL); |
64 #endif | |
100 sem->count = initial_value; | 65 sem->count = initial_value; |
101 if ( ! sem->id ) { | 66 if ( ! sem->id ) { |
102 SDL_SetError("Couldn't create semaphore"); | 67 SDL_SetError("Couldn't create semaphore"); |
103 free(sem); | 68 free(sem); |
104 sem = NULL; | 69 sem = NULL; |
112 /* Free the semaphore */ | 77 /* Free the semaphore */ |
113 void SDL_DestroySemaphore(SDL_sem *sem) | 78 void SDL_DestroySemaphore(SDL_sem *sem) |
114 { | 79 { |
115 if ( sem ) { | 80 if ( sem ) { |
116 if ( sem->id ) { | 81 if ( sem->id ) { |
82 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
83 CloseSynchHandle(sem->id); | |
84 #else | |
117 CloseHandle(sem->id); | 85 CloseHandle(sem->id); |
86 #endif | |
118 sem->id = 0; | 87 sem->id = 0; |
119 } | 88 } |
120 free(sem); | 89 free(sem); |
121 } | 90 } |
122 } | 91 } |
134 if ( timeout == SDL_MUTEX_MAXWAIT ) { | 103 if ( timeout == SDL_MUTEX_MAXWAIT ) { |
135 dwMilliseconds = INFINITE; | 104 dwMilliseconds = INFINITE; |
136 } else { | 105 } else { |
137 dwMilliseconds = (DWORD)timeout; | 106 dwMilliseconds = (DWORD)timeout; |
138 } | 107 } |
108 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
109 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) { | |
110 #else | |
139 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { | 111 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { |
112 #endif | |
140 case WAIT_OBJECT_0: | 113 case WAIT_OBJECT_0: |
141 --sem->count; | 114 --sem->count; |
142 retval = 0; | 115 retval = 0; |
143 break; | 116 break; |
144 case WAIT_TIMEOUT: | 117 case WAIT_TIMEOUT: |
182 * after a successful release the semaphore may | 155 * after a successful release the semaphore may |
183 * immediately get destroyed by another thread which | 156 * immediately get destroyed by another thread which |
184 * is waiting for this semaphore. | 157 * is waiting for this semaphore. |
185 */ | 158 */ |
186 ++sem->count; | 159 ++sem->count; |
160 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
161 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) { | |
162 #else | |
187 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) { | 163 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) { |
164 #endif | |
188 --sem->count; /* restore */ | 165 --sem->count; /* restore */ |
189 SDL_SetError("ReleaseSemaphore() failed"); | 166 SDL_SetError("ReleaseSemaphore() failed"); |
190 return -1; | 167 return -1; |
191 } | 168 } |
192 return 0; | 169 return 0; |
193 } | 170 } |
194 | |
195 #endif /* _WIN32_WCE */ |