From ec8290acdaea21b16d98f1ef5d4ae8a28ab2109a Mon Sep 17 00:00:00 2001 From: rpj Date: Wed, 3 Nov 2004 01:08:41 +0000 Subject: Mutex, semaphore, thread ID, test suite changes - see ChangeLogs --- pthread_cancel.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'pthread_cancel.c') diff --git a/pthread_cancel.c b/pthread_cancel.c index 69cec3b..c12a44d 100644 --- a/pthread_cancel.c +++ b/pthread_cancel.c @@ -121,6 +121,7 @@ pthread_cancel (pthread_t thread) int result; int cancel_self; pthread_t self; + ptw32_thread_t * tp; /* This is the proper way to test thread validity. */ result = pthread_kill (thread, 0); @@ -129,7 +130,7 @@ pthread_cancel (pthread_t thread) return result; } - if ((self = pthread_self ()) == NULL) + if ((self = pthread_self ()).p == NULL) { return ENOMEM; }; @@ -150,35 +151,37 @@ pthread_cancel (pthread_t thread) */ cancel_self = pthread_equal (thread, self); + tp = (ptw32_thread_t *) thread.p; + /* * Lock for async-cancel safety. */ - (void) pthread_mutex_lock (&thread->cancelLock); + (void) pthread_mutex_lock (&tp->cancelLock); - if (thread->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS - && thread->cancelState == PTHREAD_CANCEL_ENABLE - && thread->state < PThreadStateCanceling) + if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS + && tp->cancelState == PTHREAD_CANCEL_ENABLE + && tp->state < PThreadStateCanceling) { if (cancel_self) { - thread->state = PThreadStateCanceling; - thread->cancelState = PTHREAD_CANCEL_DISABLE; + tp->state = PThreadStateCanceling; + tp->cancelState = PTHREAD_CANCEL_DISABLE; - (void) pthread_mutex_unlock (&thread->cancelLock); + (void) pthread_mutex_unlock (&tp->cancelLock); ptw32_throw (PTW32_EPS_CANCEL); /* Never reached */ } else { - HANDLE threadH = thread->threadH; + HANDLE threadH = tp->threadH; SuspendThread (threadH); if (WaitForSingleObject (threadH, 0) == WAIT_TIMEOUT) { - thread->state = PThreadStateCanceling; - thread->cancelState = PTHREAD_CANCEL_DISABLE; + tp->state = PThreadStateCanceling; + tp->cancelState = PTHREAD_CANCEL_DISABLE; /* * If alertdrv and QueueUserAPCEx is available then the following * will result in a call to QueueUserAPCEx with the args given, otherwise @@ -186,7 +189,7 @@ pthread_cancel (pthread_t thread) * the threadH arg will be used. */ ptw32_register_cancelation (ptw32_cancel_callback, threadH, 0); - (void) pthread_mutex_unlock (&thread->cancelLock); + (void) pthread_mutex_unlock (&tp->cancelLock); ResumeThread (threadH); } } @@ -196,13 +199,20 @@ pthread_cancel (pthread_t thread) /* * Set for deferred cancellation. */ - if (thread->state >= PThreadStateCanceling - || !SetEvent (thread->cancelEvent)) + if (tp->state < PThreadStateCancelPending) + { + tp->state = PThreadStateCancelPending; + if (!SetEvent (tp->cancelEvent)) + { + result = ESRCH; + } + } + else if (tp->state >= PThreadStateCanceling) { result = ESRCH; } - (void) pthread_mutex_unlock (&thread->cancelLock); + (void) pthread_mutex_unlock (&tp->cancelLock); } return (result); -- cgit v1.2.3