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

use new aligned atomic types everywhere #1989

Merged
merged 1 commit into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions irc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type Session struct {
sasl saslStatus
passStatus serverPassStatus

batchCounter uint32
batchCounter atomic.Uint32

quitMessage string

Expand Down Expand Up @@ -262,7 +262,7 @@ func (session *Session) HasHistoryCaps() bool {
// or nesting) on an individual session connection need to be unique.
// this allows ~4 billion such batches which should be fine.
func (session *Session) generateBatchID() string {
id := atomic.AddUint32(&session.batchCounter, 1)
id := session.batchCounter.Add(1)
return strconv.FormatInt(int64(id), 32)
}

Expand Down
5 changes: 2 additions & 3 deletions irc/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package irc
import (
"fmt"
"net"
"sync/atomic"
"time"

"github.com/ergochat/ergo/irc/caps"
Expand Down Expand Up @@ -36,11 +35,11 @@ func (server *Server) Languages() (lm *languages.Manager) {
}

func (server *Server) Defcon() uint32 {
return atomic.LoadUint32(&server.defcon)
return server.defcon.Load()
}

func (server *Server) SetDefcon(defcon uint32) {
atomic.StoreUint32(&server.defcon, defcon)
server.defcon.Store(defcon)
}

func (client *Client) Sessions() (sessions []*Session) {
Expand Down
8 changes: 4 additions & 4 deletions irc/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Manager struct {
loggers []singleLogger
stdoutWriteLock sync.Mutex // use one lock for both stdout and stderr
fileWriteLock sync.Mutex
loggingRawIO uint32
loggingRawIO atomic.Uint32
}

// LoggingConfig represents the configuration of a single logger.
Expand Down Expand Up @@ -107,7 +107,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error {
}

logger.loggers = nil
atomic.StoreUint32(&logger.loggingRawIO, 0)
logger.loggingRawIO.Store(0)

// for safety, this deep-copies all mutable data in `config`
// XXX let's keep it that way
Expand Down Expand Up @@ -138,7 +138,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error {
ioEnabled := typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"]))
// raw I/O is only logged at level debug;
if ioEnabled && logConfig.Level == LogDebug {
atomic.StoreUint32(&logger.loggingRawIO, 1)
logger.loggingRawIO.Store(1)
}
if sLogger.MethodFile.Enabled {
file, err := os.OpenFile(sLogger.MethodFile.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
Expand All @@ -157,7 +157,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error {

// IsLoggingRawIO returns true if raw user input and output is being logged.
func (logger *Manager) IsLoggingRawIO() bool {
return atomic.LoadUint32(&logger.loggingRawIO) == 1
return logger.loggingRawIO.Load() == 1
}

// Log logs the given message with the given details.
Expand Down
18 changes: 9 additions & 9 deletions irc/mysql/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ const (
type e struct{}

type MySQL struct {
timeout *int64
trackAccountMessages uint32
db *sql.DB
logger *logger.Manager
db *sql.DB
logger *logger.Manager

insertHistory *sql.Stmt
insertSequence *sql.Stmt
Expand All @@ -60,22 +58,24 @@ type MySQL struct {
config Config

wakeForgetter chan e

timeout atomic.Uint64
trackAccountMessages atomic.Uint32
}

func (mysql *MySQL) Initialize(logger *logger.Manager, config Config) {
mysql.timeout = new(int64)
mysql.logger = logger
mysql.wakeForgetter = make(chan e, 1)
mysql.SetConfig(config)
}

func (mysql *MySQL) SetConfig(config Config) {
atomic.StoreInt64(mysql.timeout, int64(config.Timeout))
mysql.timeout.Store(uint64(config.Timeout))
var trackAccountMessages uint32
if config.TrackAccountMessages {
trackAccountMessages = 1
}
atomic.StoreUint32(&mysql.trackAccountMessages, trackAccountMessages)
mysql.trackAccountMessages.Store(trackAccountMessages)
mysql.stateMutex.Lock()
mysql.config = config
mysql.stateMutex.Unlock()
Expand Down Expand Up @@ -555,11 +555,11 @@ func (mysql *MySQL) prepareStatements() (err error) {
}

func (mysql *MySQL) getTimeout() time.Duration {
return time.Duration(atomic.LoadInt64(mysql.timeout))
return time.Duration(mysql.timeout.Load())
}

func (mysql *MySQL) isTrackingAccountMessages() bool {
return atomic.LoadUint32(&mysql.trackAccountMessages) != 0
return mysql.trackAccountMessages.Load() != 0
}

func (mysql *MySQL) logError(context string, err error) (quit bool) {
Expand Down
4 changes: 2 additions & 2 deletions irc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Server struct {
stats Stats
semaphores ServerSemaphores
flock flock.Flocker
defcon uint32
defcon atomic.Uint32
}

// NewServer returns a new Oragono server.
Expand All @@ -103,8 +103,8 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
logger: logger,
rehashSignal: make(chan os.Signal, 1),
exitSignals: make(chan os.Signal, len(utils.ServerExitSignals)),
defcon: 5,
}
server.defcon.Store(5)

server.accepts.Initialize()
server.clients.Initialize()
Expand Down
13 changes: 6 additions & 7 deletions irc/usermaskset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/ergochat/ergo/irc/utils"
)
Expand All @@ -27,8 +26,8 @@ type UserMaskSet struct {
sync.RWMutex
serialCacheUpdateMutex sync.Mutex
masks map[string]MaskInfo
regexp unsafe.Pointer
muteRegexp unsafe.Pointer
regexp atomic.Pointer[regexp.Regexp]
muteRegexp atomic.Pointer[regexp.Regexp]
}

func NewUserMaskSet() *UserMaskSet {
Expand Down Expand Up @@ -110,7 +109,7 @@ func (set *UserMaskSet) Masks() (result map[string]MaskInfo) {

// Match matches the given n!u@h against the standard (non-ext) bans.
func (set *UserMaskSet) Match(userhost string) bool {
regexp := (*regexp.Regexp)(atomic.LoadPointer(&set.regexp))
regexp := set.regexp.Load()

if regexp == nil {
return false
Expand All @@ -129,7 +128,7 @@ func (set *UserMaskSet) MatchMute(userhost string) bool {
}

func (set *UserMaskSet) MuteRegexp() *regexp.Regexp {
return (*regexp.Regexp)(atomic.LoadPointer(&set.muteRegexp))
return set.muteRegexp.Load()
}

func (set *UserMaskSet) Length() int {
Expand Down Expand Up @@ -162,6 +161,6 @@ func (set *UserMaskSet) setRegexp() {
re := compileMasks(maskExprs)
muteRe := compileMasks(muteExprs)

atomic.StorePointer(&set.regexp, unsafe.Pointer(re))
atomic.StorePointer(&set.muteRegexp, unsafe.Pointer(muteRe))
set.regexp.Store(re)
set.muteRegexp.Store(muteRe)
}