Specifications

11-1
11
Chapter 11Multithreading a Device Driver
11
11
11
This chapter describes the methods for protecting a device driver in a multiprocessor
system. It provides an introduction to the multithreaded, preemptive kernel and protection
mechanisms. It shows the procedures for using spin locks, sleep locks, and synchroniza-
tion variables to protect critical sections of code.
The Multithreaded, Preemptive Kernel and Device Drivers 11
The kernel used on the Series 6000 system is multithreaded and preemptive. Multithread-
ing the kernel permits more than one thread of execution in the kernel at one time; in other
words, it provides the ability for two or more lightweight processes (LWPs) running on sep-
arate processors to execute within the same section of kernel code simultaneously. Making
the kernel preemptive makes it possible for an LWP that is executing in kernel mode to be
forced to relinquish the CPU; this permits quick response to high-priority LWPs.
Having a multithreaded preemptive kernel makes it necessary to use special protection
mechanisms to prevent data structures from being corrupted; for example, a common mul-
tithreading problem is protection of a linked list. When one LWP is inserting an item in or
deleting an item from a linked list, other LWPs must be prevented from modifying or fol-
lowing the links of the list. If another LWP modifies the list at the same time, the list can be
corrupted. If another LWP attempts to follow the linked list, it can miss a link in the list, or
if it picks up a link that is not yet initialized, it can be scanning memory that is not really a
part of the list.
Device drivers are a part of the kernel and must be multithreaded in the same manner as
any other operating system feature; that is, critical sections and shared data structures must
be protected when corruption is possible. The protection mechanisms that are available for
use in developing device drivers are spin locks, sleep locks, and event synchronization
primitives. Use of each of these types of mechanisms is explained in the “Using the Syn-
chronization Primitives”section, page 11-4.
Protecting a Device Driver 11
A device driver must have its own internal protection, or it must be marked so that it can
execute only on a single processor. When completely multithreaded, multiple LWPs can be
active within the driver at any given time. To protect a driver internally, you must be thor-
oughly familiar with the driver and its operation; you must carefully examine the follow-
ing to determine the extent of protection needed and the type of mechanism to be used:
Sections of code around which the interrupt priority level (IPL) is raised