Safely implementing Thread.Abort
"Thong (Tum) Nguyen" <tum-x2aT3/[email protected]>
| Newsgroups | gmane.comp.gnu.dotgnu.developer |
|---|---|
| Message-ID | <[email protected]> |
Hi :) I've been thinking about possible ways to safely implementing Thread.Abort. Thread.Abort appears to be important with AppDomains and since AppDomains are the CLR's equivalent of processes, Thread.Abort is required to safely kill and cleanup a "process". The requirements for safely implementing abort would be that abort requests are only processed while executing managed code (definitely not in unmanaged code) and that it is done at a safe point in the code. It has been suggested that there should be a check at every backwards branch instruction for an abort but that would be a performance killer. Here's something which might be feasible... We could generate a marker at each backwards branch instruction (a series of NOP instructions would do). All marker addresses are recorded in the method info. When an abort is generated, the thread is suspended using an "unsafe" suspend (SuspendThread on windows and signals on unix). While the thread is suspended, the NOP instructions are replaced with calls to a function that checks/throws aborts. The thread is then resumed. On resumption, the thread won't immediately abort (which is important if it is half way through executing a critical piece of code) but it will abort at the next backwards jump or the next time it enters a wait_join_sleep state. When the function that checks/throws abort is called, it will turn the call instructions back into NOPs. I think this could work...what do you guys think? ^Tum