INFO: TLS Considerations for Device Drivers (298864)



The information in this article applies to:

  • Microsoft Windows CE Operating System, Versions 3.0
  • Microsoft Windows CE Operating System, Versions 2.12
  • Microsoft Windows CE Operating System, Versions 2.11
  • Microsoft Windows CE Operating System, Versions 2.0
  • Microsoft Windows CE Platform Builder 2.11
  • Microsoft Windows CE Platform Builder 2.12
  • Microsoft Windows CE Platform Builder 3.0

This article was previously published under Q298864

SUMMARY

A device driver implementation must take into account whether its thread of execution is owned by a calling process (application) or its own process. Those that are owned by a calling process must not make system calls that will potentially access thread local storage (TLS).

MORE INFORMATION

In Windows CE, an application thread can make a number of system calls that cause the thread to transition from the application's process space to the driver's process space. An example of this would be a display driver getting called as a result of an ExtEscape() or a number of graphics device interface (GDI) and window manager functions. The display driver runs in the GWES (Gwes.exe) process space.

Because TLS slots are reserved per process in a structure owned by the thread, the driver cannot use the TLS slots of an application's thread. Some system API's (such as gethostbyname()) will attempt to use these TLS slots and should not be called while on an application thread within a device driver. This applies to drivers loaded by GWES (such as display, keyboard, and touch panel) and all drivers loaded by Device Manager (Device.exe).

A driver can compare the results of GetOwnerProcess() and OpenProcess() to determine if the thread is owned by the current process or by an application.
HANDLE hOwnerProcess;
HANDLE hDriverProcess;
BOOL fIsApplicationThread;

hOwnerProcess = GetOwnerProcess();
hDriverProcess = OpenProcess( 0, FALSE, GetCurrentProcessID() );
fIsApplicationThread = ( hOwnerProcess != hDriverProcess );
				
If it is a requirement of the driver's architecture to use functions like gethostbyname() on behalf of the caller, the driver will have to create a secondary thread to make the call.

REFERENCES

See GetOwnerProcess() in the Platform Builder Documentation or the MSDN Online Library:

Modification Type:MinorLast Reviewed:8/18/2005
Keywords:kbinfo KB298864