...
Run Format

Source file src/runtime/internal/atomic/atomic_arm.go

Documentation: runtime/internal/atomic

     1  // Copyright 2009 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 arm
     6  
     7  package atomic
     8  
     9  import (
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  type spinlock struct {
    15  	v uint32
    16  }
    17  
    18  //go:nosplit
    19  func (l *spinlock) lock() {
    20  	for {
    21  		if Cas(&l.v, 0, 1) {
    22  			return
    23  		}
    24  	}
    25  }
    26  
    27  //go:nosplit
    28  func (l *spinlock) unlock() {
    29  	Store(&l.v, 0)
    30  }
    31  
    32  var locktab [57]struct {
    33  	l   spinlock
    34  	pad [sys.CacheLineSize - unsafe.Sizeof(spinlock{})]byte
    35  }
    36  
    37  func addrLock(addr *uint64) *spinlock {
    38  	return &locktab[(uintptr(unsafe.Pointer(addr))>>3)%uintptr(len(locktab))].l
    39  }
    40  
    41  // Atomic add and return new value.
    42  //go:nosplit
    43  func Xadd(val *uint32, delta int32) uint32 {
    44  	for {
    45  		oval := *val
    46  		nval := oval + uint32(delta)
    47  		if Cas(val, oval, nval) {
    48  			return nval
    49  		}
    50  	}
    51  }
    52  
    53  //go:noescape
    54  func Xadduintptr(ptr *uintptr, delta uintptr) uintptr
    55  
    56  //go:nosplit
    57  func Xchg(addr *uint32, v uint32) uint32 {
    58  	for {
    59  		old := *addr
    60  		if Cas(addr, old, v) {
    61  			return old
    62  		}
    63  	}
    64  }
    65  
    66  //go:nosplit
    67  func Xchguintptr(addr *uintptr, v uintptr) uintptr {
    68  	return uintptr(Xchg((*uint32)(unsafe.Pointer(addr)), uint32(v)))
    69  }
    70  
    71  // Not noescape -- it installs a pointer to addr.
    72  func StorepNoWB(addr unsafe.Pointer, v unsafe.Pointer)
    73  
    74  //go:noescape
    75  func Store(addr *uint32, v uint32)
    76  
    77  //go:nosplit
    78  func goCas64(addr *uint64, old, new uint64) bool {
    79  	if uintptr(unsafe.Pointer(addr))&7 != 0 {
    80  		*(*int)(nil) = 0 // crash on unaligned uint64
    81  	}
    82  	_ = *addr // if nil, fault before taking the lock
    83  	var ok bool
    84  	addrLock(addr).lock()
    85  	if *addr == old {
    86  		*addr = new
    87  		ok = true
    88  	}
    89  	addrLock(addr).unlock()
    90  	return ok
    91  }
    92  
    93  //go:nosplit
    94  func goXadd64(addr *uint64, delta int64) uint64 {
    95  	if uintptr(unsafe.Pointer(addr))&7 != 0 {
    96  		*(*int)(nil) = 0 // crash on unaligned uint64
    97  	}
    98  	_ = *addr // if nil, fault before taking the lock
    99  	var r uint64
   100  	addrLock(addr).lock()
   101  	r = *addr + uint64(delta)
   102  	*addr = r
   103  	addrLock(addr).unlock()
   104  	return r
   105  }
   106  
   107  //go:nosplit
   108  func goXchg64(addr *uint64, v uint64) uint64 {
   109  	if uintptr(unsafe.Pointer(addr))&7 != 0 {
   110  		*(*int)(nil) = 0 // crash on unaligned uint64
   111  	}
   112  	_ = *addr // if nil, fault before taking the lock
   113  	var r uint64
   114  	addrLock(addr).lock()
   115  	r = *addr
   116  	*addr = v
   117  	addrLock(addr).unlock()
   118  	return r
   119  }
   120  
   121  //go:nosplit
   122  func goLoad64(addr *uint64) uint64 {
   123  	if uintptr(unsafe.Pointer(addr))&7 != 0 {
   124  		*(*int)(nil) = 0 // crash on unaligned uint64
   125  	}
   126  	_ = *addr // if nil, fault before taking the lock
   127  	var r uint64
   128  	addrLock(addr).lock()
   129  	r = *addr
   130  	addrLock(addr).unlock()
   131  	return r
   132  }
   133  
   134  //go:nosplit
   135  func goStore64(addr *uint64, v uint64) {
   136  	if uintptr(unsafe.Pointer(addr))&7 != 0 {
   137  		*(*int)(nil) = 0 // crash on unaligned uint64
   138  	}
   139  	_ = *addr // if nil, fault before taking the lock
   140  	addrLock(addr).lock()
   141  	*addr = v
   142  	addrLock(addr).unlock()
   143  }
   144  
   145  //go:nosplit
   146  func Or8(addr *uint8, v uint8) {
   147  	// Align down to 4 bytes and use 32-bit CAS.
   148  	uaddr := uintptr(unsafe.Pointer(addr))
   149  	addr32 := (*uint32)(unsafe.Pointer(uaddr &^ 3))
   150  	word := uint32(v) << ((uaddr & 3) * 8) // little endian
   151  	for {
   152  		old := *addr32
   153  		if Cas(addr32, old, old|word) {
   154  			return
   155  		}
   156  	}
   157  }
   158  
   159  //go:nosplit
   160  func And8(addr *uint8, v uint8) {
   161  	// Align down to 4 bytes and use 32-bit CAS.
   162  	uaddr := uintptr(unsafe.Pointer(addr))
   163  	addr32 := (*uint32)(unsafe.Pointer(uaddr &^ 3))
   164  	word := uint32(v) << ((uaddr & 3) * 8)    // little endian
   165  	mask := uint32(0xFF) << ((uaddr & 3) * 8) // little endian
   166  	word |= ^mask
   167  	for {
   168  		old := *addr32
   169  		if Cas(addr32, old, old&word) {
   170  			return
   171  		}
   172  	}
   173  }
   174  
   175  //go:nosplit
   176  func armcas(ptr *uint32, old, new uint32) bool
   177  
   178  //go:noescape
   179  func Load(addr *uint32) uint32
   180  
   181  //go:noescape
   182  func Loadp(addr unsafe.Pointer) unsafe.Pointer
   183  
   184  //go:noescape
   185  func Cas64(addr *uint64, old, new uint64) bool
   186  
   187  //go:noescape
   188  func Xadd64(addr *uint64, delta int64) uint64
   189  
   190  //go:noescape
   191  func Xchg64(addr *uint64, v uint64) uint64
   192  
   193  //go:noescape
   194  func Load64(addr *uint64) uint64
   195  
   196  //go:noescape
   197  func Store64(addr *uint64, v uint64)
   198  

View as plain text