golang原子库atomic

前端之家收集整理的这篇文章主要介绍了golang原子库atomic前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
packageatomic

import(
	"unsafe"
)

//BUG(rsc):Onx86-32,the64-bitfunctionsuseinstructionsunavailablebeforethePentiumMMX.
//
//Onnon-LinuxARM,the64-bitfunctionsuseinstructionsunavailablebeforetheARMv6kcore.
//
//OnbothARMandx86-32,itisthecaller'sresponsibilitytoarrangefor64-bit
//alignmentof64-bitwordsaccessedatomically.Thefirstwordinaglobal
//variableorinanallocatedstructorslicecanbereliedupontobe
//64-bitaligned.

//SwapInt32atomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapInt32(addr*int32,newint32)(oldint32)

//SwapInt64atomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapInt64(addr*int64,newint64)(oldint64)

//SwapUint32atomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapUint32(addr*uint32,newuint32)(olduint32)

//SwapUint64atomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapUint64(addr*uint64,newuint64)(olduint64)

//SwapUintptratomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapUintptr(addr*uintptr,newuintptr)(olduintptr)

//SwapPointeratomicallystoresnewinto*addrandreturnstheprevIoUs*addrvalue.
funcSwapPointer(addr*unsafe.Pointer,newunsafe.Pointer)(oldunsafe.Pointer)

//CompareAndSwapInt32executesthecompare-and-swapoperationforanint32value.
funcCompareAndSwapInt32(addr*int32,old,newint32)(swappedbool)

//CompareAndSwapInt64executesthecompare-and-swapoperationforanint64value.
funcCompareAndSwapInt64(addr*int64,newint64)(swappedbool)

//CompareAndSwapUint32executesthecompare-and-swapoperationforauint32value.
funcCompareAndSwapUint32(addr*uint32,newuint32)(swappedbool)

//CompareAndSwapUint64executesthecompare-and-swapoperationforauint64value.
funcCompareAndSwapUint64(addr*uint64,newuint64)(swappedbool)

//CompareAndSwapUintptrexecutesthecompare-and-swapoperationforauintptrvalue.
funcCompareAndSwapUintptr(addr*uintptr,newuintptr)(swappedbool)

//CompareAndSwapPointerexecutesthecompare-and-swapoperationforaunsafe.Pointervalue.
funcCompareAndSwapPointer(addr*unsafe.Pointer,newunsafe.Pointer)(swappedbool)

//AddInt32atomicallyaddsdeltato*addrandreturnsthenewvalue.
funcAddInt32(addr*int32,deltaint32)(newint32)

//AddUint32atomicallyaddsdeltato*addrandreturnsthenewvalue.
//Tosubtractasignedpositiveconstantvaluecfromx,doAddUint32(&x,^uint32(c-1)).
//Inparticular,todecrementx,^uint32(0)).
funcAddUint32(addr*uint32,deltauint32)(newuint32)

//AddInt64atomicallyaddsdeltato*addrandreturnsthenewvalue.
funcAddInt64(addr*int64,deltaint64)(newint64)

//AddUint64atomicallyaddsdeltato*addrandreturnsthenewvalue.
//Tosubtractasignedpositiveconstantvaluecfromx,doAddUint64(&x,^uint64(c-1)).
//Inparticular,^uint64(0)).
funcAddUint64(addr*uint64,deltauint64)(newuint64)

//AddUintptratomicallyaddsdeltato*addrandreturnsthenewvalue.
funcAddUintptr(addr*uintptr,deltauintptr)(newuintptr)

//LoadInt32atomicallyloads*addr.
funcLoadInt32(addr*int32)(valint32)

//LoadInt64atomicallyloads*addr.
funcLoadInt64(addr*int64)(valint64)

//LoadUint32atomicallyloads*addr.
funcLoadUint32(addr*uint32)(valuint32)

//LoadUint64atomicallyloads*addr.
funcLoadUint64(addr*uint64)(valuint64)

//LoadUintptratomicallyloads*addr.
funcLoadUintptr(addr*uintptr)(valuintptr)

//LoadPointeratomicallyloads*addr.
funcLoadPointer(addr*unsafe.Pointer)(valunsafe.Pointer)

//StoreInt32atomicallystoresvalinto*addr.
funcStoreInt32(addr*int32,valint32)

//StoreInt64atomicallystoresvalinto*addr.
funcStoreInt64(addr*int64,valint64)

//StoreUint32atomicallystoresvalinto*addr.
funcStoreUint32(addr*uint32,valuint32)

//StoreUint64atomicallystoresvalinto*addr.
funcStoreUint64(addr*uint64,valuint64)

//StoreUintptratomicallystoresvalinto*addr.
funcStoreUintptr(addr*uintptr,valuintptr)

//StorePointeratomicallystoresvalinto*addr.
funcStorePointer(addr*unsafe.Pointer,valunsafe.Pointer)

//HelperforARM.Linkerwilldiscardonothersystems
funcpanic64(){
	panic("sync/atomic:broken64-bitatomicoperations(buggyQEMU)")
}

所有这些操作都是atomic,原子执行的操作,golang底层实

比如:

swap函数相当于(并且是原子执行的):

old = *addr

*addr = new

return old

原文链接:https://www.f2er.com/go/190291.html

猜你在找的Go相关文章