jlikeme / log4go

Automatically exported from code.google.com/p/log4go
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Fix for a data race found with Go 1.1's new race detector #15

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
$ hg log -p -r 49
changeset:   49:c177575d6b20
tag:         tip
user:        Fredrik Ehnbom <fredrik@ehnbom.nu>
date:        Wed Apr 17 10:45:22 2013 +0200
summary:     Fix for a data race found with Go 1.1's new race detector 
http://tip.golang.org/doc/articles/race_detector.html

diff -r c3294304d93f -r c177575d6b20 pattlog.go
--- a/pattlog.go    Sat Feb 25 12:58:10 2012 -0800
+++ b/pattlog.go    Wed Apr 17 10:45:22 2013 +0200
@@ -3,9 +3,10 @@
 package log4go

 import (
+   "bytes"
    "fmt"
-   "bytes"
    "io"
+   "sync"
 )

 const (
@@ -21,6 +22,7 @@
 }

 var formatCache = &formatCacheType{}
+var formatMutex sync.Mutex

 // Known format codes:
 // %T - Time (15:04:05 MST)
@@ -43,7 +45,9 @@
    out := bytes.NewBuffer(make([]byte, 0, 64))
    secs := rec.Created.UnixNano() / 1e9

+   formatMutex.Lock()
    cache := *formatCache
+   formatMutex.Unlock()
    if cache.LastUpdateSeconds != secs {
        month, day, year := rec.Created.Month(), rec.Created.Day(), rec.Created.Year()
        hour, minute, second := rec.Created.Hour(), rec.Created.Minute(), rec.Created.Second()
@@ -55,8 +59,10 @@
            longTime:          fmt.Sprintf("%02d:%02d:%02d %s", hour, minute, second, zone),
            longDate:          fmt.Sprintf("%04d/%02d/%02d", year, month, day),
        }
+       formatMutex.Lock()
        cache = *updated
        formatCache = updated
+       formatMutex.Unlock()
    }

    // Split the string into pieces by % signs

Original issue reported on code.google.com by quarns...@gmail.com on 17 Apr 2013 at 8:47