This is the mail archive of the mailing list for the pthreas-win32 project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Implementing async canceling


I'm trying to design a way to make async canceling work using Jason Nye's

The way I'm reading Posix, an async cancel should immediately cancel the
target thread if canceling is enabled, and otherwise defere the cancel until
the target thread enables canceling with pthread_setcancelstate().
Canceling should allways call the thread's cleanup handlers.

So, how could this be implemented?

I think that pthread_cancel() should do something like this:

if ( thread->cancelType == PTHREAD_CANCEL_ASYNCRONOUS
	&& thread->cancelState == PTHREAD_CANCEL_ENABLE )
	CancelThread(thread->threadH);	/* Jason's implementation */
{	/* deferred cancelation or async cancelation with cancel disabled */

This immediately cancels the thread if async and canceling are enabled.
Otherwise, it sets the cancelEvent for a deferred cancel.

Now, pthread_setcancelstate() should check if there are pending async cancel

.  code for setting the new cancel state is placed here
if (self->cancelState == PTHREAD_CANCEL_ENABLE
	&& ( WaitForSingleObject(self->cancelEvent, 0) == WAIT_OBJECT_0 ))
		/* the thread has a cancel request pending, perform the
cancel */

Note that you should consider all of this as pseudocode; it's just a simple
design and has no error checking nor has it ever seen a compiler.

CancelMe() should be a function that calls the thread's cleanup handlers
etc. Is there yet such a function available? CancelMe() is also the function
that CancelThread() sets the target threads eip to.

My questions are:
a) any comments on this design?
b) what should CancelMe() do?
c) is this implementation Posix complient?

Erik Hensema

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]