github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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 compare-and-swap operation, implemented by the CompareAndSwapT 17 // functions, is the atomic equivalent of: 18 // 19 // if *addr == old { 20 // *addr = new 21 // return true 22 // } 23 // return false 24 // 25 // The add operation, implemented by the AddT functions, is the atomic 26 // equivalent of: 27 // 28 // *addr += delta 29 // return *addr 30 // 31 // The load and store operations, implemented by the LoadT and StoreT 32 // functions, are the atomic equivalents of "return *addr" and 33 // "*addr = val". 34 // 35 package atomic 36 37 import ( 38 "unsafe" 39 ) 40 41 // BUG(rsc): On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX. 42 // 43 // On both ARM and x86-32, it is the caller's responsibility to arrange for 64-bit 44 // alignment of 64-bit words accessed atomically. The first word in a global 45 // variable or in an allocated struct or slice can be relied upon to be 46 // 64-bit aligned. 47 48 // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value. 49 func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool) 50 51 // CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value. 52 func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool) 53 54 // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value. 55 func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool) 56 57 // CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value. 58 func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool) 59 60 // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value. 61 func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool) 62 63 // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value. 64 func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) 65 66 // AddInt32 atomically adds delta to *addr and returns the new value. 67 func AddInt32(addr *int32, delta int32) (new int32) 68 69 // AddUint32 atomically adds delta to *addr and returns the new value. 70 func AddUint32(addr *uint32, delta uint32) (new uint32) 71 72 // AddInt64 atomically adds delta to *addr and returns the new value. 73 func AddInt64(addr *int64, delta int64) (new int64) 74 75 // AddUint64 atomically adds delta to *addr and returns the new value. 76 func AddUint64(addr *uint64, delta uint64) (new uint64) 77 78 // AddUintptr atomically adds delta to *addr and returns the new value. 79 func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) 80 81 // LoadInt32 atomically loads *addr. 82 func LoadInt32(addr *int32) (val int32) 83 84 // LoadInt64 atomically loads *addr. 85 func LoadInt64(addr *int64) (val int64) 86 87 // LoadUint32 atomically loads *addr. 88 func LoadUint32(addr *uint32) (val uint32) 89 90 // LoadUint64 atomically loads *addr. 91 func LoadUint64(addr *uint64) (val uint64) 92 93 // LoadUintptr atomically loads *addr. 94 func LoadUintptr(addr *uintptr) (val uintptr) 95 96 // LoadPointer atomically loads *addr. 97 func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) 98 99 // StoreInt32 atomically stores val into *addr. 100 func StoreInt32(addr *int32, val int32) 101 102 // StoreInt64 atomically stores val into *addr. 103 func StoreInt64(addr *int64, val int64) 104 105 // StoreUint32 atomically stores val into *addr. 106 func StoreUint32(addr *uint32, val uint32) 107 108 // StoreUint64 atomically stores val into *addr. 109 func StoreUint64(addr *uint64, val uint64) 110 111 // StoreUintptr atomically stores val into *addr. 112 func StoreUintptr(addr *uintptr, val uintptr) 113 114 // StorePointer atomically stores val into *addr. 115 func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer) 116 117 // Helper for ARM. Linker will discard on other systems 118 func panic64() { 119 panic("sync/atomic: broken 64-bit atomic operations (buggy QEMU)") 120 }