[fix] nazaatomic: uint64和int64,在32位系统时,使用mutex替代标准库中的atomic,避免崩溃,见文章:https://pengrl.com/p/21030/

pull/2/head
q191201771 4 years ago
parent aa23a2a9d8
commit 414ddb2b99

@ -22,14 +22,6 @@ type Uint32 struct {
core uint32
}
type Int64 struct {
core int64
}
type Uint64 struct {
core uint64
}
// ----------------------------------------------------------------------------
func (obj *Int32) Load() int32 {
@ -102,76 +94,6 @@ func (obj *Uint32) Swap(new uint32) (old uint32) {
// ----------------------------------------------------------------------------
func (obj *Uint64) Load() uint64 {
return atomic.LoadUint64(&obj.core)
}
func (obj *Uint64) Store(val uint64) {
atomic.StoreUint64(&obj.core, val)
}
func (obj *Uint64) Add(delta uint64) (new uint64) {
return atomic.AddUint64(&obj.core, delta)
}
// @param delta 举例传入3则减3
func (obj *Uint64) Sub(delta uint64) (new uint64) {
return atomic.AddUint64(&obj.core, ^uint64(delta-1))
}
func (obj *Uint64) Increment() (new uint64) {
return atomic.AddUint64(&obj.core, 1)
}
func (obj *Uint64) Decrement() (new uint64) {
return atomic.AddUint64(&obj.core, ^uint64(0))
}
func (obj *Uint64) CompareAndSwap(old uint64, new uint64) (swapped bool) {
return atomic.CompareAndSwapUint64(&obj.core, old, new)
}
func (obj *Uint64) Swap(new uint64) (old uint64) {
return atomic.SwapUint64(&obj.core, new)
}
// ----------------------------------------------------------------------------
func (obj *Int64) Load() int64 {
return atomic.LoadInt64(&obj.core)
}
func (obj *Int64) Store(val int64) {
atomic.StoreInt64(&obj.core, val)
}
func (obj *Int64) Add(delta int64) (new int64) {
return atomic.AddInt64(&obj.core, delta)
}
// @param delta 举例传入3则减3
func (obj *Int64) Sub(delta int64) (new int64) {
return atomic.AddInt64(&obj.core, -delta)
}
func (obj *Int64) Increment() (new int64) {
return atomic.AddInt64(&obj.core, 1)
}
func (obj *Int64) Decrement() (new int64) {
return atomic.AddInt64(&obj.core, -1)
}
func (obj *Int64) CompareAndSwap(old int64, new int64) (swapped bool) {
return atomic.CompareAndSwapInt64(&obj.core, old, new)
}
func (obj *Int64) Swap(new int64) (old int64) {
return atomic.SwapInt64(&obj.core, new)
}
// ----------------------------------------------------------------------------
func (obj *Bool) Load() bool {
return int32tobool(obj.core.Load())
}

@ -0,0 +1,163 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
// +build 386 arm
package nazaatomic
import (
"sync"
)
// 注意因为32位系统下使用标准库中的atomic操作64位整型有bug所以32位系统原子操作64位整型时我们使用mutex
type Int64 struct {
mu sync.Mutex
core int64
}
type Uint64 struct {
mu sync.Mutex
core uint64
}
// ----------------------------------------------------------------------------
func (obj *Uint64) Load() uint64 {
obj.mu.Lock()
ret := obj.core
obj.mu.Unlock()
return ret
}
func (obj *Uint64) Store(val uint64) {
obj.mu.Lock()
obj.core = val
obj.mu.Unlock()
}
func (obj *Uint64) Add(delta uint64) (new uint64) {
obj.mu.Lock()
obj.core += delta
new = obj.core
obj.mu.Unlock()
return
}
// @param delta 举例传入3则减3
func (obj *Uint64) Sub(delta uint64) (new uint64) {
obj.mu.Lock()
obj.core += ^uint64(delta - 1)
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Uint64) Increment() (new uint64) {
obj.mu.Lock()
obj.core += 1
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Uint64) Decrement() (new uint64) {
obj.mu.Lock()
obj.core += ^uint64(0)
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Uint64) CompareAndSwap(old uint64, new uint64) (swapped bool) {
obj.mu.Lock()
if obj.core == old {
obj.core = new
obj.mu.Unlock()
return true
}
obj.mu.Unlock()
return false
}
func (obj *Uint64) Swap(new uint64) (old uint64) {
obj.mu.Lock()
old = obj.core
obj.core = new
obj.mu.Unlock()
return
}
// ----------------------------------------------------------------------------
func (obj *Int64) Load() int64 {
obj.mu.Lock()
ret := obj.core
obj.mu.Unlock()
return ret
}
func (obj *Int64) Store(val int64) {
obj.mu.Lock()
obj.core = val
obj.mu.Unlock()
}
func (obj *Int64) Add(delta int64) (new int64) {
obj.mu.Lock()
obj.core += delta
new = obj.core
obj.mu.Unlock()
return
}
// @param delta 举例传入3则减3
func (obj *Int64) Sub(delta int64) (new int64) {
obj.mu.Lock()
obj.core += ^int64(delta - 1)
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Int64) Increment() (new int64) {
obj.mu.Lock()
obj.core += 1
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Int64) Decrement() (new int64) {
obj.mu.Lock()
obj.core += ^int64(0)
new = obj.core
obj.mu.Unlock()
return
}
func (obj *Int64) CompareAndSwap(old int64, new int64) (swapped bool) {
obj.mu.Lock()
if obj.core == old {
obj.core = new
obj.mu.Unlock()
return true
}
obj.mu.Unlock()
return false
}
func (obj *Int64) Swap(new int64) (old int64) {
obj.mu.Lock()
old = obj.core
obj.core = new
obj.mu.Unlock()
return
}

@ -0,0 +1,91 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
// +build !386, !arm
package nazaatomic
import "sync/atomic"
type Int64 struct {
core int64
}
type Uint64 struct {
core uint64
}
// ----------------------------------------------------------------------------
func (obj *Uint64) Load() uint64 {
return atomic.LoadUint64(&obj.core)
}
func (obj *Uint64) Store(val uint64) {
atomic.StoreUint64(&obj.core, val)
}
func (obj *Uint64) Add(delta uint64) (new uint64) {
return atomic.AddUint64(&obj.core, delta)
}
// @param delta 举例传入3则减3
func (obj *Uint64) Sub(delta uint64) (new uint64) {
return atomic.AddUint64(&obj.core, ^uint64(delta-1))
}
func (obj *Uint64) Increment() (new uint64) {
return atomic.AddUint64(&obj.core, 1)
}
func (obj *Uint64) Decrement() (new uint64) {
return atomic.AddUint64(&obj.core, ^uint64(0))
}
func (obj *Uint64) CompareAndSwap(old uint64, new uint64) (swapped bool) {
return atomic.CompareAndSwapUint64(&obj.core, old, new)
}
func (obj *Uint64) Swap(new uint64) (old uint64) {
return atomic.SwapUint64(&obj.core, new)
}
// ----------------------------------------------------------------------------
func (obj *Int64) Load() int64 {
return atomic.LoadInt64(&obj.core)
}
func (obj *Int64) Store(val int64) {
atomic.StoreInt64(&obj.core, val)
}
func (obj *Int64) Add(delta int64) (new int64) {
return atomic.AddInt64(&obj.core, delta)
}
// @param delta 举例传入3则减3
func (obj *Int64) Sub(delta int64) (new int64) {
return atomic.AddInt64(&obj.core, -delta)
}
func (obj *Int64) Increment() (new int64) {
return atomic.AddInt64(&obj.core, 1)
}
func (obj *Int64) Decrement() (new int64) {
return atomic.AddInt64(&obj.core, -1)
}
func (obj *Int64) CompareAndSwap(old int64, new int64) (swapped bool) {
return atomic.CompareAndSwapInt64(&obj.core, old, new)
}
func (obj *Int64) Swap(new int64) (old int64) {
return atomic.SwapInt64(&obj.core, new)
}
Loading…
Cancel
Save