Timer in a driver?

Hello,
I have special error checking code in my driver, which must be executed every 500ms. How can I achieve this? Is there a possibility of creating Timer(-threads)?

Yes , read the "KeSetTimer " description in the WDK / DDK

Christiaan

----- Original Message -----
From:
To: “Windows System Software Devs Interest List”
Sent: Friday, September 01, 2006 8:25 AM
Subject: [ntdev] Timer in a driver?

> Hello,
> I have special error checking code in my driver, which must be executed every 500ms. How can I achieve this? Is there a
possibility of creating Timer(-threads)?
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

wrote in message news:xxxxx@ntdev…
> Hello,
> I have special error checking code in my driver, which must be executed every 500ms. How can I achieve this? Is there a
> possibility of creating Timer(-threads)?

No way. You can’t have exactly 500 ms intervals, with a “timer thread” or otherwise.
Make your code resilient to random delays.

Regards,
–PA

Hey,
thanks for your answers, but I don’t really know how to create a timer…

class CTimerStruct
{
public:
KDPC m_DPCObject;
KTIMER m_Timer;
};

VOID TimerProc(KDPC* pDPC,void* pTimerStruct,void*,void*)
{
// My code here…

//Reinit now?!
//KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,
// RtlConvertLongToLargeInteger(-500),
// &reinterpret_cast(pTimerStruct)->m_DPCObject);
}

//…
CTimerStruct* pTimerStruct=reinterpret_cast(pMasterDevice->DeviceExtension);
KeInitializeDpc(&pTimerStruct->m_DPCObject, TimerProc, NULL);
KeInitializeTimer(&pTimerStruct->m_Timer);
KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,
RtlConvertLongToLargeInteger(-500),//Timeinterval 500ms realtive to current system time
&reinterpret_cast(pTimerStruct)->m_DPCObject);
//…

I initalize my TimerStruct (in the DeviceExtension) just after the main device is created. Then after 500ms(?) TimerProc is called. Unfortunately 500ms after the first call of TimerProc TimerProc is not called again :frowning:

What’s wrong?!

Thanks

You need to look at KeInitializeTimerEx or reset the timer in your timer
callback.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
http://www.windrvr.com
Remove StopSpam from the email to reply

wrote in message news:xxxxx@ntdev…
> Hey,
> thanks for your answers, but I don’t really know how to create a timer…
>
> class CTimerStruct
> {
> public:
> KDPC m_DPCObject;
> KTIMER m_Timer;
> };
>
> VOID TimerProc(KDPC* pDPC,void* pTimerStruct,void*,void*)
> {
> // My code here…
>
> //Reinit now?!
> //KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,
> // RtlConvertLongToLargeInteger(-500),
> // &reinterpret_cast(pTimerStruct)->m_DPCObject);
> }
>
> //…
> CTimerStruct*
> pTimerStruct=reinterpret_cast(pMasterDevice->DeviceExtension);
> KeInitializeDpc(&pTimerStruct->m_DPCObject, TimerProc, NULL);
> KeInitializeTimer(&pTimerStruct->m_Timer);
> KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,
> RtlConvertLongToLargeInteger(-500),//Timeinterval 500ms realtive to
> current system time
> &reinterpret_cast(pTimerStruct)->m_DPCObject);
> //…
>
> I initalize my TimerStruct (in the DeviceExtension) just after the main
> device is created. Then after 500ms(?) TimerProc is called. Unfortunately
> 500ms after the first call of TimerProc TimerProc is not called again :frowning:
>
> What’s wrong?!
>
> Thanks
>

Saw my mistake… third argument of KeInitializeDpc must be pTimerStruct instead of NULL, and TimerProc must be

VOID TimerProc(KDPC* pDPC,void* pTimerStruct,void*,void*)
{
KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer, RtlConvertLongToLargeInteger(-500),&reinterpret_cast(pTimerStruct)->m_DPCObject);
}

Just one last questions :slight_smile:
What’s the format of the time interval? When I set -500, the TimerProc is called nearly 20-30 times a second, and not 2 times :</ctimerstruct>

Have you considered using KMDF for your driver? A lot of these types of
issues/castings go away.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@safe-mail.net
Sent: Saturday, September 02, 2006 8:31 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Timer in a driver?

Saw my mistake… third argument of KeInitializeDpc must be pTimerStruct
instead of NULL, and TimerProc must be

VOID TimerProc(KDPC* pDPC,void* pTimerStruct,void*,void*)
{
KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,
RtlConvertLongToLargeInteger(-500),&reinterpret_cast(pTim
erStruct)->m_DPCObject);
}

Just one last questions :slight_smile:
What’s the format of the time interval? When I set -500, the TimerProc
is called nearly 20-30 times a second, and not 2 times :\


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

>Have you considered using KMDF for your driver? A lot of these types of issues/castings go away.
Well, not really :\

Anyway, I also found that mistake. The timeinterval must be multiplied with 1000, so -500000 is the right time interval for 500ms :slight_smile:

Just one very last questions :slight_smile:
The driver is loaded by a usermode program after the system is booted. The device and the timer is created in the driver’s entry function.
Where can I delete the device and the timer? The driver cannot be unloaded by the user, and so I didn’t provide an Unload routine and code for deleteing the device and the timer. Is that OK?

If you have no unload routine, why do you want to delete the
device/driver? You can do it anywhere you want. The app can send an
IOCTL or you can decide to do it yourself in an arbitrary context.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@safe-mail.net
Sent: Sunday, September 03, 2006 7:34 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Timer in a driver?

Have you considered using KMDF for your driver? A lot of these types of
issues/castings go away.
Well, not really :\

Anyway, I also found that mistake. The timeinterval must be multiplied
with 1000, so -500000 is the right time interval for 500ms :slight_smile:

Just one very last questions :slight_smile:
The driver is loaded by a usermode program after the system is booted.
The device and the timer is created in the driver’s entry function.
Where can I delete the device and the timer? The driver cannot be
unloaded by the user, and so I didn’t provide an Unload routine and code
for deleteing the device and the timer. Is that OK?


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

No, the driver is loaded by the app, the app is closed but the driver stays in memory until shutdown. I thought It’s better to clean the resources anywhere (a function which is called before the pc is shut down), but I don’t know where :ß

If its a demand start driver why not just put the unload routine in and let
it shut down when it is closed? Not particularly difficult to do.

Loren

Timers are the issue. If the timer has an associated DPC there are
issues (on at least some Windows versions) with safely unloading it
(basically, timer DPC routines are on a private list, so when you cancel
and the DPC is already queued to execute it isn’t canceled and thus may
fire when the driver has been unloaded.)

I know timers are one of the items that make a driver unsafe to unload
(as do executive work queue items.) Many of these things have been
resolved or deprecated from more recent WDK builds.

Tony

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

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Sunday, September 03, 2006 3:41 PM
To: ntdev redirect
Subject: Re: RE:[ntdev] Timer in a driver?

If its a demand start driver why not just put the unload routine in and
let
it shut down when it is closed? Not particularly difficult to do.

Loren


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Tony Mason дµÀ:

(basically, timer DPC routines are on a private list, so when you cancel
and the DPC is already queued to execute it isn’t canceled and thus may
fire when the driver has been unloaded.)
Just be curiosity about it. Can’t KeFlushQueuedDpcs solve this issue?

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Sunday, September 03, 2006 3:41 PM
To: ntdev redirect
Subject: Re: RE:[ntdev] Timer in a driver?

If its a demand start driver why not just put the unload routine in and
let
it shut down when it is closed? Not particularly difficult to do.

Loren


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

The queue was private to the timer package, not managed by the standard kernel DPC queue mechanism which is what created the issue in the first place. It is quite possible that this problem, like most others that we currently know, is resolved in Vista.

Tony

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

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of hanzhu
Sent: Sunday, September 03, 2006 5:51 PM
To: ntdev redirect
Subject: Re: [ntdev] Timer in a driver?

Tony Mason $B
> (basically, timer DPC routines are on a private list, so when you cancel
> and the DPC is already queued to execute it isn’t canceled and thus may
> fire when the driver has been unloaded.)
Just be curiosity about it. Can’t KeFlushQueuedDpcs solve this issue?

>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
> Sent: Sunday, September 03, 2006 3:41 PM
> To: ntdev redirect
> Subject: Re: RE:[ntdev] Timer in a driver?
>
> If its a demand start driver why not just put the unload routine in and
> let
> it shut down when it is closed? Not particularly difficult to do.
>
> Loren
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Io’s timers are fine.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From:
To: “Windows System Software Devs Interest List”
Sent: Friday, September 01, 2006 10:25 AM
Subject: [ntdev] Timer in a driver?

> Hello,
> I have special error checking code in my driver, which must be executed
every 500ms. How can I achieve this? Is there a possibility of creating
Timer(-threads)?
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Tony, I’m a little confused since I’m rather sleepy this morning.:slight_smile:
Please help me clarify it. Thanks a lot.
the WDK docs on KeCancelTimer mentioned:
“Since for periodic timers KeCancelTimer always returns TRUE, drivers
must use a different technique to wait until the DPC has completed. Use
the KeFlushQueuedDpcs routine to block until the DPC executes.”
According to my understanding, KeFlushQueueDpcs returns only after the
started timer DPC callback completes, doesn’t it?


Best Regards,
hanzhu

Tony Mason дµÀ:

The queue was private to the timer package, not managed by the standard kernel DPC queue mechanism which is what created the issue in the first place. It is quite possible that this problem, like most others that we currently know, is resolved in Vista.

Tony

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

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of hanzhu
Sent: Sunday, September 03, 2006 5:51 PM
To: ntdev redirect
Subject: Re: [ntdev] Timer in a driver?

Tony Mason $B>
>> (basically, timer DPC routines are on a private list, so when you cancel
>> and the DPC is already queued to execute it isn’t canceled and thus may
>> fire when the driver has been unloaded.)
> Just be curiosity about it. Can’t KeFlushQueuedDpcs solve this issue?
>
>
>> -----Original Message-----
>> From: xxxxx@lists.osr.com
>> [mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
>> Sent: Sunday, September 03, 2006 3:41 PM
>> To: ntdev redirect
>> Subject: Re: RE:[ntdev] Timer in a driver?
>>
>> If its a demand start driver why not just put the unload routine in and
>> let
>> it shut down when it is closed? Not particularly difficult to do.
>>
>> Loren
>>
>>
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>>
>> —
>> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>>
>> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

> VOID TimerProc(KDPC* pDPC,void* pTimerStruct,void*,void*)

{
// My code here…

//Reinit now?!
//KeSetTimer(&reinterpret_cast(pTimerStruct)->m_Timer,

Correct, you must reinit now.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

> What’s the format of the time interval? When I set -500, the TimerProc is
called

Time interval is 64bit and is in 100ns units. The absolute times start from the
beginning of year 1601.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

> Where can I delete the device and the timer? The driver cannot be unloaded by

the user, and so I didn’t provide an Unload routine and code for deleteing the
device and the timer. Is that OK?

Yes.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

I believe this issue was first observed in Windows 2000. It is quite possible that it has been resolved in later versions. If the documentation states that it is, then I’d trust that. After all, I generally don’t worry about unload problems anyway.

Tony

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

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of hanzhu
Sent: Sunday, September 03, 2006 7:01 PM
To: ntdev redirect
Subject: Re: [ntdev] Timer in a driver?

Tony, I’m a little confused since I’m rather sleepy this morning.:slight_smile:
Please help me clarify it. Thanks a lot.
the WDK docs on KeCancelTimer mentioned:
“Since for periodic timers KeCancelTimer always returns TRUE, drivers
must use a different technique to wait until the DPC has completed. Use
the KeFlushQueuedDpcs routine to block until the DPC executes.”
According to my understanding, KeFlushQueueDpcs returns only after the
started timer DPC callback completes, doesn’t it?


Best Regards,
hanzhu

Tony Mason $B> The queue was private to the timer package, not managed by the standard kernel DPC queue mechanism which is what created the issue in the first place. It is quite possible that this problem, like most others that we currently know, is resolved in Vista.

>
> Tony
>
> Tony Mason
> Consulting Partner
> OSR Open Systems Resources, Inc.
> http://www.osr.com
>
>
> -----Original Message-----
> From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of hanzhu
> Sent: Sunday, September 03, 2006 5:51 PM
> To: ntdev redirect
> Subject: Re: [ntdev] Timer in a driver?
>
>
> Tony Mason $B>
>> (basically, timer DPC routines are on a private list, so when you cancel
>> and the DPC is already queued to execute it isn’t canceled and thus may
>> fire when the driver has been unloaded.)
> Just be curiosity about it. Can’t KeFlushQueuedDpcs solve this issue?
>
>
>> -----Original Message-----
>> From: xxxxx@lists.osr.com
>> [mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
>> Sent: Sunday, September 03, 2006 3:41 PM
>> To: ntdev redirect
>> Subject: Re: RE:[ntdev] Timer in a driver?
>>
>> If its a demand start driver why not just put the unload routine in and
>> let
>> it shut down when it is closed? Not particularly difficult to do.
>>
>> Loren
>>
>>
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>>
>> —
>> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>>
>> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer