github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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 func AddUint32(addr *uint32, delta uint32) (new uint32) 98 99 // AddInt64 atomically adds delta to *addr and returns the new value. 100 func AddInt64(addr *int64, delta int64) (new int64) 101 102 // AddUint64 atomically adds delta to *addr and returns the new value. 103 func AddUint64(addr *uint64, delta uint64) (new uint64) 104 105 // AddUintptr atomically adds delta to *addr and returns the new value. 106 func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) 107 108 // LoadInt32 atomically loads *addr. 109 func LoadInt32(addr *int32) (val int32) 110 111 // LoadInt64 atomically loads *addr. 112 func LoadInt64(addr *int64) (val int64) 113 114 // LoadUint32 atomically loads *addr. 115 func LoadUint32(addr *uint32) (val uint32) 116 117 // LoadUint64 atomically loads *addr. 118 func LoadUint64(addr *uint64) (val uint64) 119 120 // LoadUintptr atomically loads *addr. 121 func LoadUintptr(addr *uintptr) (val uintptr) 122 123 // LoadPointer atomically loads *addr. 124 func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) 125 126 // StoreInt32 atomically stores val into *addr. 127 func StoreInt32(addr *int32, val int32) 128 129 // StoreInt64 atomically stores val into *addr. 130 func StoreInt64(addr *int64, val int64) 131 132 // StoreUint32 atomically stores val into *addr. 133 func StoreUint32(addr *uint32, val uint32) 134 135 // StoreUint64 atomically stores val into *addr. 136 func StoreUint64(addr *uint64, val uint64) 137 138 // StoreUintptr atomically stores val into *addr. 139 func StoreUintptr(addr *uintptr, val uintptr) 140 141 // StorePointer atomically stores val into *addr. 142 func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer) 143 144 // Helper for ARM. Linker will discard on other systems 145 func panic64() { 146 panic("sync/atomic: broken 64-bit atomic operations (buggy QEMU)") 147 }