Atomic "heartbeat and system call entry"

I am trying to implement my own cancellation point mechanism in C ++. The code is mostly Linux oriented, but I hope it will be portable across POSIX systems.

For the obvious reason, every blocking system call must be a cancellation point, otherwise the thread might block forever and nothing can wake it up. I am currently using a signal to achieve this. When a thread receives a special signal representing a cancellation request, the signal handler sets up the local thread. If this thread is blocked on some system call, it will crash with errno == EINTR

. Then he will have the opportunity to check the flag.

But I soon discovered that the above method is not ideal. If the signal comes in first and then the thread enters the blocking system call, it also blocks forever. Checking the flag before every blocking call doesn't do things good enough. The signal can appear immediately after checking:

if(cancelled)
    throw CancellationException();
 // <--- The signal comes here. ---> //
blocking_call();

      

Here's my problem: is there a way to make sure that the cancel flag is not set when the system call is entered? If not, what is the common practice for developing such cancellation clauses?

+3


source to share


1 answer


I'll make this a real answer.



If you want to do something about cancellation for pthreads check out pthread cancellation handlers . Could this work to drop your exceptions?

0


source







All Articles