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