comparison src/thread/os2/SDL_syssem.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
40 }; 40 };
41 41
42 42
43 /* Create a semaphore */ 43 /* Create a semaphore */
44 DECLSPEC SDL_sem *SDLCALL 44 DECLSPEC SDL_sem *SDLCALL
45 SDL_CreateSemaphore (Uint32 initial_value) 45 SDL_CreateSemaphore(Uint32 initial_value)
46 { 46 {
47 SDL_sem *sem; 47 SDL_sem *sem;
48 ULONG ulrc; 48 ULONG ulrc;
49 49
50 /* Allocate sem memory */ 50 /* Allocate sem memory */
51 sem = (SDL_sem *) SDL_malloc (sizeof (*sem)); 51 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
52 if (sem) { 52 if (sem) {
53 /* Create the mutex semaphore */ 53 /* Create the mutex semaphore */
54 ulrc = DosCreateMutexSem (NULL, &(sem->id), 0, TRUE); 54 ulrc = DosCreateMutexSem(NULL, &(sem->id), 0, TRUE);
55 if (ulrc) { 55 if (ulrc) {
56 SDL_SetError ("Couldn't create semaphore"); 56 SDL_SetError("Couldn't create semaphore");
57 SDL_free (sem); 57 SDL_free(sem);
58 sem = NULL; 58 sem = NULL;
59 } else { 59 } else {
60 DosCreateEventSem (NULL, &(sem->changed), 0, FALSE); 60 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE);
61 sem->value = initial_value; 61 sem->value = initial_value;
62 DosReleaseMutexSem (sem->id); 62 DosReleaseMutexSem(sem->id);
63 } 63 }
64 } else { 64 } else {
65 SDL_OutOfMemory (); 65 SDL_OutOfMemory();
66 } 66 }
67 return (sem); 67 return (sem);
68 } 68 }
69 69
70 /* Free the semaphore */ 70 /* Free the semaphore */
71 DECLSPEC void SDLCALL 71 DECLSPEC void SDLCALL
72 SDL_DestroySemaphore (SDL_sem * sem) 72 SDL_DestroySemaphore(SDL_sem * sem)
73 { 73 {
74 if (sem) { 74 if (sem) {
75 if (sem->id) { 75 if (sem->id) {
76 DosCloseEventSem (sem->changed); 76 DosCloseEventSem(sem->changed);
77 DosCloseMutexSem (sem->id); 77 DosCloseMutexSem(sem->id);
78 sem->id = 0; 78 sem->id = 0;
79 } 79 }
80 SDL_free (sem); 80 SDL_free(sem);
81 } 81 }
82 } 82 }
83 83
84 DECLSPEC int SDLCALL 84 DECLSPEC int SDLCALL
85 SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout) 85 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
86 { 86 {
87 ULONG ulrc; 87 ULONG ulrc;
88 88
89 if (!sem) { 89 if (!sem) {
90 SDL_SetError ("Passed a NULL sem"); 90 SDL_SetError("Passed a NULL sem");
91 return -1; 91 return -1;
92 } 92 }
93 93
94 if (timeout == SDL_MUTEX_MAXWAIT) { 94 if (timeout == SDL_MUTEX_MAXWAIT) {
95 while (1) { 95 while (1) {
96 ulrc = DosRequestMutexSem (sem->id, SEM_INDEFINITE_WAIT); 96 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
97 if (ulrc) { 97 if (ulrc) {
98 /* if error waiting mutex */ 98 /* if error waiting mutex */
99 SDL_SetError ("DosRequestMutexSem() failed"); 99 SDL_SetError("DosRequestMutexSem() failed");
100 return -1; 100 return -1;
101 } else if (sem->value) { 101 } else if (sem->value) {
102 sem->value--; 102 sem->value--;
103 DosReleaseMutexSem (sem->id); 103 DosReleaseMutexSem(sem->id);
104 return 0; 104 return 0;
105 } else { 105 } else {
106 ULONG ulPostCount; 106 ULONG ulPostCount;
107 DosResetEventSem (sem->changed, &ulPostCount); 107 DosResetEventSem(sem->changed, &ulPostCount);
108 DosReleaseMutexSem (sem->id); 108 DosReleaseMutexSem(sem->id);
109 /* continue waiting until somebody posts the semaphore */ 109 /* continue waiting until somebody posts the semaphore */
110 DosWaitEventSem (sem->changed, SEM_INDEFINITE_WAIT); 110 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT);
111 } 111 }
112 } 112 }
113 } else if (timeout == 0) { 113 } else if (timeout == 0) {
114 ulrc = DosRequestMutexSem (sem->id, SEM_INDEFINITE_WAIT); 114 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
115 if (ulrc == NO_ERROR) { 115 if (ulrc == NO_ERROR) {
116 if (sem->value) { 116 if (sem->value) {
117 sem->value--; 117 sem->value--;
118 DosReleaseMutexSem (sem->id); 118 DosReleaseMutexSem(sem->id);
119 return 0; 119 return 0;
120 } else { 120 } else {
121 DosReleaseMutexSem (sem->id); 121 DosReleaseMutexSem(sem->id);
122 return SDL_MUTEX_TIMEDOUT; 122 return SDL_MUTEX_TIMEDOUT;
123 } 123 }
124 } else { 124 } else {
125 SDL_SetError ("DosRequestMutexSem() failed"); 125 SDL_SetError("DosRequestMutexSem() failed");
126 return -1; 126 return -1;
127 } 127 }
128 } else { 128 } else {
129 ulrc = DosRequestMutexSem (sem->id, SEM_INDEFINITE_WAIT); 129 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
130 if (ulrc) { 130 if (ulrc) {
131 /* if error waiting mutex */ 131 /* if error waiting mutex */
132 SDL_SetError ("DosRequestMutexSem() failed"); 132 SDL_SetError("DosRequestMutexSem() failed");
133 return -1; 133 return -1;
134 } else if (sem->value) { 134 } else if (sem->value) {
135 sem->value--; 135 sem->value--;
136 DosReleaseMutexSem (sem->id); 136 DosReleaseMutexSem(sem->id);
137 return 0; 137 return 0;
138 } else { 138 } else {
139 ULONG ulPostCount; 139 ULONG ulPostCount;
140 DosResetEventSem (sem->changed, &ulPostCount); 140 DosResetEventSem(sem->changed, &ulPostCount);
141 DosReleaseMutexSem (sem->id); 141 DosReleaseMutexSem(sem->id);
142 /* continue waiting until somebody posts the semaphore */ 142 /* continue waiting until somebody posts the semaphore */
143 ulrc = DosWaitEventSem (sem->changed, timeout); 143 ulrc = DosWaitEventSem(sem->changed, timeout);
144 if (ulrc == NO_ERROR) 144 if (ulrc == NO_ERROR)
145 return 0; 145 return 0;
146 else 146 else
147 return SDL_MUTEX_TIMEDOUT; 147 return SDL_MUTEX_TIMEDOUT;
148 } 148 }
150 /* never reached */ 150 /* never reached */
151 return -1; 151 return -1;
152 } 152 }
153 153
154 DECLSPEC int SDLCALL 154 DECLSPEC int SDLCALL
155 SDL_SemTryWait (SDL_sem * sem) 155 SDL_SemTryWait(SDL_sem * sem)
156 { 156 {
157 return SDL_SemWaitTimeout (sem, 0); 157 return SDL_SemWaitTimeout(sem, 0);
158 } 158 }
159 159
160 DECLSPEC int SDLCALL 160 DECLSPEC int SDLCALL
161 SDL_SemWait (SDL_sem * sem) 161 SDL_SemWait(SDL_sem * sem)
162 { 162 {
163 return SDL_SemWaitTimeout (sem, SDL_MUTEX_MAXWAIT); 163 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
164 } 164 }
165 165
166 /* Returns the current count of the semaphore */ 166 /* Returns the current count of the semaphore */
167 DECLSPEC Uint32 SDLCALL 167 DECLSPEC Uint32 SDLCALL
168 SDL_SemValue (SDL_sem * sem) 168 SDL_SemValue(SDL_sem * sem)
169 { 169 {
170 if (!sem) { 170 if (!sem) {
171 SDL_SetError ("Passed a NULL sem"); 171 SDL_SetError("Passed a NULL sem");
172 return 0; 172 return 0;
173 } 173 }
174 return sem->value; 174 return sem->value;
175 } 175 }
176 176
177 DECLSPEC int SDLCALL 177 DECLSPEC int SDLCALL
178 SDL_SemPost (SDL_sem * sem) 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 193
194 /* vi: set ts=4 sw=4 expandtab: */ 194 /* vi: set ts=4 sw=4 expandtab: */