use atomic.Load to ensure atomicity

This commit is contained in:
David Zhao
2021-01-28 23:40:30 -08:00
parent fe83baa662
commit dd8a1a5055
3 changed files with 28 additions and 13 deletions
+20 -6
View File
@@ -29,7 +29,6 @@ func initLogger(config zap.Config, level string) {
func InitProduction(logLevel string) { func InitProduction(logLevel string) {
initLogger(zap.NewProductionConfig(), logLevel) initLogger(zap.NewProductionConfig(), logLevel)
} }
func InitDevelopment(logLevel string) { func InitDevelopment(logLevel string) {
@@ -37,21 +36,36 @@ func InitDevelopment(logLevel string) {
} }
func Debugw(msg string, keysAndValues ...interface{}) { func Debugw(msg string, keysAndValues ...interface{}) {
getLogger().Debugw(msg, keysAndValues...) if logger == nil {
return
}
logger.Debugw(msg, keysAndValues...)
} }
func Infow(msg string, keysAndValues ...interface{}) { func Infow(msg string, keysAndValues ...interface{}) {
getLogger().Infow(msg, keysAndValues...) if logger == nil {
return
}
logger.Infow(msg, keysAndValues...)
} }
func Warnw(msg string, keysAndValues ...interface{}) { func Warnw(msg string, keysAndValues ...interface{}) {
getLogger().Warnw(msg, keysAndValues...) if logger == nil {
return
}
logger.Warnw(msg, keysAndValues...)
} }
func Errorw(msg string, keysAndValues ...interface{}) { func Errorw(msg string, keysAndValues ...interface{}) {
getLogger().Errorw(msg, keysAndValues...) if logger == nil {
return
}
logger.Errorw(msg, keysAndValues...)
} }
func Desugar() *zap.Logger { func Desugar() *zap.Logger {
return getLogger().Desugar() if logger == nil {
getLogger()
}
return logger.Desugar()
} }
+1
View File
@@ -382,6 +382,7 @@ func (p *ParticipantImpl) SendJoinResponse(roomInfo *livekit.Room, otherParticip
} }
func (p *ParticipantImpl) SendParticipantUpdate(participants []*livekit.ParticipantInfo) error { func (p *ParticipantImpl) SendParticipantUpdate(participants []*livekit.ParticipantInfo) error {
return p.responseSink.WriteMessage(&livekit.SignalResponse{ return p.responseSink.WriteMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Update{ Message: &livekit.SignalResponse_Update{
Update: &livekit.ParticipantUpdate{ Update: &livekit.ParticipantUpdate{
+7 -7
View File
@@ -10,25 +10,25 @@ var (
) )
type AtomicFlag struct { type AtomicFlag struct {
val uint32 val int32
} }
// set flag to value if existing flag is different, otherwise return // set flag to value if existing flag is different, otherwise return
func (b *AtomicFlag) TrySet(bVal bool) bool { func (b *AtomicFlag) TrySet(bVal bool) bool {
var v uint32 var v int32
if bVal { if bVal {
v = 1 v = 1
} }
old := b.val prev := atomic.SwapInt32(&b.val, v)
// value is the same, nochanges // already set. unsuccessful
if old == v { if prev == v {
return false return false
} }
return atomic.CompareAndSwapUint32(&b.val, old, v) return true
} }
func (b *AtomicFlag) Get() bool { func (b *AtomicFlag) Get() bool {
return b.val == 1 return atomic.LoadInt32(&b.val) == 1
} }
// a channel that ignores writes when it closes instead of panic // a channel that ignores writes when it closes instead of panic