github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/sync/atomic/doc.go (about) 1 // Copyright 2011 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 // +build !race 6 7 // Package atomic provides low-level atomic memory primitives 8 // useful for implementing synchronization algorithms. 9 // 10 // These functions require great care to be used correctly. 11 // Except for special, low-level applications, synchronization is better 12 // done with channels or the facilities of the sync package. 13 // Share memory by communicating; 14 // don't communicate by sharing memory. 15 // 16 // The swap operation, implemented by the SwapT functions, is the atomic 17 // equivalent of: 18 // 19 // old = *addr 20 // *addr = new 21 // return old 22 // 23 // The compare-and-swap operation, implemented by the CompareAndSwapT 24 // functions, is the atomic equivalent of: 25 // 26 // if *addr == old { 27 // *addr = new 28 // return true 29 // } 30 // return false 31 // 32 // The add operation, implemented by the AddT functions, is the atomic 33 // equivalent of: 34 // 35 // *addr += delta 36 // return *addr 37 // 38 // The load and store operations, implemented by the LoadT and StoreT 39 // functions, are the atomic equivalents of "return *addr" and 40 // "*addr = val". 41 // 42 package atomic 43 44 import ( 45 "unsafe" 46 ) 47 48 // BUG(rsc): On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX. 49 // 50 // On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core. 51 // 52 // On both ARM and x86-32, it is the caller's responsibility to arrange for 64-bit 53 // alignment of 64-bit words accessed atomically. The first word in a global 54 // variable or in an allocated struct or slice can be relied upon to be 55 // 64-bit aligned. 56 57 // SwapInt32 atomically stores new into *addr and returns the previous *addr value. 58 func SwapInt32(addr *int32, new int32) (old int32) 59 60 // SwapInt64 atomically stores new into *addr and returns the previous *addr value. 61 func SwapInt64(addr *int64, new int64) (old int64) 62 63 // SwapUint32 atomically stores new into *addr and returns the previous *addr value. 64 func SwapUint32(addr *uint32, new uint32) (old uint32) 65 66 // SwapUint64 atomically stores new into *addr and returns the previous *addr value. 67 func SwapUint64(addr *uint64, new uint64) (old uint64) 68 69 // SwapUintptr atomically stores new into *addr and returns the previous *addr value. 70 func SwapUintptr(addr *uintptr, new uintptr) (old uintptr) 71 72 // SwapPointer atomically stores new into *addr and returns the previous *addr value. 73 func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer) 74 75 // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value. 76 func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool) 77 78 // CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value. 79 func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool) 80 81 // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value. 82 func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool) 83 84 // CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value. 85 func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool) 86 87 // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value. 88 func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool) 89 90 // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value. 91 func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) 92 93 // AddInt32 atomically adds delta to *addr and returns the new value. 94 func AddInt32(addr *int32, delta int32) (new int32) 95 96 // AddUint32 atomically adds delta to *addr and returns the new value. 97 // To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)). 98 // In particular, to decrement x, do AddUint32(&x, ^uint32(0)). 99 func AddUint32(addr *uint32, delta uint32) (new uint32) 100 101 // AddInt64 atomically adds delta to *addr and returns the new value. 102 func AddInt64(addr *int64, delta int64) (new int64) 103 104 // AddUint64 atomically adds delta to *addr and returns the new value. 105 // To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)). 106 // In particular, to decrement x, do AddUint64(&x, ^uint64(0)). 107 func AddUint64(addr *uint64, delta uint64) (new uint64) 108 109 // AddUintptr atomically adds delta to *addr and returns the new value. 110 func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) 111 112 // LoadInt32 atomically loads *addr. 113 func LoadInt32(addr *int32) (val int32) 114 115 // LoadInt64 atomically loads *addr. 116 func LoadInt64(addr *int64) (val int64) 117 118 // LoadUint32 atomically loads *addr. 119 func LoadUint32(addr *uint32) (val uint32) 120 121 // LoadUint64 atomically loads *addr. 122 func LoadUint64(addr *uint64) (val uint64) 123 124 // LoadUintptr atomically loads *addr. 125 func LoadUintptr(addr *uintptr) (val uintptr) 126 127 // LoadPointer atomically loads *addr. 128 func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) 129 130 // StoreInt32 atomically stores val into *addr. 131 func StoreInt32(addr *int32, val int32) 132 133 // StoreInt64 atomically stores val into *addr. 134 func StoreInt64(addr *int64, val int64) 135 136 // StoreUint32 atomically stores val into *addr. 137 func StoreUint32(addr *uint32, val uint32) 138 139 // StoreUint64 atomically stores val into *addr. 140 func StoreUint64(addr *uint64, val uint64) 141 142 // StoreUintptr atomically stores val into *addr. 143 func StoreUintptr(addr *uintptr, val uintptr) 144 145 // StorePointer atomically stores val into *addr. 146 func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer) 147 148 // Helper for ARM. Linker will discard on other systems 149 func panic64() { 150 panic("sync/atomic: broken 64-bit atomic operations (buggy QEMU)") 151 }