Signal a thread?

I have a DLL that creates a thread which does Overlapped DeviceIOcontrol to
my NDIS Miniport driver. My driver does not pend IRPs but waits till the
respective event occurs and completes them. Now my application that loaded
the DLL signals the event that the thread is waiting on. But my thread does
not fall through and still waits for the IRP completion from my driver. How
do I make my thread respond to the app.

Thanks
Daniel


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

Daniel,

To allow DeviceIoControl to return before the operation is complete, pend
the IRPs and complete them asyncronously rather than waiting. Waiting in
your driver blocks the thread that your call to DeviceIoControl is running
on.

-Tim

Timothy A. Johns — xxxxx@driverdev.com
Driver Development Corporation — 800.841.0092
Bring Up Your Hardware — Fast. www.driverdev.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Daniel Pradeep
Sent: Thursday, May 24, 2001 10:50 AM
To: NT Developers Interest List
Subject: [ntdev] Signal a thread?

I have a DLL that creates a thread which does Overlapped
DeviceIOcontrol to
my NDIS Miniport driver. My driver does not pend IRPs but waits till the
respective event occurs and completes them. Now my application that loaded
the DLL signals the event that the thread is waiting on. But my
thread does
not fall through and still waits for the IRP completion from my
driver. How
do I make my thread respond to the app.

Thanks
Daniel


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


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

> I have a DLL that creates a thread which does Overlapped DeviceIOcontrol
to

my NDIS Miniport driver. My driver does not pend IRPs but waits till the
respective event occurs and completes them. Now my application that loaded
the DLL signals the event that the thread is waiting on. But my thread
does
not fall through and still waits for the IRP completion from my driver.
How
do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This seems to be the
easiest way.

Max


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

I rewrote my driver to PEnd the IRPs. But still this does not solve the
problem in XP. I have some queries on the working of thread signalling???

I call DeviceIO with overlapped. I then call getOverlapped…And wait for
the event to complete. In driver I pend the IRP, register a cancel routine
and complete it when I get an event. Now the problem I have is, what happens
if the driver does not get the event to complete the IRP. The thread is
still waiting on Getoverlapped and my app has already called its cleanup
routine…I am puzzled that my app cannot signal the thread it
created…Is this the normal Behaviour or am I missing something

Thanks
Daniel

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Thursday, May 24, 2001 5:43 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

I have a DLL that creates a thread which does Overlapped DeviceIOcontrol
to
my NDIS Miniport driver. My driver does not pend IRPs but waits till the
respective event occurs and completes them. Now my application that loaded
the DLL signals the event that the thread is waiting on. But my thread
does
not fall through and still waits for the IRP completion from my driver.
How
do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This seems to be the
easiest way.

Max


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


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

I would suggest that you use a timer for timeout in the driver and then
cancel the IRP. That would solve your problem.

–Sam

At 06:52 PM 5/24/2001 -0700, you wrote:

I rewrote my driver to PEnd the IRPs. But still this does not solve the
problem in XP. I have some queries on the working of thread signalling???

I call DeviceIO with overlapped. I then call getOverlapped…And wait for
the event to complete. In driver I pend the IRP, register a cancel routine
and complete it when I get an event. Now the problem I have is, what happens
if the driver does not get the event to complete the IRP. The thread is
still waiting on Getoverlapped and my app has already called its cleanup
routine…I am puzzled that my app cannot signal the thread it
created…Is this the normal Behaviour or am I missing something

Thanks
Daniel

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Thursday, May 24, 2001 5:43 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

> I have a DLL that creates a thread which does Overlapped DeviceIOcontrol
to
> my NDIS Miniport driver. My driver does not pend IRPs but waits till the
> respective event occurs and completes them. Now my application that loaded
> the DLL signals the event that the thread is waiting on. But my thread
does
> not fall through and still waits for the IRP completion from my driver.
How
> do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This seems to be the
easiest way.

Max


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


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


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

Daniel,

In addition to the event handle(s) that you created for the overlapped
structure(s), you can also create an additional event to signal when your
application is cleaning up.

The application can WaitForMultipleObjects() or MsgWaitForMultipleObjects()
on the event handle(s) that you created in the overlapped structure(s) as
well as the additional event. When one (or several) of the event(s) are
signalled, you can check to see if it was your ‘cleanup’ event. If not,
then call GetOverlappedResult() with bWait == FALSE to retrieve the results
from that particular event/overlapped call that was signalled.

-Tim

Timothy A. Johns — xxxxx@driverdev.com
Driver Development Corporation — 800.841.0092
Bring Up Your Hardware — Fast. www.driverdev.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Daniel Pradeep
Sent: Thursday, May 24, 2001 6:52 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

I rewrote my driver to PEnd the IRPs. But still this does not solve the
problem in XP. I have some queries on the working of thread signalling???

I call DeviceIO with overlapped. I then call getOverlapped…And wait for
the event to complete. In driver I pend the IRP, register a cancel routine
and complete it when I get an event. Now the problem I have is,
what happens
if the driver does not get the event to complete the IRP. The thread is
still waiting on Getoverlapped and my app has already called its cleanup
routine…I am puzzled that my app cannot signal the thread it
created…Is this the normal Behaviour or am I missing something

Thanks
Daniel

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Thursday, May 24, 2001 5:43 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

> I have a DLL that creates a thread which does Overlapped DeviceIOcontrol
to
> my NDIS Miniport driver. My driver does not pend IRPs but waits till the
> respective event occurs and completes them. Now my application
that loaded
> the DLL signals the event that the thread is waiting on. But my thread
does
> not fall through and still waits for the IRP completion from my driver.
How
> do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This
seems to be the
easiest way.

Max


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


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


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

IRP cancellation is a very tricky issue filled with race condition
possibilities. Walter Oney outlines a nice way of dealing with timeouts
and cancellations in his book “Programming the Microsoft Windows Driver
Model” on pages 214-216.

Regards,
Youssef

-----Original Message-----
From: Sameer Kohli [mailto:xxxxx@austin.rr.com]
Sent: Thursday, May 24, 2001 8:12 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

I would suggest that you use a timer for timeout in the driver and then

cancel the IRP. That would solve your problem.

–Sam

At 06:52 PM 5/24/2001 -0700, you wrote:

I rewrote my driver to PEnd the IRPs. But still this does not solve the
problem in XP. I have some queries on the working of thread
signalling???

I call DeviceIO with overlapped. I then call getOverlapped…And wait
for
the event to complete. In driver I pend the IRP, register a cancel
routine
and complete it when I get an event. Now the problem I have is, what
happens
if the driver does not get the event to complete the IRP. The thread is
still waiting on Getoverlapped and my app has already called its
cleanup
routine…I am puzzled that my app cannot signal the thread it
created…Is this the normal Behaviour or am I missing something

Thanks
Daniel

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Thursday, May 24, 2001 5:43 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

> I have a DLL that creates a thread which does Overlapped
DeviceIOcontrol
to
> my NDIS Miniport driver. My driver does not pend IRPs but waits till
the
> respective event occurs and completes them. Now my application that
loaded
> the DLL signals the event that the thread is waiting on. But my
thread
does
> not fall through and still waits for the IRP completion from my
driver.
How
> do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This seems to
be the
easiest way.

Max


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


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


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


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

Tried that too…Does not work. This thread’s main functionality is to wait
for notifications from the driver whenever any app in the system sends the
driver a Multicast_IOCTL. So timeout is ruled out. What surprises me is that
my thread does not respond to any other event it is waiting on until the IRP
is completed…

This is what I do…
hEvent[2] , m_ThreadState is global
Main thread:

hEvent[0] = CreateEvent(
NULL, // SD
TRUE, // reset type
FALSE, // initial
state
NULL ); // object
name
hEvent[1] = CreateEvent( NULL, // SD
TRUE, // reset type
FALSE, // initial
state
NULL );
_beginthread(threadfn,0,NULL);

At Cleanup:
m_ThreadState = 1;
SetEvent(hEvent[1]);

threadfn()
{
while(!m_ThreadState){

DeviceIoControl (hFile,
IOCTL_… ,
NULL,0,
buf,buf_size ,
&outbuf_len, &async_over);
if(WaitForMultipleObjects(2,
hEvent,
FALSE,// handle to object
INFINITE // time-out interval
) == WAIT_FAILED)
}
if(m_ThreadState)
{
return;
}
ResetEvent(hEvent[0]);

}

In my driver:
irpList is global;

DISpatch routine…

IoMarkIrpPending(pIrp);
IoSetCancelRoutine(pIrp,CancelRoutine);
InsertTailList(&irpList, &pIrp->Tail.Overlay.ListEntry);
ntStatus = pIrp->IoStatus.Status = STATUS_PENDING;

If(ntStatus != STATUS_PENDING)
IoCompleteRequest( pIrp, IO_NO_INCREMENT );

return ntStatus;

Completion routine…

while (!IsListEmpty(&irpList))
{
pIrp = CONTAINING_RECORD(irpList.Flink, IRP,
Tail.Overlay.ListEntry);
if (IoSetCancelRoutine(pIrp, NULL))
{
//
// It isn’t being cancelled, and can’t be cancelled henceforth.
//
RemoveEntryList(irpList.Flink);

pIrp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
}

Am I mising something or is there any other way to go about this…
Thanks
DAniel

-----Original Message-----
From: Timothy A. Johns [mailto:xxxxx@driverdev.com]
Sent: Thursday, May 24, 2001 9:03 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

Daniel,

In addition to the event handle(s) that you created for the overlapped
structure(s), you can also create an additional event to signal when your
application is cleaning up.

The application can WaitForMultipleObjects() or MsgWaitForMultipleObjects()
on the event handle(s) that you created in the overlapped structure(s) as
well as the additional event. When one (or several) of the event(s) are
signalled, you can check to see if it was your ‘cleanup’ event. If not,
then call GetOverlappedResult() with bWait == FALSE to retrieve the results
from that particular event/overlapped call that was signalled.

-Tim

Timothy A. Johns — xxxxx@driverdev.com
Driver Development Corporation — 800.841.0092
Bring Up Your Hardware — Fast. www.driverdev.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Daniel Pradeep
Sent: Thursday, May 24, 2001 6:52 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

I rewrote my driver to PEnd the IRPs. But still this does not solve the
problem in XP. I have some queries on the working of thread signalling???

I call DeviceIO with overlapped. I then call getOverlapped…And wait for
the event to complete. In driver I pend the IRP, register a cancel routine
and complete it when I get an event. Now the problem I have is,
what happens
if the driver does not get the event to complete the IRP. The thread is
still waiting on Getoverlapped and my app has already called its cleanup
routine…I am puzzled that my app cannot signal the thread it
created…Is this the normal Behaviour or am I missing something

Thanks
Daniel

-----Original Message-----
From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
Sent: Thursday, May 24, 2001 5:43 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

> I have a DLL that creates a thread which does Overlapped DeviceIOcontrol
to
> my NDIS Miniport driver. My driver does not pend IRPs but waits till the
> respective event occurs and completes them. Now my application
that loaded
> the DLL signals the event that the thread is waiting on. But my thread
does
> not fall through and still waits for the IRP completion from my driver.
How
> do I make my thread respond to the app.

Rewrite the driver to pend the IRPs instead of waiting. This
seems to be the
easiest way.

Max


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


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


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


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

The user code looks OK, just beware of using autoreset events if you later
call GetOverlappedResult(bWait == TRUE), or if you only handle one
completion at a time and more than one event is signalled. Are you calling
CreateFile(FILE_FLAG_OVERLAPPED)?

In the driver code you probably shouldn’t set the cancel routine until after
you’ve inserted the IRP in the queue and set it’s status, but this does not
likely have any effect on what you’re seeing.

I believe you mentioned this is an IRP you’re sending to an NDIS driver of
some type. Is the user code blocked in the call to DeviceIoControl(), or
the call to WaitForMultipleObjects()?

-Tim

Timothy A. Johns — xxxxx@driverdev.com
Driver Development Corporation — 800.841.0092
Bring Up Your Hardware — Fast. www.driverdev.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Daniel Pradeep
Sent: Tuesday, May 29, 2001 3:39 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

Tried that too…Does not work. This thread’s main functionality
is to wait
for notifications from the driver whenever any app in the system sends the
driver a Multicast_IOCTL. So timeout is ruled out. What surprises
me is that
my thread does not respond to any other event it is waiting on
until the IRP
is completed…

This is what I do…
hEvent[2] , m_ThreadState is global
Main thread:

hEvent[0] = CreateEvent(
NULL, // SD
TRUE, // reset type
FALSE, // initial
state
NULL ); // object
name
hEvent[1] = CreateEvent( NULL, // SD
TRUE, // reset type
FALSE, // initial
state
NULL );
_beginthread(threadfn,0,NULL);

At Cleanup:
m_ThreadState = 1;
SetEvent(hEvent[1]);

threadfn()
{
while(!m_ThreadState){

DeviceIoControl (hFile,
IOCTL_… ,
NULL,0,
buf,buf_size ,
&outbuf_len, &async_over);
if(WaitForMultipleObjects(2,
hEvent,
FALSE,// handle to object
INFINITE // time-out interval
) == WAIT_FAILED)
}
if(m_ThreadState)
{
return;
}
ResetEvent(hEvent[0]);

}

In my driver:
irpList is global;

DISpatch routine…

IoMarkIrpPending(pIrp);
IoSetCancelRoutine(pIrp,CancelRoutine);
InsertTailList(&irpList, &pIrp->Tail.Overlay.ListEntry);
ntStatus = pIrp->IoStatus.Status = STATUS_PENDING;

If(ntStatus != STATUS_PENDING)
IoCompleteRequest( pIrp, IO_NO_INCREMENT );

return ntStatus;

Completion routine…

while (!IsListEmpty(&irpList))
{
pIrp = CONTAINING_RECORD(irpList.Flink, IRP,
Tail.Overlay.ListEntry);
if (IoSetCancelRoutine(pIrp, NULL))
{
//
// It isn’t being cancelled, and can’t be cancelled
henceforth.
//
RemoveEntryList(irpList.Flink);

pIrp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
}

Am I mising something or is there any other way to go about this…
Thanks
DAniel

-----Original Message-----
From: Timothy A. Johns [mailto:xxxxx@driverdev.com]
Sent: Thursday, May 24, 2001 9:03 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Signal a thread?

Daniel,

In addition to the event handle(s) that you created for the overlapped
structure(s), you can also create an additional event to signal when your
application is cleaning up.

The application can WaitForMultipleObjects() or
MsgWaitForMultipleObjects()
on the event handle(s) that you created in the overlapped structure(s) as
well as the additional event. When one (or several) of the event(s) are
signalled, you can check to see if it was your ‘cleanup’ event. If not,
then call GetOverlappedResult() with bWait == FALSE to retrieve
the results
from that particular event/overlapped call that was signalled.

-Tim

Timothy A. Johns — xxxxx@driverdev.com
Driver Development Corporation — 800.841.0092
Bring Up Your Hardware — Fast. www.driverdev.com

> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com]On Behalf Of Daniel Pradeep
> Sent: Thursday, May 24, 2001 6:52 PM
> To: NT Developers Interest List
> Subject: [ntdev] Re: Signal a thread?
>
>
> I rewrote my driver to PEnd the IRPs. But still this does not solve the
> problem in XP. I have some queries on the working of thread
signalling???
>
> I call DeviceIO with overlapped. I then call
getOverlapped…And wait for
> the event to complete. In driver I pend the IRP, register a
cancel routine
> and complete it when I get an event. Now the problem I have is,
> what happens
> if the driver does not get the event to complete the IRP. The thread is
> still waiting on Getoverlapped and my app has already called its cleanup
> routine…I am puzzled that my app cannot signal the thread it
> created…Is this the normal Behaviour or am I missing something
>
>
> Thanks
> Daniel
>
> -----Original Message-----
> From: Maxim S. Shatskih [mailto:xxxxx@storagecraft.com]
> Sent: Thursday, May 24, 2001 5:43 PM
> To: NT Developers Interest List
> Subject: [ntdev] Re: Signal a thread?
>
>
> > I have a DLL that creates a thread which does Overlapped
DeviceIOcontrol
> to
> > my NDIS Miniport driver. My driver does not pend IRPs but
waits till the
> > respective event occurs and completes them. Now my application
> that loaded
> > the DLL signals the event that the thread is waiting on. But
my thread
> does
> > not fall through and still waits for the IRP completion from
my driver.
> How
> > do I make my thread respond to the app.
>
> Rewrite the driver to pend the IRPs instead of waiting. This
> seems to be the
> easiest way.
>
> Max
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@broadlogic.com
> To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com
>
> —
> You are currently subscribed to ntdev as: xxxxx@driverdev.com
> To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com
>


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


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


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