comparison src/thread/win32/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 bb6839704ed6
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
30 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 30 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
31 #include "win_ce_semaphore.h" 31 #include "win_ce_semaphore.h"
32 #endif 32 #endif
33 33
34 34
35 struct SDL_semaphore { 35 struct SDL_semaphore
36 {
36 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 37 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
37 SYNCHHANDLE id; 38 SYNCHHANDLE id;
38 #else 39 #else
39 HANDLE id; 40 HANDLE id;
40 #endif 41 #endif
41 Uint32 volatile count; 42 Uint32 volatile count;
42 }; 43 };
43 44
44 45
45 /* Create a semaphore */ 46 /* Create a semaphore */
46 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 47 SDL_sem *
48 SDL_CreateSemaphore(Uint32 initial_value)
47 { 49 {
48 SDL_sem *sem; 50 SDL_sem *sem;
49 51
50 /* Allocate sem memory */ 52 /* Allocate sem memory */
51 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); 53 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
52 if ( sem ) { 54 if (sem) {
53 /* Create the semaphore, with max value 32K */ 55 /* Create the semaphore, with max value 32K */
54 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 56 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
55 sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL); 57 sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
56 #else 58 #else
57 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL); 59 sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
58 #endif 60 #endif
59 sem->count = initial_value; 61 sem->count = initial_value;
60 if ( ! sem->id ) { 62 if (!sem->id) {
61 SDL_SetError("Couldn't create semaphore"); 63 SDL_SetError("Couldn't create semaphore");
62 SDL_free(sem); 64 SDL_free(sem);
63 sem = NULL; 65 sem = NULL;
64 } 66 }
65 } else { 67 } else {
66 SDL_OutOfMemory(); 68 SDL_OutOfMemory();
67 } 69 }
68 return(sem); 70 return (sem);
69 } 71 }
70 72
71 /* Free the semaphore */ 73 /* Free the semaphore */
72 void SDL_DestroySemaphore(SDL_sem *sem) 74 void
75 SDL_DestroySemaphore(SDL_sem * sem)
73 { 76 {
74 if ( sem ) { 77 if (sem) {
75 if ( sem->id ) { 78 if (sem->id) {
76 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 79 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
77 CloseSynchHandle(sem->id); 80 CloseSynchHandle(sem->id);
78 #else 81 #else
79 CloseHandle(sem->id); 82 CloseHandle(sem->id);
80 #endif 83 #endif
81 sem->id = 0; 84 sem->id = 0;
82 } 85 }
83 SDL_free(sem); 86 SDL_free(sem);
84 } 87 }
85 } 88 }
86 89
87 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 90 int
91 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
88 { 92 {
89 int retval; 93 int retval;
90 DWORD dwMilliseconds; 94 DWORD dwMilliseconds;
91 95
92 if ( ! sem ) { 96 if (!sem) {
93 SDL_SetError("Passed a NULL sem"); 97 SDL_SetError("Passed a NULL sem");
94 return -1; 98 return -1;
95 } 99 }
96 100
97 if ( timeout == SDL_MUTEX_MAXWAIT ) { 101 if (timeout == SDL_MUTEX_MAXWAIT) {
98 dwMilliseconds = INFINITE; 102 dwMilliseconds = INFINITE;
99 } else { 103 } else {
100 dwMilliseconds = (DWORD)timeout; 104 dwMilliseconds = (DWORD) timeout;
101 } 105 }
102 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 106 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
103 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) { 107 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
104 #else 108 #else
105 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { 109 switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
106 #endif 110 #endif
107 case WAIT_OBJECT_0: 111 case WAIT_OBJECT_0:
108 --sem->count; 112 --sem->count;
109 retval = 0; 113 retval = 0;
110 break; 114 break;
111 case WAIT_TIMEOUT: 115 case WAIT_TIMEOUT:
112 retval = SDL_MUTEX_TIMEDOUT; 116 retval = SDL_MUTEX_TIMEDOUT;
113 break; 117 break;
114 default: 118 default:
115 SDL_SetError("WaitForSingleObject() failed"); 119 SDL_SetError("WaitForSingleObject() failed");
116 retval = -1; 120 retval = -1;
117 break; 121 break;
118 } 122 }
119 return retval; 123 return retval;
120 } 124 }
121 125
122 int SDL_SemTryWait(SDL_sem *sem) 126 int
127 SDL_SemTryWait(SDL_sem * sem)
123 { 128 {
124 return SDL_SemWaitTimeout(sem, 0); 129 return SDL_SemWaitTimeout(sem, 0);
125 } 130 }
126 131
127 int SDL_SemWait(SDL_sem *sem) 132 int
133 SDL_SemWait(SDL_sem * sem)
128 { 134 {
129 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); 135 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
130 } 136 }
131 137
132 /* Returns the current count of the semaphore */ 138 /* Returns the current count of the semaphore */
133 Uint32 SDL_SemValue(SDL_sem *sem) 139 Uint32
140 SDL_SemValue(SDL_sem * sem)
134 { 141 {
135 if ( ! sem ) { 142 if (!sem) {
136 SDL_SetError("Passed a NULL sem"); 143 SDL_SetError("Passed a NULL sem");
137 return 0; 144 return 0;
138 } 145 }
139 return sem->count; 146 return sem->count;
140 } 147 }
141 148
142 int SDL_SemPost(SDL_sem *sem) 149 int
150 SDL_SemPost(SDL_sem * sem)
143 { 151 {
144 if ( ! sem ) { 152 if (!sem) {
145 SDL_SetError("Passed a NULL sem"); 153 SDL_SetError("Passed a NULL sem");
146 return -1; 154 return -1;
147 } 155 }
148 /* Increase the counter in the first place, because 156 /* Increase the counter in the first place, because
149 * after a successful release the semaphore may 157 * after a successful release the semaphore may
150 * immediately get destroyed by another thread which 158 * immediately get destroyed by another thread which
151 * is waiting for this semaphore. 159 * is waiting for this semaphore.
152 */ 160 */
153 ++sem->count; 161 ++sem->count;
154 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 162 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
155 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) { 163 if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
156 #else 164 #else
157 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) { 165 if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
158 #endif 166 #endif
159 --sem->count; /* restore */ 167 --sem->count; /* restore */
160 SDL_SetError("ReleaseSemaphore() failed"); 168 SDL_SetError("ReleaseSemaphore() failed");
161 return -1; 169 return -1;
162 } 170 }
163 return 0; 171 return 0;
164 } 172 }
173
174 /* vi: set ts=4 sw=4 expandtab: */