3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-15 08:29:31 +01:00
ergo/irc/utils/config.go

34 lines
936 B
Go
Raw Normal View History

2022-05-03 17:06:57 +02:00
// Copyright (c) 2022 Shivaram Lingamneni
// released under the MIT license
package utils
import (
"sync/atomic"
"unsafe"
)
/*
This can be used to implement the following pattern:
2022-05-04 05:27:24 +02:00
1. Prepare a config object (this can be arbitrarily expensive)
2. Take a pointer to the config object and use Set() to install it
3. Use Get() to access the config from any goroutine
4. To update the config, call Set() again with a new prepared config object
5. As long as any individual config object is not modified (by any goroutine)
after it is installed with Set(), this is free of data races, and Get()
2022-05-03 17:06:57 +02:00
is extremely cheap (on amd64 it compiles down to plain MOV instructions).
*/
2022-05-04 05:27:24 +02:00
type ConfigStore[Config any] struct {
2022-05-03 17:06:57 +02:00
ptr unsafe.Pointer
}
2022-05-04 05:27:24 +02:00
func (c *ConfigStore[Config]) Get() *Config {
return (*Config)(atomic.LoadPointer(&c.ptr))
2022-05-03 17:06:57 +02:00
}
2022-05-04 05:27:24 +02:00
func (c *ConfigStore[Config]) Set(ptr *Config) {
2022-05-03 17:06:57 +02:00
atomic.StorePointer(&c.ptr, unsafe.Pointer(ptr))
}