Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Mar 31, 2015 · 0 comments
Open

Comments

@GoogleCodeExporter
Copy link

$ 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant