🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #449

Re: Interrupts in OoO

#449
From: EricP
Date: Thu, 29 Oct 2026 18:50
111 lines
5125 bytes
On 2026-Oct-28 18:10, Chris M. Thomasson wrote:
> On 10/28/2026 11:39 AM, EricP wrote:
>> On 2026-Oct-26 17:14, Chris M. Thomasson wrote:
>>> On 10/26/2026 2:03 PM, EricP wrote:
>>>>
>>>
>>> Have you ever messed around with a Pthread async cancel?
>>>
>>> PTHREAD_CANCEL_ASYNCHRONOUS
>>>
>>> Iirc, a pthread emulation over on windows had to use a damn kernel driver to do it. I say, avoid at all costs!
>>
>> No, I have never used Pthreads and I have no idea
>> how it is implemented on Windows.
> 
> Okay.
> 
>>
>> It sounds like it calls TerminateThread which does exactly that,
>> without giving an application any chance to clean up nicely.
>> But in the Unix manuals I also see warnings about the use of
>> async cancel in general on any implementation.
> 
> Its odd. It needs an actual kernel driver to implement PTHREAD_CANCEL_ASYNCHRONOUS via, iirc, kernel mode APC.

People often use drivers to work around functions requiring
elevated privileges.

>>
>> VMS got this one right - the ability to asynchronously force a
>> process/thread to do a clean exit.
>> A call to SYS$FORCEEX posts a user mode AST to the process/thread,
>> which *interrupts* and raises a FORCE_EXIT exception in the process/thread,
>> allowing stack exception catch handlers or thread exit rundown handlers
>> a chance to clean up properly (release mutexes, finish a heap
>> allocation without corrupting the heap, etc).
>>
>> WNT doesn't have asynchronous (interrupting) user mode APC's,
>> just synchronous (polled) ones at alertable waits.
>> So you can't always force another thread to do your bidding like stopping.
>> (It is actually possible to do this using GetThreadContext and SetThreadContext.
>> But as this would be non-standard the surrounding code for things like
>> mutex holders and heap allocators would not expect it and would have no
>> supporting exit handler code for it.
>> Also Get/SetThreadContext requires debug privilege.)
>>
>>
> 

It appears what I speculated is exactly what they did.

https://sourceforge.net/projects/pthreads4w/files/

This refers to them writing their own function QueueUserAPCEx
to force a really asynchronous (interrupting) subroutine call
onto a thread.

I didn't want to download their files and search them but
found some documentation elsewhere:

https://locklessinc.com/articles/pthreads_on_windows/

You can see below they are doing exactly what I said,
using GetThreadContext and SetThreadContext to "edit" the thread
and jam their own routine call into it.
It sounds like they packaged this into a DLL and invoke it with a
driver to get around the need for giving Debug privilege to everyone.

This won't work for the reasons I said above:
the software above it in the call stack must have been written
*knowing* that this "force exit" can happen and have established
exit handlers for the stack unwind. The OS must define this
force-exit mechanism right from the start so all cleanup code uses it.
VMS did that, WNT did not.

static int pthread_cancel(pthread_t t)
{
	if (t->p_state & PTHREAD_CANCEL_ASYNCHRONOUS)
	{
		/* Dangerous asynchronous cancelling */
		CONTEXT ctxt;
		
		/* Already done? */
		if (t->cancelled) return ESRCH;
		
		ctxt.ContextFlags = CONTEXT_CONTROL;
		
		SuspendThread(t->h);
		GetThreadContext(t->h, &ctxt);
#ifdef _M_X64
		ctxt.Rip = (uintptr_t) _pthread_invoke_cancel;
#else
		ctxt.Eip = (uintptr_t) _pthread_invoke_cancel;
#endif
		SetThreadContext(t->h, &ctxt);
		
		/* Also try deferred Cancelling */
		t->cancelled = 1;
	
		/* Notify everyone to look */
		_InterlockedIncrement(&_pthread_cancelling);
		
		ResumeThread(t->h);
	}






Technical Details
Message-ID: <EzMES.77867$Hu16.65270@fx22.iad>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx22.iad.POSTED!not-for-mail
References: <2024Oct3.160055@mips.complang.tuwien.ac.at> <vdmrk6$3rksr$1@dont-email.me> <LyELO.69485$2nv5.62232@fx39.iad> <TdWLO.282116$FzW1.158190@fx14.iad> <963a276fd8d43e4212477cefae7f6e46@www.novabbs.org> <8IcMO.249144$v8v2.147178@fx18.iad> <2024Oct5.195712@mips.complang.tuwien.ac.at> <6adbf319.9129937@news.eternal-september.org> <1792869649-5857@newsgrouper.org> <fCpDS.27362$9b8f.8498@fx39.iad> <L%JDS.15598$fJ0f.4191@fx44.iad> <11bo1vr$pqob$1@dont-email.me> <zePDS.2$1GC5.1@fx45.iad> <11bofs0$vhqi$1@dont-email.me> <2krES.62082$BaS5.11177@fx35.iad> <11btrsu$2rb9b$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx22.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Interrupts in OoO
Newsgroups: comp.arch
References: <2024Oct3.160055@mips.complang.tuwien.ac.at>
 <vdmrk6$3rksr$1@dont-email.me> <LyELO.69485$2nv5.62232@fx39.iad>
 <TdWLO.282116$FzW1.158190@fx14.iad>
 <963a276fd8d43e4212477cefae7f6e46@www.novabbs.org>
 <8IcMO.249144$v8v2.147178@fx18.iad>
 <2024Oct5.195712@mips.complang.tuwien.ac.at>
 <6adbf319.9129937@news.eternal-september.org>
 <1792869649-5857@newsgrouper.org> <fCpDS.27362$9b8f.8498@fx39.iad>
 <L%JDS.15598$fJ0f.4191@fx44.iad> <11bo1vr$pqob$1@dont-email.me>
 <zePDS.2$1GC5.1@fx45.iad> <11bofs0$vhqi$1@dont-email.me>
 <2krES.62082$BaS5.11177@fx35.iad> <11btrsu$2rb9b$1@dont-email.me>
Content-Language: en-CA
From: EricP <ThatWouldBeTelling@thevillage.com>
In-Reply-To: <11btrsu$2rb9b$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 111
Message-ID: <EzMES.77867$Hu16.65270@fx22.iad>
X-Complaints-To: abuse@UsenetServer.com
NNTP-Posting-Date: Thu, 29 Oct 2026 18:50:12 UTC
Date: Thu, 29 Oct 2026 14:50:11 -0400
X-Received-Bytes: 4893