News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Yawn

Started by oex, April 14, 2010, 02:19:01 AM

Previous topic - Next topic

oex

What does the 'Sleep' function actually do?


Edit: I know what the result is but what does it do on an OS level to stop execution....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

here is my understanding of it
besides "waiting" the specified time-period, it relinquishes the remainder of the current time-slice
this allows some other process/thread(s) to execute
when execution returns to the calling thread, it will be at the beginning of a new time-slice
it can be used to prevent lock-up

oex

So it efectively runs SetPriorityClass IDLE_PRIORITY_CLASS or somesuch? Only it considers System Idle Process as a running process....

And btw if you dont have power saving I take it System Idle Process is just a loop?
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote from: oex on April 14, 2010, 12:38:34 PM
So it efectively runs SetPriorityClass IDLE_PRIORITY_CLASS or somesuch? Only it considers System Idle Process as a running process....

And btw if you dont have power saving I take it System Idle Process is just a loop?

It will be usefull if you create a new thread, it will save the CPU usage on the loop.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

qWord

I think that the thread is removed from task schedulers thread/task chain until the requested time has past - so it is simply not execute.
FPU in a trice: SmplMath
It's that simple!

MichaelW

I think for anything greater than a Sleep(0) the system effectively suspends the calling thread.
eschew obfuscation

oex

Interesting ty all.... This answers my underlying question that the Sleep function is probably the lowest level I should go without interfering with windows management although I'll have to dig into the last 2 posts a little further to be sure....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Slugsnack

You can try looking into ZwDelayExecution() in ntdll.dll

qWord

Quote from: Slugsnack on April 14, 2010, 03:23:20 PM
You can try looking into ZwDelayExecution() in ntdll.dll
using an kernel debugger he canĀ  :bg
FPU in a trice: SmplMath
It's that simple!

donkey

This is how I see the function...

The system first creates an activation context for a waitable timer (all global objects require an actctx) then that timer object sets up a spinlock that is outside of the calling process in kernel mode. The spinlock simply monitors the clock and is set to the signalled state when the timer expires. In the mean time the thread is suspended by removing its entry from the context switch list (it is Zombified), it is reinserted after the spinlock exits and the temporary ActCtx is released, this allows its time slice to be used by other processes. The advantage to making the timer a global object and running it independent of the calling process is obvious, it allows for the complete suspension of the calling thread, something that is not possible if you replace it with your own code, this is true of all waitable objects. You can mimic the behavior of Sleep by setting up a waitable timer and calling WaitForSingleObject, though the execution path is slightly different the effect is the same.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable