github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/atomic.go (about)

     1  package main
     2  
     3  import (
     4  	"sync/atomic"
     5  	"unsafe"
     6  
     7  	"runtime/volatile"
     8  )
     9  
    10  func main() {
    11  	i32 := int32(-5)
    12  	println("AddInt32:", atomic.AddInt32(&i32, 8), i32)
    13  
    14  	i64 := int64(-5)
    15  	println("AddInt64:", atomic.AddInt64(&i64, 8), i64)
    16  
    17  	u32 := uint32(5)
    18  	println("AddUint32:", atomic.AddUint32(&u32, 8), u32)
    19  
    20  	u64 := uint64(5)
    21  	println("AddUint64:", atomic.AddUint64(&u64, 8), u64)
    22  
    23  	uptr := uintptr(5)
    24  	println("AddUintptr:", uint64(atomic.AddUintptr(&uptr, 8)), uint64(uptr))
    25  
    26  	println("SwapInt32:", atomic.SwapInt32(&i32, 33), i32)
    27  	println("SwapInt64:", atomic.SwapInt64(&i64, 33), i64)
    28  	println("SwapUint32:", atomic.SwapUint32(&u32, 33), u32)
    29  	println("SwapUint64:", atomic.SwapUint64(&u64, 33), u64)
    30  	println("SwapUintptr:", uint64(atomic.SwapUintptr(&uptr, 33)), uint64(uptr))
    31  	ptr := unsafe.Pointer(&i32)
    32  	println("SwapPointer:", atomic.SwapPointer(&ptr, unsafe.Pointer(&u32)) == unsafe.Pointer(&i32), ptr == unsafe.Pointer(&u32))
    33  
    34  	i32 = int32(-5)
    35  	println("CompareAndSwapInt32:", atomic.CompareAndSwapInt32(&i32, 5, 3), i32)
    36  	println("CompareAndSwapInt32:", atomic.CompareAndSwapInt32(&i32, -5, 3), i32)
    37  
    38  	i64 = int64(-5)
    39  	println("CompareAndSwapInt64:", atomic.CompareAndSwapInt64(&i64, 5, 3), i64)
    40  	println("CompareAndSwapInt64:", atomic.CompareAndSwapInt64(&i64, -5, 3), i64)
    41  
    42  	u32 = uint32(5)
    43  	println("CompareAndSwapUint32:", atomic.CompareAndSwapUint32(&u32, 4, 3), u32)
    44  	println("CompareAndSwapUint32:", atomic.CompareAndSwapUint32(&u32, 5, 3), u32)
    45  
    46  	u64 = uint64(5)
    47  	println("CompareAndSwapUint64:", atomic.CompareAndSwapUint64(&u64, 4, 3), u64)
    48  	println("CompareAndSwapUint64:", atomic.CompareAndSwapUint64(&u64, 5, 3), u64)
    49  
    50  	uptr = uintptr(5)
    51  	println("CompareAndSwapUintptr:", atomic.CompareAndSwapUintptr(&uptr, 4, 3), uint64(uptr))
    52  	println("CompareAndSwapUintptr:", atomic.CompareAndSwapUintptr(&uptr, 5, 3), uint64(uptr))
    53  
    54  	ptr = unsafe.Pointer(&i32)
    55  	println("CompareAndSwapPointer:", atomic.CompareAndSwapPointer(&ptr, unsafe.Pointer(&u32), unsafe.Pointer(&i64)), ptr == unsafe.Pointer(&i32))
    56  	println("CompareAndSwapPointer:", atomic.CompareAndSwapPointer(&ptr, unsafe.Pointer(&i32), unsafe.Pointer(&i64)), ptr == unsafe.Pointer(&i64))
    57  
    58  	println("LoadInt32:", atomic.LoadInt32(&i32))
    59  	println("LoadInt64:", atomic.LoadInt64(&i64))
    60  	println("LoadUint32:", atomic.LoadUint32(&u32))
    61  	println("LoadUint64:", atomic.LoadUint64(&u64))
    62  	println("LoadUintptr:", uint64(atomic.LoadUintptr(&uptr)))
    63  	println("LoadPointer:", atomic.LoadPointer(&ptr) == unsafe.Pointer(&i64))
    64  
    65  	atomic.StoreInt32(&i32, -20)
    66  	println("StoreInt32:", i32)
    67  
    68  	atomic.StoreInt64(&i64, -20)
    69  	println("StoreInt64:", i64)
    70  
    71  	atomic.StoreUint32(&u32, 20)
    72  	println("StoreUint32:", u32)
    73  
    74  	atomic.StoreUint64(&u64, 20)
    75  	println("StoreUint64:", u64)
    76  
    77  	atomic.StoreUintptr(&uptr, 20)
    78  	println("StoreUintptr:", uint64(uptr))
    79  
    80  	atomic.StorePointer(&ptr, unsafe.Pointer(&uptr))
    81  	println("StorePointer:", ptr == unsafe.Pointer(&uptr))
    82  
    83  	// test atomic.Value load/store operations
    84  	testValue(int(3), int(-2))
    85  	testValue("", "foobar", "baz")
    86  
    87  	// Test atomic and volatile operations as deferred values.
    88  	testDefer()
    89  }
    90  
    91  func testValue(values ...interface{}) {
    92  	var av atomic.Value
    93  	for _, val := range values {
    94  		av.Store(val)
    95  		loadedVal := av.Load()
    96  		if loadedVal != val {
    97  			println("val store/load didn't work, expected", val, "but got", loadedVal)
    98  		}
    99  	}
   100  }
   101  
   102  func testDefer() {
   103  	n1 := int32(5)
   104  	n2 := uint32(6)
   105  	defer func() {
   106  		println("deferred atomic add:", n1)
   107  		println("deferred volatile store:", n2)
   108  	}()
   109  	defer atomic.AddInt32(&n1, 3)
   110  	defer volatile.StoreUint32(&n2, 22)
   111  }