diff options
author | rpj <rpj> | 2004-05-17 01:38:02 +0000 |
---|---|---|
committer | rpj <rpj> | 2004-05-17 01:38:02 +0000 |
commit | 771465fed0cf50ee2dd790723245fc091699c324 (patch) | |
tree | d8c18d095a33fe7c4564bd90c5f313bb9e4057dd /w32_CancelableWait.c | |
parent | 8b14911744f58cbe3730703f3fcc41cd969fd0f3 (diff) |
re-indentation, bug fixes, hooks for pre-emptive async cancelation
Diffstat (limited to 'w32_CancelableWait.c')
-rw-r--r-- | w32_CancelableWait.c | 138 |
1 files changed, 67 insertions, 71 deletions
diff --git a/w32_CancelableWait.c b/w32_CancelableWait.c index 6352ff9..a40fa4d 100644 --- a/w32_CancelableWait.c +++ b/w32_CancelableWait.c @@ -62,87 +62,83 @@ ptw32_cancelable_wait (HANDLE waitHandle, DWORD timeout) handles[0] = waitHandle; - if ((self = pthread_self()) != NULL) + if ((self = pthread_self ()) != NULL) { /* * Get cancelEvent handle */ if (self->cancelState == PTHREAD_CANCEL_ENABLE) - { + { - if ((handles[1] = self->cancelEvent) != NULL) - { - nHandles++; - } - } + if ((handles[1] = self->cancelEvent) != NULL) + { + nHandles++; + } + } } else { handles[1] = NULL; } - status = WaitForMultipleObjects ( - nHandles, - handles, - PTW32_FALSE, - timeout); - - - switch (status - WAIT_OBJECT_0) - { - case 0: - /* - * Got the handle. - * In the event that both handles are signalled, the smallest index - * value (us) is returned. As it has been arranged, this ensures that - * we don't drop a signal that we should act on (i.e. semaphore, - * mutex, or condition variable etc). - */ - result = 0; - break; - - case 1: - /* - * Got cancel request. - * In the event that both handles are signaled, the cancel will - * be ignored (see case 0 comment). - */ - ResetEvent (handles[1]); - - if (self != NULL) - { - /* - * Should handle POSIX and implicit POSIX threads.. - * Make sure we haven't been async-canceled in the meantime. - */ - (void) pthread_mutex_lock(&self->cancelLock); - if (self->state < PThreadStateCanceling) - { - self->state = PThreadStateCanceling; - self->cancelState = PTHREAD_CANCEL_DISABLE; - (void) pthread_mutex_unlock(&self->cancelLock); - ptw32_throw(PTW32_EPS_CANCEL); - - /* Never reached */ - } - (void) pthread_mutex_unlock(&self->cancelLock); - } - - /* Should never get to here. */ - result = EINVAL; - break; - - default: - if (status == WAIT_TIMEOUT) - { - result = ETIMEDOUT; - } - else - { - result = EINVAL; - } - break; - } + status = WaitForMultipleObjects (nHandles, handles, PTW32_FALSE, timeout); + + + switch (status - WAIT_OBJECT_0) + { + case 0: + /* + * Got the handle. + * In the event that both handles are signalled, the smallest index + * value (us) is returned. As it has been arranged, this ensures that + * we don't drop a signal that we should act on (i.e. semaphore, + * mutex, or condition variable etc). + */ + result = 0; + break; + + case 1: + /* + * Got cancel request. + * In the event that both handles are signaled, the cancel will + * be ignored (see case 0 comment). + */ + ResetEvent (handles[1]); + + if (self != NULL) + { + /* + * Should handle POSIX and implicit POSIX threads.. + * Make sure we haven't been async-canceled in the meantime. + */ + (void) pthread_mutex_lock (&self->cancelLock); + if (self->state < PThreadStateCanceling) + { + self->state = PThreadStateCanceling; + self->cancelState = PTHREAD_CANCEL_DISABLE; + (void) pthread_mutex_unlock (&self->cancelLock); + ptw32_throw (PTW32_EPS_CANCEL); + + /* Never reached */ + } + (void) pthread_mutex_unlock (&self->cancelLock); + } + + /* Should never get to here. */ + result = EINVAL; + break; + + default: + if (status == WAIT_TIMEOUT) + { + result = ETIMEDOUT; + } + else + { + result = EINVAL; + } + break; + } return (result); @@ -151,11 +147,11 @@ ptw32_cancelable_wait (HANDLE waitHandle, DWORD timeout) int pthreadCancelableWait (HANDLE waitHandle) { - return (ptw32_cancelable_wait(waitHandle, INFINITE)); + return (ptw32_cancelable_wait (waitHandle, INFINITE)); } int pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout) { - return (ptw32_cancelable_wait(waitHandle, timeout)); + return (ptw32_cancelable_wait (waitHandle, timeout)); } |