Writing Device Drivers

Using Mutexes

Every section of the driver code that needs to read or write the shared data structure must do the following:

For example, to protect access to the busy flag in the state structure:

	...
 	mutex_enter(&xsp->mu);
 	xsp->busy = 0;
 	mutex_exit(&xsp->mu);
 	....

The scope of a mutex--the data it protects--is entirely up to the programmer. A mutex protects some particular data structure because the programmer chooses to do so and uses it accordingly. A mutex protects a data structure only if every code path that accesses the data structure does so while holding the mutex. For additional guidelines on using mutexes see Appendix G, Advanced Topics.