fix: thread-safe in getWriter of logx (#2319)

master
Kevin Wan 2 years ago committed by GitHub
parent 8c72136631
commit dfeef5e497
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -358,19 +358,9 @@ func errorTextSync(msg string) {
} }
func getWriter() Writer { func getWriter() Writer {
var w Writer w := writer.Load()
writer.lock.RLock()
w = writer.writer
writer.lock.RUnlock()
if w == nil { if w == nil {
writer.lock.Lock() w = writer.StoreIfNil(newConsoleWriter())
if writer.writer == nil {
writer.writer = newConsoleWriter()
}
w = writer.writer
writer.lock.Unlock()
} }
return w return w

@ -67,6 +67,17 @@ func (w *atomicWriter) Store(v Writer) {
w.writer = v w.writer = v
} }
func (w *atomicWriter) StoreIfNil(v Writer) Writer {
w.lock.Lock()
defer w.lock.Unlock()
if w.writer == nil {
w.writer = v
}
return w.writer
}
func (w *atomicWriter) Swap(v Writer) Writer { func (w *atomicWriter) Swap(v Writer) Writer {
w.lock.Lock() w.lock.Lock()
defer w.lock.Unlock() defer w.lock.Unlock()

Loading…
Cancel
Save