This chapter opens the extension part of the book. The eight core chapters followed the main line “foundations → spinning → sleeping → lock-free and specialized locks”. This one goes back to a historical side branch we deliberately set aside: semaphore. It is one of the oldest sleeping synchronization primitives in Linux, older and plainer than the mutex of Chapter 5. We treat it on its own for two reasons. First, to see exactly what mutex added on top of it. Second, to finally settle a misunderstanding that has run through the earlier chapters: the “sem” in the name rwsem has no implementation lineage with this chapter’s semaphore. rwsem belongs to the mutex family (owner pointer, optimistic spinning, handoff), while semaphore is a separate branch whose structure does not even have an owner.
The whole implementation of semaphore lives in a single source file, short enough to read in one sitting. The sources for this chapter are kernel/locking/semaphore.c and include/linux/semaphore.h.
Note: the main line is x86-64 with a non-
PREEMPT_RTconfiguration.semaphoredoes not change form underPREEMPT_RT; its behavior is the same in every configuration.