How to sleep in driver thread?

My Windows driver will create one thread. I want to sleep the thread several micro second but the thread must not busy -wait , it must give up the CPU in this several micro second.

Which function can I use?

thanks

xxxxx@gmail.com wrote:

My Windows driver will create one thread. I want to sleep the thread several micro second but the thread must not busy -wait , it must give up the CPU in this several micro second.

Which function can I use?

  1. KeDelayExecutionThread
  2. KeWaitOnSingleObject on an EVENT object never signalled, with timeout

Sandor LUKACS

> My Windows driver will create one thread. I want to sleep the thread

several micro second but the thread must not busy -wait , it must give up
the CPU in this several micro second.

There is no way to make your thread sleep ONLY several microseconds. The
shortest sleep time will normally be like 10 milliseconds (which can be
improved to 1 millisecond if you change the timer period). Even though the
api’s for sleeping specify the time in system time units of 100 nanoseconds,
the actual clock resolution is like 10 milliseconds.

Since you say you don’t want to busy wait, does that mean you basically
want:

while (TRUE) {
Do something with hardware
Sleep a few microseconds
}

Also note that while your thread is running, DPC’s and ISR’s will be
interrupting your thread and taking away the cpu. A common guideline for
DPC’s to run in less than 100 microseconds, although some will sometimes run
rather longer (potentially milliseconds). There also might be multiple DPC’s
queued, so your thread will be interrupted for the time period of multiple
DPC’s.

So even if you had the ability to sleep a few microseconds, actual execution
might look like:

Touch your hardware
Sleep a few microseconds
ISR/DPC’s control the cpu for 300 microseconds
Touch your hardware
ISR/DPC’s control the cpu for 150 microseconds

Jan

Sandor LUKACS wrote:

xxxxx@gmail.com wrote:
> My Windows driver will create one thread. I want to sleep the thread
> several micro second but the thread must not busy -wait , it must
> give up the CPU in this several micro second.
> Which function can I use?
>

  1. KeDelayExecutionThread
  2. KeWaitOnSingleObject on an EVENT object never signalled, with timeout

…neither of which will actually give microsecond resolution.

There is simply no way to do what you ask. If you give up the CPU, you
have to be willing to live with millisecond resolution, not
microsecond. The timer interrupt doesn’t fire often enough to do that.
Are you waiting for some action in the hardware?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> I want to sleep the thread several micro second but the thread must not busy

As guys explained to you already, your objective is simply infeasible under Windows NT - if you yield the CPU, you have to wait until your thread gets rescheduled. Asssuming that this is the highest-priority thread in the system, i.e. it gets rescheduled straight away, the *minimum* period that must elapse that before your thread can resume execution is still one clock tick. If your thread has to fight for the CPU time with other threads, the delay may be much, much longer.

Furthermore, even while your thread runs, unless current IRQL is at the highest possible level or interrupts are disabled on the given CPU, you still cannot be sure that any two consequitive instructions get executed right one after another - if interrupt occurs, interrupt handler (and if your current IRQL< DPC level, DPC routine that interrupt handler queues) will get executed in between these two instructions, and all this will happen thransparently to your code.

If you want to be sure that your code waits exactly for N microseconds, you have to consider writing
it for some other platform - this task is simply infeasible under Windows NT, as well as under any other general-purpose OS. The only OS where your objective can be met is some RTOS that is written and optimized for some certain (and fairly limited) set of tasks - GPOSes are just unsuitable for this purpose…

Anton Bassov

Tim Roberts wrote:

Sandor LUKACS wrote:

> xxxxx@gmail.com wrote:
>
>> My Windows driver will create one thread. I want to sleep the thread
>> several micro second but the thread must not busy -wait , it must
>> give up the CPU in this several micro second.
>> Which function can I use?
>>
>>
> 1) KeDelayExecutionThread
> 2) KeWaitOnSingleObject on an EVENT object never signalled, with timeout
>

…neither of which will actually give microsecond resolution.

You are right. I didn’t take into consideration the microsecond vs.
millisecond part :wink:

There is simply no way to do what you ask. If you give up the CPU, you
have to be willing to live with millisecond resolution, not
microsecond. The timer interrupt doesn’t fire often enough to do that.
Are you waiting for some action in the hardware?