waiting on a system thread to finish

I create a system thread with PsCreateSystemThread(…). I pass in a
pointer to the thread handle which gets set by a successful call.

In user space, when you want to shut a worker thread down, you can ask the
thread to stop, and then wait on the thread handle you got back when you
created the thread. Often you have to do that to keep things from going
awry when your application is shutting down.

I need to do the same thing in the kernel: I tell my kernel thread to
terminate, and it calls PsTerminateSystemThread(…) on itself.

But I don’t want any driver shutdown and cleanup to continue until that
thread is gone. What is the method used to know when
PsTerminateSystemThread(…) returns?

I tried KeWaitForSingleObject(…) using the thread handle from
PsCreateSystemThread, but that didn’t work.

Thanks,
Skip

KeWaitForSingleObject() requires a POINTER to the Thread object, not a
HANDLE.

1.) Use ObReferenceObjectByHandle() to get a pointer to the Thread object.

2.) Call ZwClose() to get rid of your reference to the Thread object handle.

3.) When you don’t need the pointer anymore (i.e., after you’ve finished
your KeWaitFor…), use ObDereferenceObject() to release the Thread object
pointer.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of skip
Sent: Friday, April 26, 2002 12:12 PM
To: NT Developers Interest List
Subject: [ntdev] waiting on a system thread to finish

I create a system thread with PsCreateSystemThread(…). I pass in a
pointer to the thread handle which gets set by a successful call.

In user space, when you want to shut a worker thread down, you can ask the
thread to stop, and then wait on the thread handle you got back when you
created the thread. Often you have to do that to keep things from going
awry when your application is shutting down.

I need to do the same thing in the kernel: I tell my kernel thread to
terminate, and it calls PsTerminateSystemThread(…) on itself.

But I don’t want any driver shutdown and cleanup to continue until that
thread is gone. What is the method used to know when
PsTerminateSystemThread(…) returns?

I tried KeWaitForSingleObject(…) using the thread handle from
PsCreateSystemThread, but that didn’t work.

Thanks,
Skip


You are currently subscribed to ntdev as: xxxxx@nfr.com
To unsubscribe send a blank email to %%email.unsub%%

** Reply to message from “skip” on Fri, 26 Apr 2002
12:11:54 -0400

> But I don’t want any driver shutdown and cleanup to continue until that
> thread is gone. What is the method used to know when
> PsTerminateSystemThread(…) returns?
>
> I tried KeWaitForSingleObject(…) using the thread handle from
> PsCreateSystemThread, but that didn’t work.

This has been discussed pretty recently. Here’s the gist.

1. Call ObReferenceObjectByHandle() to get the pointer to the thread object.
2. Call ZwClose(threadhandle), because you don’t need the handle anymore.
3. The code that created the thread sets a “kill” event when it wants to
terminate the thread.
4. The thread waits on the kill event, and calls PsTerminateSystemThread on
itself when the event is set.
5. After setting the “kill” event, the code that created the thread waits on
the thread object.
6. When the thread actually dies, the thread object is signaled, and the main
code can proceed with the knowledge that the thread is gone.

Sincerely,

Chris Myers
Senior Project Engineer
Quatech, Inc.

> I need to do the same thing in the kernel: I tell my kernel thread to

terminate

How did you do this? This is surely a place of your bug.

Max