From 242cf826be0ebd94a41b46ac5fa8c81025773b14 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Mon, 14 Jul 2014 19:02:52 +0200 Subject: [PATCH] final draft --- _drafts/Russ-Cox.markdown | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/_drafts/Russ-Cox.markdown b/_drafts/Russ-Cox.markdown index 74802ec..37c18cf 100644 --- a/_drafts/Russ-Cox.markdown +++ b/_drafts/Russ-Cox.markdown @@ -7,6 +7,8 @@ image: /images/brent.simmons.jpg Russ Cox works at Google on the Go programming language. He previously worked at Bell Labs where he contributed to Plan9. +

Who are you?

+

I’m a programmer. @@ -109,9 +111,7 @@ marked locked and must now decide what to do. The first, simplest approach, is to try again, and again, and again. Eventually thread A will release the lock (by marking the core unlocked), at which point thread B’s atomic operation will succeed. -This approach is called spinning, and a lock using this approach is called a spin lock. -Spinning only makes sense if the lock is never held for very long, -so that B’s spin loop only executes a small number of times. +This approach is called spinning, and a lock using this approach is called a spin lock.

A simple spin lock implementation looks like: @@ -144,6 +144,12 @@ It is 0 or 1 to indicate unlocked or locked. The atomic_cmp_and_set and atomic_set use special machine instructions to manipulate lk->bit atomically. +

+Spinning only makes sense if the lock is never held for very long, +so that B’s spin loop only executes a small number of times. +If the lock can be held for longer periods of time, spinning while it is held +wastes CPU and can interact badly with the operating system scheduler. +

The second, more general approach is to maintain a queue of threads interested in acquiring the lock. In this approach, when thread B finds the lock already held, @@ -307,4 +313,4 @@ What did they learn? Wilkes later recalled,

For more about this early history, see Brian Hayes’s “The Discovery of Debugging” and Martin Campbell-Kelly’s “The Airy Tape: An Early Chapter in the History of Debugging.” - + -- 2.43.0