|
@@ -1,33 +0,0 @@
|
1
|
|
-// Copyright (c) 2022 Shivaram Lingamneni
|
2
|
|
-// released under the MIT license
|
3
|
|
-
|
4
|
|
-package utils
|
5
|
|
-
|
6
|
|
-import (
|
7
|
|
- "sync/atomic"
|
8
|
|
- "unsafe"
|
9
|
|
-)
|
10
|
|
-
|
11
|
|
-/*
|
12
|
|
-This can be used to implement the following pattern:
|
13
|
|
-
|
14
|
|
-1. Prepare a config object (this can be arbitrarily expensive)
|
15
|
|
-2. Take a pointer to the config object and use Set() to install it
|
16
|
|
-3. Use Get() to access the config from any goroutine
|
17
|
|
-4. To update the config, call Set() again with a new prepared config object
|
18
|
|
-5. As long as any individual config object is not modified (by any goroutine)
|
19
|
|
- after it is installed with Set(), this is free of data races, and Get()
|
20
|
|
- is extremely cheap (on amd64 it compiles down to plain MOV instructions).
|
21
|
|
-*/
|
22
|
|
-
|
23
|
|
-type ConfigStore[Config any] struct {
|
24
|
|
- ptr unsafe.Pointer
|
25
|
|
-}
|
26
|
|
-
|
27
|
|
-func (c *ConfigStore[Config]) Get() *Config {
|
28
|
|
- return (*Config)(atomic.LoadPointer(&c.ptr))
|
29
|
|
-}
|
30
|
|
-
|
31
|
|
-func (c *ConfigStore[Config]) Set(ptr *Config) {
|
32
|
|
- atomic.StorePointer(&c.ptr, unsafe.Pointer(ptr))
|
33
|
|
-}
|