]> Cypherpunks repositories - gostls13.git/commitdiff
Use the mutex in exvar.Set since map access is not atomic.
authorDavid Symonds <dsymonds@golang.org>
Mon, 20 Apr 2009 07:42:08 +0000 (00:42 -0700)
committerDavid Symonds <dsymonds@golang.org>
Mon, 20 Apr 2009 07:42:08 +0000 (00:42 -0700)
Imagine your var has a value of zero. If you have a goroutine calling Set(5),
and another calling Increment(+1), then you only want one of these outcomes:
  - Set completes first, and then Increment occurs => 6
  - Increment completes first, and then Set occurs => 5

However, you could get a sequence:
  - read (for Increment) 0
  - set (for Set) 5
  - write (for Increment) 1
This results in a value of 1, which is undesirable.

Kudos to dnadasi for catching this.

R=r
APPROVED=r
DELTA=3  (3 added, 0 deleted, 0 changed)
OCL=27625
CL=27625

src/lib/exvar.go

index ccfd34acd968af6f7d4c0551984a16f4aefb6259..319a0977a2c2bd7eaf2fc6af51d219920918de00 100644 (file)
@@ -36,6 +36,9 @@ func Increment(name string, inc int) {
 
 // Set sets the var called name to value.
 func Set(name string, value int) {
+       mutex.Lock();
+       defer mutex.Unlock();
+
        intVars[name] = value
 }