This is the mail archive of the pthreads-win32@sourceware.cygnus.com 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]

Re: asynchronous cancellation


John, Jason, all,

The following applies to the C++ version of things, but SEH versions
should hopefully be similar.

Following on from John's explanation of C++ stack unwinding etc, and
the fact that pthreads-win32 implements cancelation by throwing an
exception, and the fact that the C++ version of pthread_cleanup_pop
is implemented as a destructor (after John's original
implementation):

For deferred cancelation:
1) stack unwinding should occur properly IMO

2) cleanup handlers should be called.

For async cancelation:
1) Jason's method could be used with the following simple change to
   AsyncCancelPoint to work with the current pthreads-win32
   mechanism:

void cancelThread(HANDLE hThread)
{
    ::SuspendThread(hThread);
    if (::WaitForSingleObject(hThread, 0) != WAIT_TIMEDOUT) {
            // Ok, thread did not exit before we got to it.
           CONTEXT context;
           context.ContextFlags = CONTEXT_CONTROL;
           ::GetThreadContext(hThread, &context);
           // _x86 only!!!
           context.Eip = (DWORD)AsyncCancelPoint;
           ::SetThreadContext(hThread, &context);
           ::ResumeThread(hThread);
    }
}

// declare AsyncCancelPoint:
void AsyncCancelPoint()
{
    // This is all it needs to do to call cleanup handlers,
    // call object destructors, and exit the thread.
    throw Pthread_exception_cancel();
}

For cancelation in general:
1) the following macro should be included in pthread.h:

#define catch(E) \
	catch(Pthread_exception_cancel) { \
		throw(); \
	} \
	catch(E)


Is this getting closer to the ideal, or am I way off the track?

Ross

+----------------------+---+
| Ross Johnson         |   | E-Mail: rpj@ise.canberra.edu.au
| Info Sciences and Eng|___|
| University of Canberra   | FAX:    +61 6 2015227
| PO Box 1                 |
| Belconnen  ACT    2616   | WWW:    http://willow.canberra.edu.au/~rpj/
| AUSTRALIA                |
+--------------------------+



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