Implementing Shared/Exclusive Locks

Example 4-4 is a revision of Example 4-3 that uses an sx lock instead of a mutex.

Note

To save space, the functions race_ioctl, race_new, race_find, and race_destroy aren’t listed here, as they haven’t been changed.

Example 4-4. race_sx.c

#include <sys/param.h>
  #include <sys/module.h>
  #include <sys/kernel.h>
  #include <sys/systm.h>

  #include <sys/conf.h>
  #include <sys/uio.h>
  #include <sys/malloc.h>
  #include <sys/ioccom.h>
  #include <sys/queue.h>
  #include <sys/lock.h>
 #include <sys/sx.h> #include "race_ioctl.h" MALLOC_DEFINE(M_RACE, "race", "race object"); struct race_softc { LIST_ENTRY(race_softc) list; int unit; }; ...

Get FreeBSD Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.