C H A P T E R  7

Synchronization and Threading

As we have seen throughout this book, the role of a driver is to make the functionality that is provided by a hardware device available to the operating system and to user applications. This means that the code inside a driver may be called from any number of running applications at any time, depending on when an application wishes to request the services of the hardware device. In handling these requests, the driver runs in the thread context of the application that made the control call. In addition to these requests, the hardware itself can require servicing and may generate interrupts at arbitrary times that the driver must respond to. The end result for the driver developer is that driver code runs in a complex multithreaded environment, even without the driver creating any additional threads of its own.

The computer hardware on which a driver will execute will likely have multiple CPU cores. So, in addition to the driver code being preempted by an interrupt from the device or a request from a thread in another application, it's possible for your driver to be running on multiple cores simultaneously. This applies even to the interrupt service routine for your driver, which can run in parallel to the non-interrupt code of your driver on another CPU core.

As is the case with multithreaded application code, it's important that a driver provides synchronized access to its internal structures and any data that could potentially be read or written from multiple threads. How a driver provides the arbitration between multiple threads that are attempting to access its hardware is dependent on the type of device. Some hardware can only be accessed by one client at a time. For example, a serial port device will grant exclusive access to one user process at a time; the driver will make sure that an attempt by another process to open the serial port will be rejected. On the other hand, a disk device can expect to receive requests from multiple processes and, since the hardware itself can handle only one request at a time, it is the responsibility of the driver to queue the incoming requests and issue them to the disk device in a serial manner.

The I/O Kit provides several different mechanisms that a driver can use to implement a scheme that provides arbitrated access to its hardware while ensuring that the driver's internal structures remain valid in a multithreaded environment. This chapter assumes that you have a basic understanding of code synchronization and have previously written multithreaded application code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.249.220