Newbie question: Changing thread contexts

Under what conditions can the thread context change?

I am concerned that when I call some routine, e.g. IoCallDriver or
KeWaitForSingleObject, and that when I return I come back in a different
context.

I just want to make sure that this cannot happen (assuming the system is
functioning correctly).

Ralph Shnelvar


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Ralph,

This is both a very simple question to answer (“never”) and very difficult -
because the very way the OS works is by changing thread context when a
thread calls KeWaitForSingleObject - but it changes to a DIFFERENT thread
context, so from the perspective of the thread that made the initial call
everything else that happens is “transparent”.

Think about it this way - a thread calls KeWaitForSingleObject. When the
call returns, it returns *for that thread* because something has occurred
that has satisfied the call to KeWaitForSingleObject (a timeout, the
signaling of the event, an APC is delivered or the thread is alerted.) What
the OS has done in the meantime doesn’t have any bearing on that thread.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: Ralph Shnelvar [mailto:xxxxx@dos32.com]
Sent: Monday, December 24, 2001 12:28 PM
To: File Systems Developers
Subject: [ntfsd] Newbie question: Changing thread contexts

Under what conditions can the thread context change?

I am concerned that when I call some routine, e.g. IoCallDriver or
KeWaitForSingleObject, and that when I return I come back in a different
context.

I just want to make sure that this cannot happen (assuming the system is
functioning correctly).

Ralph Shnelvar


You are currently subscribed to ntfsd as: xxxxx@osr.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

> I just want to make sure that this cannot happen (assuming the system is

DISPATCH_LEVEL is a mean of achieving this.

Max


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Not if you want to call KeWaitXXX while at it:-)

“Maxim S. Shatskih” wrote:

> I just want to make sure that this cannot happen (assuming the system is

DISPATCH_LEVEL is a mean of achieving this.

Max


You are currently subscribed to ntfsd as: xxxxx@alfasp.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


Kind regards, Dejan M. CEO Alfa Co. www.alfasp.com
E-mail: xxxxx@alfasp.com
ICQ#: 56570367
Alfa File Monitor - File monitoring system for Win32 developers.
Alfa File Protector - File protection and hiding system for Win32 developers.


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

I think you have misunderstood Ralph’s question; at least, I would be
very careful to separate the two cases here:

  1. avoid another thread being scheduled before the call completes: raise
    to dispatch
  2. … the correct “never” answer from Tony as to whether the thread
    context can change *within* the thread (remember, there is no magic)

As Dejan notes, #1 is loaded with interesting issues. Not a lot of the
system can be called while at dispatch level.


This posting is provided “AS IS” with no warranties, and confers no
rights. You assume all risk for your use

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Monday, December 24, 2001 1:19 PM
To: File Systems Developers
Subject: [ntfsd] Re: Newbie question: Changing thread contexts

I just want to make sure that this cannot happen (assuming the system
is

DISPATCH_LEVEL is a mean of achieving this.

Max


You are currently subscribed to ntfsd as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Dear Tony:

Well, that answers it for KeWaitForSingleObject. I assume the same case
happens when I return from, say, IoCallDriver.

I want to ask the same question about which context I’m in when the
completion routine gets called.

Ralph Shnelvar

P.S.

And to all the good people on this list … Merry Christmas and/or Happy
Holidays and/or Happy Computing.

May all the bugs that plague you be fixed in the coming year.

On Mon, 24 Dec 2001 16:02:05 -0500, you wrote:

Ralph,

This is both a very simple question to answer (“never”) and very difficult -
because the very way the OS works is by changing thread context when a
thread calls KeWaitForSingleObject - but it changes to a DIFFERENT thread
context, so from the perspective of the thread that made the initial call
everything else that happens is “transparent”.

Think about it this way - a thread calls KeWaitForSingleObject. When the
call returns, it returns *for that thread* because something has occurred
that has satisfied the call to KeWaitForSingleObject (a timeout, the
signaling of the event, an APC is delivered or the thread is alerted.) What
the OS has done in the meantime doesn’t have any bearing on that thread.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: Ralph Shnelvar [mailto:xxxxx@dos32.com]
Sent: Monday, December 24, 2001 12:28 PM
To: File Systems Developers
Subject: [ntfsd] Newbie question: Changing thread contexts

Under what conditions can the thread context change?

I am concerned that when I call some routine, e.g. IoCallDriver or
KeWaitForSingleObject, and that when I return I come back in a different
context.

I just want to make sure that this cannot happen (assuming the system is
functioning correctly).

Ralph Shnelvar


You are currently subscribed to ntfsd as: xxxxx@osr.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: xxxxx@dos32.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

IoCallDriver() will always return in the same thread contect that it was
called in. A completion routine can be called in an arbitrary thread
context.

If you want to synchronize a call to IoCallDriver()m you can do the
following:

// Generic completion routine to synchronize a request.
static NTSTATUS SyncComplete(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PVOID Context)
{
// Check for a Context pointer.
if (ARGUMENT_PRESENT(Context))
// Signal the IRP originator that this operation is complete.
KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE);
// Halt completing the IRP and return.
return STATUS_MORE_PROCESSING_REQUIRED;
UNREFERENCED_PARAMETER(DeviceObject);
UNREFERENCED_PARAMETER(Irp);
}

// Calls the lower driver and wait for the operation to be completed by
the lower driver
static NTSTATUS CallDriverSync(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
NTSTATUS ntStatus;
KEVENT event;
// Initialize the completion event.
KeInitializeEvent(&event, NotificationEvent, FALSE);
// Set completion routine for this request.
IoSetCompletionRoutine(Irp, SyncComplete, &event, TRUE, TRUE, TRUE);
// Call the target driver.
if ((ntStatus = IoCallDriver(DeviceObject, Irp)) == STATUS_PENDING)
{
// Wait for the request to complete.
KeWaitForSingleObject(&event, Executive, KernelMode, TRUE,
NULL);
// Update the local status.
ntStatus = Irp->IoStatus.Status;
}
return ntStatus;
}

Jamey
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Ralph Shnelvar
Sent: Tuesday, December 25, 2001 6:26 AM
To: File Systems Developers
Subject: [ntfsd] RE: Newbie question: Changing thread contexts

Dear Tony:

Well, that answers it for KeWaitForSingleObject. I assume the same case
happens when I return from, say, IoCallDriver.

I want to ask the same question about which context I’m in when the
completion routine gets called.

Ralph Shnelvar

P.S.

And to all the good people on this list … Merry Christmas and/or Happy
Holidays and/or Happy Computing.

May all the bugs that plague you be fixed in the coming year.

On Mon, 24 Dec 2001 16:02:05 -0500, you wrote:

Ralph,

This is both a very simple question to answer (“never”) and very
difficult - because the very way the OS works is by changing thread
context when a thread calls KeWaitForSingleObject - but it changes to a

DIFFERENT thread context, so from the perspective of the thread that
made the initial call everything else that happens is “transparent”.

Think about it this way - a thread calls KeWaitForSingleObject. When
the call returns, it returns *for that thread* because something has
occurred that has satisfied the call to KeWaitForSingleObject (a
timeout, the signaling of the event, an APC is delivered or the thread
is alerted.) What the OS has done in the meantime doesn’t have any
bearing on that thread.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: Ralph Shnelvar [mailto:xxxxx@dos32.com]
Sent: Monday, December 24, 2001 12:28 PM
To: File Systems Developers
Subject: [ntfsd] Newbie question: Changing thread contexts

Under what conditions can the thread context change?

I am concerned that when I call some routine, e.g. IoCallDriver or
KeWaitForSingleObject, and that when I return I come back in a
different context.

I just want to make sure that this cannot happen (assuming the system
is functioning correctly).

Ralph Shnelvar


You are currently subscribed to ntfsd as: xxxxx@osr.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: xxxxx@dos32.com
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: xxxxx@storagecraft.com To
unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com