PTHREAD_CANCEL_DEFERRED

Kostik Belousov kostikbel at gmail.com
Wed Aug 11 21:19:29 UTC 2010


Hi,
Let consider the thread in the state where the cancelation is enabled
and cancelation mode set to the PTHREAD_CANCEL_DEFERRED.

SUSv4 says the following:
Whenever a thread has cancelability enabled and a cancellation request
has been made with that thread as the target, and the thread then
calls any function that is a cancellation point (such as
pthread_testcancel() or read()), the cancellation request shall be
acted upon before the function returns. If a thread has cancelability
enabled and a cancellation request is made with the thread as a target
while the thread is suspended at a cancellation point, the thread
shall be awakened and the cancellation request shall be acted upon.

Take the close(2) as example, and assume that the cancel is enabled
for the thread in deferred mode. libthr wrapper around the syscall
executes this:

     	curthread->cancel_point++;
	testcancel(curthread);
	__sys_close(fd);
	curthread->cancel_point--;

The pthread_cancel() sends the SIGCANCEL signal to the
thread. SIGCANCEL handler calls pthread_exit() if thread has the
cancel_point greater then 0.

I think this is not right. For instance, thread can be canceled due to
SIGCANCEL delivery at the point after the return into the usermode
from close(), but before cancel_point is decremented. IMO, the cited
statements from the SUSv4 prohibit such behaviour. We should cancel
either before closing fd, or during the sleep in close(), but then the
syscall should be aborted ("as if signal is delivered"), and again fd
should not be closed.

Actually, besides not being compliant, I think that the current
behaviour is not useful, since in deferred case, we cannot know
whether the action by the call that is cancelation point was performed
or not.

This is not a rant, I probably will fix the issue if it is agreed
upon. Opinions ?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-threads/attachments/20100811/a4d541b6/attachment.pgp


More information about the freebsd-threads mailing list