From 5958e39b3f98528cbe9a8ae8d98a457db9039179 Mon Sep 17 00:00:00 2001 From: davidxu Date: Fri, 20 Aug 2010 05:15:39 +0000 Subject: In current implementation, thread cancellation is done in signal handler, which does not know what is the state of interrupted system call, for example, open() system call opened a file and the thread is still cancelled, result is descriptor leak, there are other problems which can cause resource leak or undeterminable side effect when a thread is cancelled. However, this is no longer true in new implementation. In defering mode, a thread is canceled if cancellation request is pending and later the thread enters a cancellation point, otherwise, a later pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and causes target thread to abort system call, userland code in libthr then checks cancellation state, and cancels the thread if needed. For example, the cancellation point open(), the thread may be canceled at start, but later, if it opened a file descriptor, it is not canceled, this avoids file handle leak. Another example is read(), a thread may be canceled at start of the function, but later, if it read some bytes from a socket, the thread is not canceled, the caller then can decide if it should still enable cancelling or disable it and continue reading data until it thinks it has read all bytes of a packet, and keeps a protocol stream in health state, if user ignores partly reading of a packet without disabling cancellation, then second iteration of read loop cause the thread to be cancelled. An exception is that the close() cancellation point always closes a file handle despite whether the thread is cancelled or not. The old mechanism is still kept, for a functions which is not so easily to fix a cancellation problem, the rough mechanism is used. Reviewed by: kib@ --- lib/libthr/thread/thr_join.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/libthr/thread/thr_join.c') diff --git a/lib/libthr/thread/thr_join.c b/lib/libthr/thread/thr_join.c index 139c74c..8201aba 100644 --- a/lib/libthr/thread/thr_join.c +++ b/lib/libthr/thread/thr_join.c @@ -68,6 +68,10 @@ _pthread_timedjoin_np(pthread_t pthread, void **thread_return, return (join_common(pthread, thread_return, abstime)); } +/* + * Cancellation behavior: + * if the thread is canceled, joinee is not recycled. + */ static int join_common(pthread_t pthread, void **thread_return, const struct timespec *abstime) @@ -103,10 +107,11 @@ join_common(pthread_t pthread, void **thread_return, THREAD_LIST_UNLOCK(curthread); THR_CLEANUP_PUSH(curthread, backout_join, pthread); - _thr_cancel_enter(curthread); + _thr_cancel_enter_defer(curthread, 1); tid = pthread->tid; while (pthread->tid != TID_TERMINATED) { + _thr_testcancel(curthread); if (abstime != NULL) { clock_gettime(CLOCK_REALTIME, &ts); TIMESPEC_SUB(&ts2, abstime, &ts); @@ -122,7 +127,7 @@ join_common(pthread_t pthread, void **thread_return, break; } - _thr_cancel_leave(curthread); + _thr_cancel_leave_defer(curthread, 0); THR_CLEANUP_POP(curthread, 0); if (ret == ETIMEDOUT) { -- cgit v1.1