Mercurial > dwindows
comparison win/dw.c @ 1155:e6a2f57c0842
Added support for infinite wait for dw_event_wait() on Mac, Unix and Windows.
Return code cleanup for dw_event_* and dw_named_event_* on Windows.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Thu, 15 Sep 2011 03:13:50 +0000 |
parents | 052f01522c53 |
children | f86f556ff29d |
comparison
equal
deleted
inserted
replaced
1154:052f01522c53 | 1155:e6a2f57c0842 |
---|---|
9227 * Parameters: | 9227 * Parameters: |
9228 * eve: The handle to the event returned by dw_event_new(). | 9228 * eve: The handle to the event returned by dw_event_new(). |
9229 */ | 9229 */ |
9230 int API dw_event_reset(HEV eve) | 9230 int API dw_event_reset(HEV eve) |
9231 { | 9231 { |
9232 return ResetEvent(eve); | 9232 if(ResetEvent(eve)) |
9233 return DW_ERROR_NONE; | |
9234 return DW_ERROR_GENERAL; | |
9233 } | 9235 } |
9234 | 9236 |
9235 /* | 9237 /* |
9236 * Posts a semaphore created by dw_event_new(). Causing all threads | 9238 * Posts a semaphore created by dw_event_new(). Causing all threads |
9237 * waiting on this event in dw_event_wait to continue. | 9239 * waiting on this event in dw_event_wait to continue. |
9238 * Parameters: | 9240 * Parameters: |
9239 * eve: The handle to the event returned by dw_event_new(). | 9241 * eve: The handle to the event returned by dw_event_new(). |
9240 */ | 9242 */ |
9241 int API dw_event_post(HEV eve) | 9243 int API dw_event_post(HEV eve) |
9242 { | 9244 { |
9243 return (SetEvent(eve) == 0); | 9245 if(SetEvent(eve)) |
9246 return DW_ERROR_NONE; | |
9247 return DW_ERROR_GENERAL; | |
9244 } | 9248 } |
9245 | 9249 |
9246 /* | 9250 /* |
9247 * Waits on a semaphore created by dw_event_new(), until the | 9251 * Waits on a semaphore created by dw_event_new(), until the |
9248 * event gets posted or until the timeout expires. | 9252 * event gets posted or until the timeout expires. |
9251 */ | 9255 */ |
9252 int API dw_event_wait(HEV eve, unsigned long timeout) | 9256 int API dw_event_wait(HEV eve, unsigned long timeout) |
9253 { | 9257 { |
9254 int rc; | 9258 int rc; |
9255 | 9259 |
9256 rc = WaitForSingleObject(eve, timeout); | 9260 rc = WaitForSingleObject(eve, timeout != -1 ? timeout : INFINITE); |
9257 if(rc == WAIT_OBJECT_0) | 9261 if(rc == WAIT_OBJECT_0) |
9258 return DW_ERROR_NONE; | 9262 return DW_ERROR_NONE; |
9259 if(rc == WAIT_TIMEOUT) | 9263 if(rc == WAIT_TIMEOUT) |
9260 return DW_ERROR_TIMEOUT; | 9264 return DW_ERROR_TIMEOUT; |
9261 if(rc == WAIT_ABANDONED) | 9265 if(rc == WAIT_ABANDONED) |
9262 return DW_ERROR_NON_INIT; | 9266 return DW_ERROR_INTERRUPT; |
9263 return DW_ERROR_GENERAL; | 9267 return DW_ERROR_GENERAL; |
9264 } | 9268 } |
9265 | 9269 |
9266 /* | 9270 /* |
9267 * Closes a semaphore created by dw_event_new(). | 9271 * Closes a semaphore created by dw_event_new(). |
9268 * Parameters: | 9272 * Parameters: |
9269 * eve: The handle to the event returned by dw_event_new(). | 9273 * eve: The handle to the event returned by dw_event_new(). |
9270 */ | 9274 */ |
9271 int API dw_event_close(HEV *eve) | 9275 int API dw_event_close(HEV *eve) |
9272 { | 9276 { |
9273 if(eve) | 9277 if(eve && CloseHandle(*eve)) |
9274 return (CloseHandle(*eve) == 0); | 9278 return DW_ERROR_NONE; |
9275 return DW_ERROR_NONE; | 9279 return DW_ERROR_GENERAL; |
9276 } | 9280 } |
9277 | 9281 |
9278 /* Create a named event semaphore which can be | 9282 /* Create a named event semaphore which can be |
9279 * opened from other processes. | 9283 * opened from other processes. |
9280 * Parameters: | 9284 * Parameters: |
9309 * eve: Handle to the semaphore obtained by | 9313 * eve: Handle to the semaphore obtained by |
9310 * an open or create call. | 9314 * an open or create call. |
9311 */ | 9315 */ |
9312 int API dw_named_event_reset(HEV eve) | 9316 int API dw_named_event_reset(HEV eve) |
9313 { | 9317 { |
9314 int rc; | 9318 if(ResetEvent(eve)) |
9315 | 9319 return DW_ERROR_NONE; |
9316 rc = ResetEvent(eve); | 9320 return DW_ERROR_GENERAL; |
9317 if(!rc) | |
9318 return DW_ERROR_GENERAL; | |
9319 | |
9320 return DW_ERROR_NONE; | |
9321 } | 9321 } |
9322 | 9322 |
9323 /* Sets the posted state of an event semaphore, any threads | 9323 /* Sets the posted state of an event semaphore, any threads |
9324 * waiting on the semaphore will no longer block. | 9324 * waiting on the semaphore will no longer block. |
9325 * Parameters: | 9325 * Parameters: |
9326 * eve: Handle to the semaphore obtained by | 9326 * eve: Handle to the semaphore obtained by |
9327 * an open or create call. | 9327 * an open or create call. |
9328 */ | 9328 */ |
9329 int API dw_named_event_post(HEV eve) | 9329 int API dw_named_event_post(HEV eve) |
9330 { | 9330 { |
9331 int rc; | 9331 if(SetEvent(eve)) |
9332 | 9332 DW_ERROR_NONE; |
9333 rc = SetEvent(eve); | 9333 return DW_ERROR_GENERAL; |
9334 if(!rc) | |
9335 return 1; | |
9336 | |
9337 return 0; | |
9338 } | 9334 } |
9339 | 9335 |
9340 /* Waits on the specified semaphore until it becomes | 9336 /* Waits on the specified semaphore until it becomes |
9341 * posted, or returns immediately if it already is posted. | 9337 * posted, or returns immediately if it already is posted. |
9342 * Parameters: | 9338 * Parameters: |
9347 */ | 9343 */ |
9348 int API dw_named_event_wait(HEV eve, unsigned long timeout) | 9344 int API dw_named_event_wait(HEV eve, unsigned long timeout) |
9349 { | 9345 { |
9350 int rc; | 9346 int rc; |
9351 | 9347 |
9352 rc = WaitForSingleObject(eve, timeout); | 9348 rc = WaitForSingleObject(eve, timeout != -1 ? timeout : INFINITE); |
9353 switch (rc) | 9349 if(rc == WAIT_OBJECT_0) |
9354 { | 9350 return DW_ERROR_NONE; |
9355 case WAIT_FAILED: | 9351 if(rc == WAIT_TIMEOUT) |
9356 rc = DW_ERROR_TIMEOUT; | 9352 return DW_ERROR_TIMEOUT; |
9357 break; | 9353 if(rc == WAIT_ABANDONED) |
9358 | 9354 return DW_ERROR_INTERRUPT; |
9359 case WAIT_ABANDONED: | 9355 return DW_ERROR_GENERAL; |
9360 rc = DW_ERROR_INTERRUPT; | |
9361 break; | |
9362 | |
9363 case WAIT_OBJECT_0: | |
9364 rc = 0; | |
9365 break; | |
9366 } | |
9367 | |
9368 return rc; | |
9369 } | 9356 } |
9370 | 9357 |
9371 /* Release this semaphore, if there are no more open | 9358 /* Release this semaphore, if there are no more open |
9372 * handles on this semaphore the semaphore will be destroyed. | 9359 * handles on this semaphore the semaphore will be destroyed. |
9373 * Parameters: | 9360 * Parameters: |
9374 * eve: Handle to the semaphore obtained by | 9361 * eve: Handle to the semaphore obtained by |
9375 * an open or create call. | 9362 * an open or create call. |
9376 */ | 9363 */ |
9377 int API dw_named_event_close(HEV eve) | 9364 int API dw_named_event_close(HEV eve) |
9378 { | 9365 { |
9379 int rc; | 9366 if(CloseHandle(eve)) |
9380 | 9367 return DW_ERROR_NONE; |
9381 rc = CloseHandle(eve); | 9368 return DW_ERROR_GENERAL; |
9382 if(!rc) | |
9383 return 1; | |
9384 | |
9385 return 0; | |
9386 } | 9369 } |
9387 | 9370 |
9388 /* | 9371 /* |
9389 * Allocates a shared memory region with a name. | 9372 * Allocates a shared memory region with a name. |
9390 * Parameters: | 9373 * Parameters: |