KeSetTimer and DPC ?

Hi,

I want to execute one of my function after an inerval with the arguments
dependent upon the situation. I thought KeSetTimer will be fine. The
sequence can be following:

KeInitializeDpc (mydpc,…)
KeSetTimer (…, mydpc)

My doubts are:

  1. How can I pass the arguments whie setting the KeSetTimer ?
  2. We can have only one DPC of a particular type in the system queue. What
    if I want multiple DPCs (with different arguments passed) of same type.

Amit Manocha

1.The “DeferredContext” provided in KeInitializeDpc is passed to the DPC. This must be a pointer to something from non-pagedpool (
i.e. Device Extension , or something private ) . Store your parameters there before calling KeSettimer.
2. Initialize different DPC’s with different contexts. Those DPC’s can be a wrapper call to a common routine for example.

----- Original Message -----
From: “Amit Manocha”
Newsgroups: ntdev
To: “NT Developers Interest List”
Sent: Monday, June 02, 2003 9:37 AM
Subject: [ntdev] KeSetTimer and DPC ?

> Hi,
>
> I want to execute one of my function after an inerval with the arguments
> dependent upon the situation. I thought KeSetTimer will be fine. The
> sequence can be following:
>
> KeInitializeDpc (mydpc,…)
> KeSetTimer (…, mydpc)
>
> My doubts are:
> 1. How can I pass the arguments whie setting the KeSetTimer ?
> 2. We can have only one DPC of a particular type in the system queue. What
> if I want multiple DPCs (with different arguments passed) of same type.
>
> Amit Manocha
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

Do you want to say, we will have a global variable for DeferredContext ? In
that case, I can’t execute the same code at multiple places? e.g if I want
this operation. for each packet received or file opened, it will cause
unwanted results ?

Amit Manocha
----- Original Message -----
From: “Christiaan Ghijselinck”
To: “NT Developers Interest List”
Sent: Monday, June 02, 2003 4:55 PM
Subject: [ntdev] Re: KeSetTimer and DPC ?

>
> 1.The “DeferredContext” provided in KeInitializeDpc is passed to the DPC.
This must be a pointer to something from non-pagedpool (
> i.e. Device Extension , or something private ) . Store your parameters
there before calling KeSettimer.
> 2. Initialize different DPC’s with different contexts. Those DPC’s can be
a wrapper call to a common routine for example.
>
>
> ----- Original Message -----
> From: “Amit Manocha”
> Newsgroups: ntdev
> To: “NT Developers Interest List”
> Sent: Monday, June 02, 2003 9:37 AM
> Subject: [ntdev] KeSetTimer and DPC ?
>
>
> > Hi,
> >
> > I want to execute one of my function after an inerval with the arguments
> > dependent upon the situation. I thought KeSetTimer will be fine. The
> > sequence can be following:
> >
> > KeInitializeDpc (mydpc,…)
> > KeSetTimer (…, mydpc)
> >
> > My doubts are:
> > 1. How can I pass the arguments whie setting the KeSetTimer ?
> > 2. We can have only one DPC of a particular type in the system queue.
What
> > if I want multiple DPCs (with different arguments passed) of same type.
> >
> > Amit Manocha
> >
> >
> >
> > —
> > You are currently subscribed to ntdev as:
xxxxx@compaqnet.be
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@mynetsec.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

No, not one, but several, eventually dynamically allocated instead of “global”.

----- Original Message -----
From: “Amit Manocha”
To: “NT Developers Interest List”
Sent: Monday, June 02, 2003 11:48 AM
Subject: [ntdev] Re: KeSetTimer and DPC ?

> Do you want to say, we will have a global variable for DeferredContext ? In
> that case, I can’t execute the same code at multiple places? e.g if I want
> this operation. for each packet received or file opened, it will cause
> unwanted results ?
>
> Amit Manocha
> ----- Original Message -----
> From: “Christiaan Ghijselinck”
> To: “NT Developers Interest List”
> Sent: Monday, June 02, 2003 4:55 PM
> Subject: [ntdev] Re: KeSetTimer and DPC ?
>
>
> >
> > 1.The “DeferredContext” provided in KeInitializeDpc is passed to the DPC.
> This must be a pointer to something from non-pagedpool (
> > i.e. Device Extension , or something private ) . Store your parameters
> there before calling KeSettimer.
> > 2. Initialize different DPC’s with different contexts. Those DPC’s can be
> a wrapper call to a common routine for example.
> >
> >
> > ----- Original Message -----
> > From: “Amit Manocha”
> > Newsgroups: ntdev
> > To: “NT Developers Interest List”
> > Sent: Monday, June 02, 2003 9:37 AM
> > Subject: [ntdev] KeSetTimer and DPC ?
> >
> >
> > > Hi,
> > >
> > > I want to execute one of my function after an inerval with the arguments
> > > dependent upon the situation. I thought KeSetTimer will be fine. The
> > > sequence can be following:
> > >
> > > KeInitializeDpc (mydpc,…)
> > > KeSetTimer (…, mydpc)
> > >
> > > My doubts are:
> > > 1. How can I pass the arguments whie setting the KeSetTimer ?
> > > 2. We can have only one DPC of a particular type in the system queue.
> What
> > > if I want multiple DPCs (with different arguments passed) of same type.
> > >
> > > Amit Manocha
> > >
> > >
> > >
> > > —
> > > You are currently subscribed to ntdev as:
> xxxxx@compaqnet.be
> > > To unsubscribe send a blank email to xxxxx@lists.osr.com
> > >
> >
> >
> >
> > —
> > You are currently subscribed to ntdev as: xxxxx@mynetsec.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

> 2. We can have only one DPC of a particular type in the system
queue. What

if I want multiple DPCs (with different arguments passed) of same
type.

Create several KDPC objects with the same routine.

Max

Probably, the best thing you can do is setting up a pool of pairs of initialized DPC-Objects and corresponding contexts. You can
grab a free pair out of that pool if required, and release it afterwards back into that pool. Note that if it would be necessary to
let grow that pool dynamically, this must be done at passive level ( due to KeInitializeDpc ).

----- Original Message -----
From: “Christiaan Ghijselinck”
To: “NT Developers Interest List”
Sent: Monday, June 02, 2003 12:16 PM
Subject: [ntdev] Re: KeSetTimer and DPC ?

>
> No, not one, but several, eventually dynamically allocated instead of “global”.
>
>
> ----- Original Message -----
> From: “Amit Manocha”
> To: “NT Developers Interest List”
> Sent: Monday, June 02, 2003 11:48 AM
> Subject: [ntdev] Re: KeSetTimer and DPC ?
>
>
> > Do you want to say, we will have a global variable for DeferredContext ? In
> > that case, I can’t execute the same code at multiple places? e.g if I want
> > this operation. for each packet received or file opened, it will cause
> > unwanted results ?
> >
> > Amit Manocha
> > ----- Original Message -----
> > From: “Christiaan Ghijselinck”
> > To: “NT Developers Interest List”
> > Sent: Monday, June 02, 2003 4:55 PM
> > Subject: [ntdev] Re: KeSetTimer and DPC ?
> >
> >
> > >
> > > 1.The “DeferredContext” provided in KeInitializeDpc is passed to the DPC.
> > This must be a pointer to something from non-pagedpool (
> > > i.e. Device Extension , or something private ) . Store your parameters
> > there before calling KeSettimer.
> > > 2. Initialize different DPC’s with different contexts. Those DPC’s can be
> > a wrapper call to a common routine for example.
> > >
> > >
> > > ----- Original Message -----
> > > From: “Amit Manocha”
> > > Newsgroups: ntdev
> > > To: “NT Developers Interest List”
> > > Sent: Monday, June 02, 2003 9:37 AM
> > > Subject: [ntdev] KeSetTimer and DPC ?
> > >
> > >
> > > > Hi,
> > > >
> > > > I want to execute one of my function after an inerval with the arguments
> > > > dependent upon the situation. I thought KeSetTimer will be fine. The
> > > > sequence can be following:
> > > >
> > > > KeInitializeDpc (mydpc,…)
> > > > KeSetTimer (…, mydpc)
> > > >
> > > > My doubts are:
> > > > 1. How can I pass the arguments whie setting the KeSetTimer ?
> > > > 2. We can have only one DPC of a particular type in the system queue.
> > What
> > > > if I want multiple DPCs (with different arguments passed) of same type.
> > > >
> > > > Amit Manocha
> > > >
> > > >
> > > >
> > > > —
> > > > You are currently subscribed to ntdev as:
> > xxxxx@compaqnet.be
> > > > To unsubscribe send a blank email to xxxxx@lists.osr.com
> > > >
> > >
> > >
> > >
> > > —
> > > You are currently subscribed to ntdev as: xxxxx@mynetsec.com
> > > To unsubscribe send a blank email to xxxxx@lists.osr.com
> > >
> >
> >
> >
> > —
> > You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

If i create DPCs dynamically, then where i can delete these objects?
Inside the DPC routine itself ? I think that will be dangerous because the
DPC routine is still in progress.
or somewhere else ?

Amit Manocha
“Maxim S. Shatskih” wrote in message
news:xxxxx@ntdev…
>
> > 2. We can have only one DPC of a particular type in the system
> queue. What
> > if I want multiple DPCs (with different arguments passed) of same
> type.
>
> Create several KDPC objects with the same routine.
>
> Max
>
>
>
>

DPC objects are not in use once the DPC routine is invoked, these are
seperate things.

Don Burn
Windows 2k/XP/2k3 Filesystem and Driver Consulting

“Amit Manocha” wrote in message
news:LYRIS-796-112248-2003.06.03-01.17.23–burn#xxxxx@lists.osr.com…
> If i create DPCs dynamically, then where i can delete these objects?
> Inside the DPC routine itself ? I think that will be dangerous because the
> DPC routine is still in progress.
> or somewhere else ?
>
> Amit Manocha
> “Maxim S. Shatskih” wrote in message
> news:xxxxx@ntdev…
> >
> > > 2. We can have only one DPC of a particular type in the system
> > queue. What
> > > if I want multiple DPCs (with different arguments passed) of same
> > type.
> >
> > Create several KDPC objects with the same routine.
> >
> > Max
> >

DPC Objects initialized with KeInitializeDPC are not be released ( there is no need to do a “KeUnInitializeDPC” ).

----- Original Message -----
From: “Amit Manocha”
Newsgroups: ntdev
To: “NT Developers Interest List”
Sent: Tuesday, June 03, 2003 6:57 AM
Subject: [ntdev] Re: KeSetTimer and DPC ?

> If i create DPCs dynamically, then where i can delete these objects?
> Inside the DPC routine itself ? I think that will be dangerous because the
> DPC routine is still in progress.
> or somewhere else ?
>
> Amit Manocha
> “Maxim S. Shatskih” wrote in message
> news:xxxxx@ntdev…
> >
> > > 2. We can have only one DPC of a particular type in the system
> > queue. What
> > > if I want multiple DPCs (with different arguments passed) of same
> > type.
> >
> > Create several KDPC objects with the same routine.
> >
> > Max
> >
> >
> >
> >
>
>
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

the DPC object is only in use while the DPC is in the queue. Once it’s
invoked, the kernel won’t touch the DPC object unless you queue it
again. So you’re fine freeing it in your DPC routine (as long as you
don’t queue that object again)

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Amit Manocha
Sent: Monday, June 02, 2003 9:58 PM
To: NT Developers Interest List
Subject: [ntdev] Re: KeSetTimer and DPC ?

If i create DPCs dynamically, then where i can delete these objects?
Inside the DPC routine itself ? I think that will be
dangerous because the DPC routine is still in progress.
or somewhere else ?

Amit Manocha
“Maxim S. Shatskih” wrote in message
> news:xxxxx@ntdev…
> >
> > > 2. We can have only one DPC of a particular type in the system
> > queue. What
> > > if I want multiple DPCs (with different arguments passed) of same
> > type.
> >
> > Create several KDPC objects with the same routine.
> >
> > Max
> >
> >
> >
> >
>
>
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@microsoft.com To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>
>

Following up your suggestions, I wrote the code.
The problem with this is - if i call more than one DPCs within the time this
DPC expires (using KeSetTimer) then it is executed only once.

// PART 1
/* structure for DPC context data (user defined data) */
typedef struct _DPCDATA
{
PKDPC dpc; // for freeing in mydpc
int n; // any other data
} DPCDATA, * PDPCDATA;

// PART 2
/* This is called whenever i want to execute a DPC */
PDPCDATA pdpcdata;
PKDPC dpc;
LARGE_INTEGER n;
// initialize n
n.LowPart = - 10 * 10000000;
n.HighPart = -1;
// allocate memory for dpc
dpc = (PKDPC) ExAllocatePool (NonPagedPool, sizeof (PKDPC));
// allocate memory for dpc context (user defined data)
pdpcdata = (PDPCDATA) ExAllocatePool (NonPagedPool, sizeof (PDPCDATA));
pdpcdata->dpc = dpc;
pdpcdata->n = 10;
// initialize dpc
KeInitializeDpc (dpc, mydpc, (PVOID) pdpcdata);
// call the dpc after 10 sec
KeSetTimer (&timer, n, dpc); // timer is intialized somewhere before this
point

// PART 3
/* DPC routine */
VOID mydpc (IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID
SystemArgument1, IN PVOID SystemArgument2)
{
PDPCDATA pdpcdata = (PDPCDATA) DeferredContext;
// free the memory
ExFreePool (pdpcdata->dpc);
DbgPrint (“mydpc is being called with %d”, pdpcdata->n);
ExFreePool (DeferredContext);
}

I restate my problem: if i execute code in Part 2 more than once within 10
seconds, Part 3 is executed ONLY ONCE ?
I know i am missing some conceptual point. Can any body figure it out ?

Amit Manocha

“Amit Manocha” wrote in message
news:xxxxx@ntdev…
>
> Hi,
>
> I want to execute one of my function after an inerval with the arguments
> dependent upon the situation. I thought KeSetTimer will be fine. The
> sequence can be following:
>
> KeInitializeDpc (mydpc,…)
> KeSetTimer (…, mydpc)
>
> My doubts are:
> 1. How can I pass the arguments whie setting the KeSetTimer ?
> 2. We can have only one DPC of a particular type in the system queue. What
> if I want multiple DPCs (with different arguments passed) of same type.
>
> Amit Manocha
>
>
>
>

As long as your DPC is queued ( i.e the timer runs ), the object is related to one particular DPC life. Queing it again, kills the
actual queued. This is documented. Why don’t you follow my suggestions ?

----- Original Message -----
From: “Amit Manocha”
Newsgroups: ntdev
To: “NT Developers Interest List”
Sent: Wednesday, June 04, 2003 5:26 AM
Subject: [ntdev] Re: KeSetTimer and DPC ?

> Following up your suggestions, I wrote the code.
> The problem with this is - if i call more than one DPCs within the time this
> DPC expires (using KeSetTimer) then it is executed only once.
>
> // PART 1
> /* structure for DPC context data (user defined data) /
> typedef struct _DPCDATA
> {
> PKDPC dpc; // for freeing in mydpc
> int n; // any other data
> } DPCDATA, * PDPCDATA;
>
> // PART 2
> /
This is called whenever i want to execute a DPC /
> PDPCDATA pdpcdata;
> PKDPC dpc;
> LARGE_INTEGER n;
> // initialize n
> n.LowPart = - 10 * 10000000;
> n.HighPart = -1;
> // allocate memory for dpc
> dpc = (PKDPC) ExAllocatePool (NonPagedPool, sizeof (PKDPC));
> // allocate memory for dpc context (user defined data)
> pdpcdata = (PDPCDATA) ExAllocatePool (NonPagedPool, sizeof (PDPCDATA));
> pdpcdata->dpc = dpc;
> pdpcdata->n = 10;
> // initialize dpc
> KeInitializeDpc (dpc, mydpc, (PVOID) pdpcdata);
> // call the dpc after 10 sec
> KeSetTimer (&timer, n, dpc); // timer is intialized somewhere before this
> point
>
> // PART 3
> /
DPC routine */
> VOID mydpc (IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID
> SystemArgument1, IN PVOID SystemArgument2)
> {
> PDPCDATA pdpcdata = (PDPCDATA) DeferredContext;
> // free the memory
> ExFreePool (pdpcdata->dpc);
> DbgPrint (“mydpc is being called with %d”, pdpcdata->n);
> ExFreePool (DeferredContext);
> }
>
> I restate my problem: if i execute code in Part 2 more than once within 10
> seconds, Part 3 is executed ONLY ONCE ?
> I know i am missing some conceptual point. Can any body figure it out ?
>
> Amit Manocha
>
> “Amit Manocha” wrote in message
> news:xxxxx@ntdev…
> >
> > Hi,
> >
> > I want to execute one of my function after an inerval with the arguments
> > dependent upon the situation. I thought KeSetTimer will be fine. The
> > sequence can be following:
> >
> > KeInitializeDpc (mydpc,…)
> > KeSetTimer (…, mydpc)
> >
> > My doubts are:
> > 1. How can I pass the arguments whie setting the KeSetTimer ?
> > 2. We can have only one DPC of a particular type in the system queue. What
> > if I want multiple DPCs (with different arguments passed) of same type.
> >
> > Amit Manocha
> >
> >
> >
> >
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

I really don’t understand the difference between the two. You are suggesting
have a pool of DPCs (say 5 different DPC variables) and here I am allocating
and initializing the DPC and the context on-demand. The problem is even if I
am creating new DPC, why it is killing the other DPC queue ? Even if DPC
queue is system-wide but both the entries belong to different DPCs.
Where I am wrong in my understaning ?
Another reason why i am not having pool of DPC and corresponding contexts is
that - the number of contexts is potentially very large.

Amit Manocha

“Christiaan Ghijselinck” wrote in
message news:xxxxx@ntdev…
>
>
>
> As long as your DPC is queued ( i.e the timer runs ), the object is
related to one particular DPC life. Queing it again, kills the
> actual queued. This is documented. Why don’t you follow my suggestions ?
>
>
> ----- Original Message -----
> From: “Amit Manocha”
> Newsgroups: ntdev
> To: “NT Developers Interest List”
> Sent: Wednesday, June 04, 2003 5:26 AM
> Subject: [ntdev] Re: KeSetTimer and DPC ?
>
>
> > Following up your suggestions, I wrote the code.
> > The problem with this is - if i call more than one DPCs within the time
this
> > DPC expires (using KeSetTimer) then it is executed only once.
> >
> > // PART 1
> > /* structure for DPC context data (user defined data) /
> > typedef struct _DPCDATA
> > {
> > PKDPC dpc; // for freeing in mydpc
> > int n; // any other data
> > } DPCDATA, * PDPCDATA;
> >
> > // PART 2
> > /
This is called whenever i want to execute a DPC /
> > PDPCDATA pdpcdata;
> > PKDPC dpc;
> > LARGE_INTEGER n;
> > // initialize n
> > n.LowPart = - 10 * 10000000;
> > n.HighPart = -1;
> > // allocate memory for dpc
> > dpc = (PKDPC) ExAllocatePool (NonPagedPool, sizeof (PKDPC));
> > // allocate memory for dpc context (user defined data)
> > pdpcdata = (PDPCDATA) ExAllocatePool (NonPagedPool, sizeof (PDPCDATA));
> > pdpcdata->dpc = dpc;
> > pdpcdata->n = 10;
> > // initialize dpc
> > KeInitializeDpc (dpc, mydpc, (PVOID) pdpcdata);
> > // call the dpc after 10 sec
> > KeSetTimer (&timer, n, dpc); // timer is intialized somewhere before
this
> > point
> >
> > // PART 3
> > /
DPC routine */
> > VOID mydpc (IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID
> > SystemArgument1, IN PVOID SystemArgument2)
> > {
> > PDPCDATA pdpcdata = (PDPCDATA) DeferredContext;
> > // free the memory
> > ExFreePool (pdpcdata->dpc);
> > DbgPrint (“mydpc is being called with %d”, pdpcdata->n);
> > ExFreePool (DeferredContext);
> > }
> >
> > I restate my problem: if i execute code in Part 2 more than once within
10
> > seconds, Part 3 is executed ONLY ONCE ?
> > I know i am missing some conceptual point. Can any body figure it out ?
> >
> > Amit Manocha
> >
> > “Amit Manocha” wrote in message
> > news:xxxxx@ntdev…
> > >
> > > Hi,
> > >
> > > I want to execute one of my function after an inerval with the
arguments
> > > dependent upon the situation. I thought KeSetTimer will be fine. The
> > > sequence can be following:
> > >
> > > KeInitializeDpc (mydpc,…)
> > > KeSetTimer (…, mydpc)
> > >
> > > My doubts are:
> > > 1. How can I pass the arguments whie setting the KeSetTimer ?
> > > 2. We can have only one DPC of a particular type in the system queue.
What
> > > if I want multiple DPCs (with different arguments passed) of same
type.
> > >
> > > Amit Manocha
> > >
> > >
> > >
> > >
> >
> >
> >
> > —
> > You are currently subscribed to ntdev as:
xxxxx@compaqnet.be
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
>
>
>

> > > // call the dpc after 10 sec

> > KeSetTimer (&timer, n, dpc); // timer is intialized somewhere before

> > I restate my problem: if i execute code in Part 2 more than once
within
10
> > seconds, Part 3 is executed ONLY ONCE ?

What is &timer pointing to? Are you trying to reuse the timer that will be
triggering a DPC before the timer expires? I think you need an individual
timer for each pending DPC. You can only reuse the timers after the DPC
routine is triggered.

Loren

Thanks Loren. That was the problem. Now it works.
Amit Manocha

“Loren Wilton” wrote in message news:xxxxx@ntdev…
>
> > > > // call the dpc after 10 sec
> > > > KeSetTimer (&timer, n, dpc); // timer is intialized somewhere
before
>
> > > > I restate my problem: if i execute code in Part 2 more than once
> within
> > 10
> > > > seconds, Part 3 is executed ONLY ONCE ?
>
> What is &timer pointing to? Are you trying to reuse the timer that will
be
> triggering a DPC before the timer expires? I think you need an individual
> timer for each pending DPC. You can only reuse the timers after the DPC
> routine is triggered.
>
> Loren
>
>
>
>

> If i create DPCs dynamically, then where i can delete these objects?

Inside the DPC routine itself ?

Yes. Nothing dangerous, the KDPC is no more on the list.

Max

> I restate my problem: if i execute code in Part 2 more than once
within 10

seconds, Part 3 is executed ONLY ONCE ?
I know i am missing some conceptual point. Can any body figure it
out ?

Timer granularity is a point, which is based on a rate of timer IRQs.

Max