...

Source file src/sync/atomic/doc.go

Documentation: sync/atomic

     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  //
    40  // In the terminology of the Go memory model, if the effect of
    41  // an atomic operation A is observed by atomic operation B,
    42  // then A “synchronizes before” B.
    43  // Additionally, all the atomic operations executed in a program
    44  // behave as though executed in some sequentially consistent order.
    45  // This definition provides the same semantics as
    46  // C++'s sequentially consistent atomics and Java's volatile variables.
    47  package atomic
    48  
    49  import (
    50  	"unsafe"
    51  )
    52  
    53  // BUG(rsc): On 386, the 64-bit functions use instructions unavailable before the Pentium MMX.
    54  //
    55  // On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core.
    56  //
    57  // On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to arrange
    58  // for 64-bit alignment of 64-bit words accessed atomically via the primitive
    59  // atomic functions (types [Int64] and [Uint64] are automatically aligned).
    60  // The first word in an allocated struct, array, or slice; in a global
    61  // variable; or in a local variable (because the subject of all atomic operations
    62  // will escape to the heap) can be relied upon to be 64-bit aligned.
    63  
    64  // SwapInt32 atomically stores new into *addr and returns the previous *addr value.
    65  // Consider using the more ergonomic and less error-prone [Int32.Swap] instead.
    66  func SwapInt32(addr *int32, new int32) (old int32)
    67  
    68  // SwapInt64 atomically stores new into *addr and returns the previous *addr value.
    69  // Consider using the more ergonomic and less error-prone [Int64.Swap] instead
    70  // (particularly if you target 32-bit platforms; see the bugs section).
    71  func SwapInt64(addr *int64, new int64) (old int64)
    72  
    73  // SwapUint32 atomically stores new into *addr and returns the previous *addr value.
    74  // Consider using the more ergonomic and less error-prone [Uint32.Swap] instead.
    75  func SwapUint32(addr *uint32, new uint32) (old uint32)
    76  
    77  // SwapUint64 atomically stores new into *addr and returns the previous *addr value.
    78  // Consider using the more ergonomic and less error-prone [Uint64.Swap] instead
    79  // (particularly if you target 32-bit platforms; see the bugs section).
    80  func SwapUint64(addr *uint64, new uint64) (old uint64)
    81  
    82  // SwapUintptr atomically stores new into *addr and returns the previous *addr value.
    83  // Consider using the more ergonomic and less error-prone [Uintptr.Swap] instead.
    84  func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)
    85  
    86  // SwapPointer atomically stores new into *addr and returns the previous *addr value.
    87  // Consider using the more ergonomic and less error-prone [Pointer.Swap] instead.
    88  func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
    89  
    90  // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.
    91  // Consider using the more ergonomic and less error-prone [Int32.CompareAndSwap] instead.
    92  func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
    93  
    94  // CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.
    95  // Consider using the more ergonomic and less error-prone [Int64.CompareAndSwap] instead
    96  // (particularly if you target 32-bit platforms; see the bugs section).
    97  func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
    98  
    99  // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.
   100  // Consider using the more ergonomic and less error-prone [Uint32.CompareAndSwap] instead.
   101  func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
   102  
   103  // CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.
   104  // Consider using the more ergonomic and less error-prone [Uint64.CompareAndSwap] instead
   105  // (particularly if you target 32-bit platforms; see the bugs section).
   106  func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
   107  
   108  // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.
   109  // Consider using the more ergonomic and less error-prone [Uintptr.CompareAndSwap] instead.
   110  func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
   111  
   112  // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.
   113  // Consider using the more ergonomic and less error-prone [Pointer.CompareAndSwap] instead.
   114  func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)
   115  
   116  // AddInt32 atomically adds delta to *addr and returns the new value.
   117  // Consider using the more ergonomic and less error-prone [Int32.Add] instead.
   118  func AddInt32(addr *int32, delta int32) (new int32)
   119  
   120  // AddUint32 atomically adds delta to *addr and returns the new value.
   121  // To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)).
   122  // In particular, to decrement x, do AddUint32(&x, ^uint32(0)).
   123  // Consider using the more ergonomic and less error-prone [Uint32.Add] instead.
   124  func AddUint32(addr *uint32, delta uint32) (new uint32)
   125  
   126  // AddInt64 atomically adds delta to *addr and returns the new value.
   127  // Consider using the more ergonomic and less error-prone [Int64.Add] instead
   128  // (particularly if you target 32-bit platforms; see the bugs section).
   129  func AddInt64(addr *int64, delta int64) (new int64)
   130  
   131  // AddUint64 atomically adds delta to *addr and returns the new value.
   132  // To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)).
   133  // In particular, to decrement x, do AddUint64(&x, ^uint64(0)).
   134  // Consider using the more ergonomic and less error-prone [Uint64.Add] instead
   135  // (particularly if you target 32-bit platforms; see the bugs section).
   136  func AddUint64(addr *uint64, delta uint64) (new uint64)
   137  
   138  // AddUintptr atomically adds delta to *addr and returns the new value.
   139  // Consider using the more ergonomic and less error-prone [Uintptr.Add] instead.
   140  func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
   141  
   142  // LoadInt32 atomically loads *addr.
   143  // Consider using the more ergonomic and less error-prone [Int32.Load] instead.
   144  func LoadInt32(addr *int32) (val int32)
   145  
   146  // LoadInt64 atomically loads *addr.
   147  // Consider using the more ergonomic and less error-prone [Int64.Load] instead
   148  // (particularly if you target 32-bit platforms; see the bugs section).
   149  func LoadInt64(addr *int64) (val int64)
   150  
   151  // LoadUint32 atomically loads *addr.
   152  // Consider using the more ergonomic and less error-prone [Uint32.Load] instead.
   153  func LoadUint32(addr *uint32) (val uint32)
   154  
   155  // LoadUint64 atomically loads *addr.
   156  // Consider using the more ergonomic and less error-prone [Uint64.Load] instead
   157  // (particularly if you target 32-bit platforms; see the bugs section).
   158  func LoadUint64(addr *uint64) (val uint64)
   159  
   160  // LoadUintptr atomically loads *addr.
   161  // Consider using the more ergonomic and less error-prone [Uintptr.Load] instead.
   162  func LoadUintptr(addr *uintptr) (val uintptr)
   163  
   164  // LoadPointer atomically loads *addr.
   165  // Consider using the more ergonomic and less error-prone [Pointer.Load] instead.
   166  func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
   167  
   168  // StoreInt32 atomically stores val into *addr.
   169  // Consider using the more ergonomic and less error-prone [Int32.Store] instead.
   170  func StoreInt32(addr *int32, val int32)
   171  
   172  // StoreInt64 atomically stores val into *addr.
   173  // Consider using the more ergonomic and less error-prone [Int64.Store] instead
   174  // (particularly if you target 32-bit platforms; see the bugs section).
   175  func StoreInt64(addr *int64, val int64)
   176  
   177  // StoreUint32 atomically stores val into *addr.
   178  // Consider using the more ergonomic and less error-prone [Uint32.Store] instead.
   179  func StoreUint32(addr *uint32, val uint32)
   180  
   181  // StoreUint64 atomically stores val into *addr.
   182  // Consider using the more ergonomic and less error-prone [Uint64.Store] instead
   183  // (particularly if you target 32-bit platforms; see the bugs section).
   184  func StoreUint64(addr *uint64, val uint64)
   185  
   186  // StoreUintptr atomically stores val into *addr.
   187  // Consider using the more ergonomic and less error-prone [Uintptr.Store] instead.
   188  func StoreUintptr(addr *uintptr, val uintptr)
   189  
   190  // StorePointer atomically stores val into *addr.
   191  // Consider using the more ergonomic and less error-prone [Pointer.Store] instead.
   192  func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
   193  

View as plain text