1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-26 03:52:03 +02:00

Replace uses unsafe with atomic with the generic atomic.Pointer (#4226)

Line numbers debug symbols aside this code will produce the exact same machine code.
It removes a non needed use of unsafe and replace it with compile time type checked
generics.
Will help keep code cleaner in case any of this ever gets refactored in the future.
This commit is contained in:
Jorropo 2023-06-20 12:14:45 +02:00 committed by GitHub
parent e3f547fedd
commit cda2d6c7b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View File

@ -18,7 +18,6 @@ import (
"log"
"os"
"sync/atomic"
"unsafe"
)
var (
@ -42,7 +41,7 @@ type ErrorHandler interface {
}
type ErrDelegator struct {
delegate unsafe.Pointer
delegate atomic.Pointer[ErrorHandler]
}
func (d *ErrDelegator) Handle(err error) {
@ -50,12 +49,12 @@ func (d *ErrDelegator) Handle(err error) {
}
func (d *ErrDelegator) getDelegate() ErrorHandler {
return *(*ErrorHandler)(atomic.LoadPointer(&d.delegate))
return *d.delegate.Load()
}
// setDelegate sets the ErrorHandler delegate.
func (d *ErrDelegator) setDelegate(eh ErrorHandler) {
atomic.StorePointer(&d.delegate, unsafe.Pointer(&eh))
d.delegate.Store(&eh)
}
func defaultErrorHandler() *ErrDelegator {

View File

@ -18,7 +18,6 @@ import (
"log"
"os"
"sync/atomic"
"unsafe"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
@ -28,7 +27,7 @@ import (
//
// The default logger uses stdr which is backed by the standard `log.Logger`
// interface. This logger will only show messages at the Error Level.
var globalLogger unsafe.Pointer
var globalLogger atomic.Pointer[logr.Logger]
func init() {
SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))
@ -40,11 +39,11 @@ func init() {
// To see Info messages use a logger with `l.V(4).Enabled() == true`
// To see Debug messages use a logger with `l.V(8).Enabled() == true`.
func SetLogger(l logr.Logger) {
atomic.StorePointer(&globalLogger, unsafe.Pointer(&l))
globalLogger.Store(&l)
}
func getLogger() logr.Logger {
return *(*logr.Logger)(atomic.LoadPointer(&globalLogger))
return *globalLogger.Load()
}
// Info prints messages about the general state of the API or SDK.