github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/kernel/auth/atomicptr_credentials_unsafe.go (about)

     1  package auth
     2  
     3  import (
     4  	"context"
     5  	"sync/atomic"
     6  	"unsafe"
     7  )
     8  
     9  // An AtomicPtr is a pointer to a value of type Value that can be atomically
    10  // loaded and stored. The zero value of an AtomicPtr represents nil.
    11  //
    12  // Note that copying AtomicPtr by value performs a non-atomic read of the
    13  // stored pointer, which is unsafe if Store() can be called concurrently; in
    14  // this case, do `dst.Store(src.Load())` instead.
    15  //
    16  // +stateify savable
    17  type AtomicPtrCredentials struct {
    18  	ptr unsafe.Pointer `state:".(*Credentials)"`
    19  }
    20  
    21  func (p *AtomicPtrCredentials) savePtr() *Credentials {
    22  	return p.Load()
    23  }
    24  
    25  func (p *AtomicPtrCredentials) loadPtr(_ context.Context, v *Credentials) {
    26  	p.Store(v)
    27  }
    28  
    29  // Load returns the value set by the most recent Store. It returns nil if there
    30  // has been no previous call to Store.
    31  //
    32  //go:nosplit
    33  func (p *AtomicPtrCredentials) Load() *Credentials {
    34  	return (*Credentials)(atomic.LoadPointer(&p.ptr))
    35  }
    36  
    37  // Store sets the value returned by Load to x.
    38  func (p *AtomicPtrCredentials) Store(x *Credentials) {
    39  	atomic.StorePointer(&p.ptr, (unsafe.Pointer)(x))
    40  }
    41  
    42  // Swap atomically stores `x` into *p and returns the previous *p value.
    43  func (p *AtomicPtrCredentials) Swap(x *Credentials) *Credentials {
    44  	return (*Credentials)(atomic.SwapPointer(&p.ptr, (unsafe.Pointer)(x)))
    45  }