Timing and Scheduling

The Windows CE kernel handles two kinds of timing measurements:

  • Keeping track of the current date/time for applications and for timestamps on files and network packets.
  • Timers that measure elapsed time and notify the kernel and applications when a certain period of time has passed.

To perform these tasks, the kernel interacts with three hardware clocks:

  • The real-time clock, which is separate from the CPU and all other chips and operates on its own power system on most chipsets. For more information, see Real-Time Clock.
  • The timestamp counter, typically a 64-bit register, is incremented at each clock signal. For example, on a 400-MHz microprocessor, this clock measures 2.5 nanosecond intervals.
  • The programmable interval timer notifies the kernel when some specified interval has passed.

Real-time Clock

The OAL implements three functions that the Windows CE kernel uses to communicate with the real-time clock: OEMGetRealTime, OEMSetRealTime, and OEMSetAlarmTime. For more information, see Implementing the Real-Time Clock and System Timer.

System Timer

Applications query the system timer by calling the Windows CE GetTickCount function. The Windows CE kernel then calls the OAL system-timer function, SC_GetTickCount, to retrieve the elapsed time, in milliseconds, since the system was started.

The kernel calls the OAL function InitClock to initialize the CPU clock and set the system tick interval frequency (the default is 1 ms). This function is called when the system boots for the first time and whenever a suspended device is resumed.

Windows CE also provides a prototype implementation of the system-timer interrupt service routine (ISR) for each sample platform. Each time the interrupt goes off, the ISR should reset the hardware and increment the variables CurMSec and DiffMSec by 1 ms.

The system timer should be initialized in the OEMInit routine with a call to InitClock, which is defined in the OAL.

Applications can also call the Windows Timer functions KillTimer, SetTimer, and SystemIdleTimerReset to communicate with the timer.

Timestamp Counters

Windows CE has two built-in high-resolution counter routines, QueryPerformanceCounter and QueryPerformanceFrequency, that interact with the timestamp counter. QueryPerformanceCounter returns a 64-bit number that represents timer counts. QueryPerformanceFrequency returns the number of high-performance ticks per second. It is used to convert counts to a time value.

If the target platform provides 64-bit high-resolution counters in hardware, the OEMInit routine is used to set the pQueryPerformanceCounter and pQueryPerformanceFrequency global variables.

On platforms that do not implement high-resolution counters, a timestamp counter can be implemented by using 32-bit hardware counters and with an interrupt that generates the rollover to 64-bits.

Thread Scheduling

As a pre-emptive multitasking operating system (OS), Windows CE bases pre-emption solely on the priority of the thread; that is, Windows CE schedules threads with a higher priority to run first. Threads at the same priority level run in a round-robin fashion with each thread receiving a quantum, or slice, of execution time.

Under Windows CE, an application can set the quantum on a thread-by-thread basis. This means that you can adapt the scheduler to the current needs of your application. To adjust the time quantum, Windows CE provides these two functions: CeGetThreadQuantum and CeSetThreadQuantum. CeGetThreadQuantum returns the quantum for a specified thread, while CeSetThreadQuantum is used to set the quantum for a thread. Altering the quantum for a specified thread does not affect threads with a higher priority. The OEM can set the default thread quantum for the system with the dwDefaultThreadQuantum OAL variable.

See Also

Migrating from an Open Source Platform to Windows CE | Time Reference

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.