Windos9x/me has no safe way to terminate a thread. NT on the other hand
has a simple and yet DDK-undocumented mechanism to safely wait for
thread termination.
- Use ObReferenceObjectByHandle to obtain a pointer to the thread
object you created when you called PsCreateSystemThread. This pointer is
‘safe’ as the thread object reference count has been incremented to
account for its existance. Call ZwClose to dereference the thread handle
-
Now inform your worker thread through whatever mechanism you choose
(an event, a global flag, whatever,) that it is time to die.
-
Then simply call KeWaitForSingleObject using the object pointer you
obtained from ObReferenceObjectByHandle. When your thread really has
terminated the object will be signalled (and not destroyed as you have
its reference count incremented.)
-
On return from KeWait you need to dereference the object by calling
ObDereferenceObject.
Here is a code segment from the w2k ddk floppy.c that illustrates this
technique:
status = PsCreateSystemThread(&threadHandle,
(ACCESS_MASK) 0L,
&ObjAttributes,
(HANDLE) 0L,
NULL,
FloppyThread,
DisketteExtension);
if (!NT_SUCCESS(status)) {
// deleted stuff here for error handling.
return status;
}
status = ObReferenceObjectByHandle( threadHandle,
SYNCHRONIZE,
NULL,
KernelMode,
&DisketteExtension->FloppyThread,
NULL );
ASSERT(NT_SUCCESS(status));
ZwClose(threadHandle);
And later on in the same file, the termination handler:
ASSERT(disketteExtension->FloppyThread != NULL);
KeWaitForSingleObject( disketteExtension->FloppyThread,
Executive,
KernelMode,
FALSE,
NULL );
if (disketteExtension->FloppyThread != NULL) {
ObDereferenceObject( disketteExtension->FloppyThread );
}
disketteExtension->FloppyThread = NULL;
=====================
Mark Roddy
Windows XP/2000/NT Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com
xxxxx@hollistech.com
For Windows Device Driver Training: see www.azius.com
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of David J. Craig
Sent: Monday, March 25, 2002 6:09 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Thread termination in a Win2K driver
I would change the wait logic to wait for the thread to
terminate. One trick for drivers is to have the thread that
is waiting to drop its priority to the lowest and increase
the other thread(s) to the highest. You can do a
KeWaitForSingleObject on a thread also and that is the
simplest solution.
----- Original Message -----
From: “Roy M. Silvernail”
> To: “NT Developers Interest List”
> Sent: Monday, March 25, 2002 5:21 PM
> Subject: [ntdev] Thread termination in a Win2K driver
>
>
> > I have an inherited project that’s giving me fits. It’s a driver
> > originally developed on Win98, which I’m moving to Win2K.
> The problem
> > is that the driver creates a worker thread (using
> > PsCreateSystemThread()). When the driver exits (it’s an IEEE1394
> > driver, so the plug might be pulled while the computer is up), I get
> >
> > *** Fatal System Error: 0x000000ce
> >
> > and ‘!analyze -v’ tells me the driver “unloaded without cancelling
> > timers, DPCs, worker threads, etc.”
> >
> > I’ve narrowed it down to this worker thread. If I never start the
> > thread, I can exit without incident.
> >
> > The thread does call PsTerminateSystemThread() as it exits, and
> > KdPrint statements seem to show that the cleanup routines are being
> > called and completing. The main driver thread waits on an event
> > (which the worker thread signals as it exits) before completing the
> > StopDevice code, so everything looks right. Yet the
> machine still
> > crashes, whether in response to a surprise removal or from a stop
> > command from the ‘Unplug or Eject Hardware’ dialog.
> >
> > Any clues you guys have to offer will be gratefully accepted.
> > –
> > Roy M. Silvernail
> > Software Engineer
> > Acroloop Motion Control Systems, Inc.
> > xxxxx@acroloop.com
> >
> >
> > —
> > You are currently subscribed to ntdev as: xxxxx@yoshimuni.com To
> > unsubscribe send a blank email to %%email.unsub%%
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@hollistech.com To unsubscribe send a blank email to
> %%email.unsub%%
>
>
>