github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/go-weave/weave/atomic.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package weave
     6  
     7  type AtomicInt32 struct {
     8  	v int32
     9  }
    10  
    11  func (a *AtomicInt32) Add(delta int32) (new int32) {
    12  	a.v += delta
    13  	new = a.v
    14  	globalSched.Sched()
    15  	return new
    16  }
    17  
    18  func (a *AtomicInt32) CompareAndSwap(old, new int32) (swapped bool) {
    19  	swapped = a.v == old
    20  	if swapped {
    21  		a.v = new
    22  	}
    23  	globalSched.Sched()
    24  	return
    25  }
    26  
    27  func (a *AtomicInt32) Load() int32 {
    28  	v := a.v
    29  	globalSched.Sched()
    30  	return v
    31  }
    32  
    33  func (a *AtomicInt32) Store(val int32) {
    34  	a.v = val
    35  	globalSched.Sched()
    36  }
    37  
    38  func (a *AtomicInt32) Swap(new int32) (old int32) {
    39  	old, a.v = a.v, new
    40  	globalSched.Sched()
    41  	return
    42  }