1 Star 0 Fork 0

leonxiong/xtool

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
atom.go 2.46 KB
Copy Edit Raw Blame History
package cast
import (
"fmt"
"sync"
"sync/atomic"
)
var atomMutex sync.Mutex
func LockAtom() {
atomMutex.Lock()
}
func UnlockAtom() {
atomMutex.Unlock()
}
// ////////////////////////////int32////////////////////////////
type Int struct {
atomic.Int32
}
func (me *Int) Set(v int32) int32 {
return me.Swap(v)
}
func (me *Int) Inc() int32 {
return me.Add(1)
}
func (me *Int) Dec() int32 {
return me.Add(-1)
}
func (me *Int) Get() int32 {
return me.Load()
}
func (me *Int) ToString() string {
return fmt.Sprintf("%d", me.Load())
}
type UInt struct {
atomic.Uint32
}
func (me *UInt) Set(v uint32) uint32 {
return me.Swap(v)
}
func (me *UInt) Inc() uint32 {
return me.Add(1)
}
func (me *UInt) Dec() uint32 {
return 0
}
func (me *UInt) Get() uint32 {
return me.Load()
}
func (me *UInt) ToString() string {
return fmt.Sprintf("%d", me.Load())
}
type UInt16 struct {
atomic.Uint32
}
func (me *UInt16) Add(v uint16) uint16 {
value := me.Uint32.Add(uint32(v))
return uint16(value)
}
func (me *UInt16) Set(v uint16) uint16 {
return uint16(me.Swap(uint32(v)))
}
func (me *UInt16) Inc() uint16 {
return me.Add(1)
}
func (me *UInt16) Dec() uint16 {
return 0
}
func (me *UInt16) Get() uint16 {
return uint16(me.Load())
}
func (me *UInt16) ToString() string {
return fmt.Sprintf("%d", me.Get())
}
// ////////////////////////////int64////////////////////////////
type Int64 struct {
atomic.Int64
}
func (me *Int64) Set(v int64) int64 {
return me.Swap(v)
}
func (me *Int64) Inc() int64 {
return me.Add(1)
}
func (me *Int64) Dec() int64 {
return me.Add(-1)
}
func (me *Int64) Get() int64 {
return me.Load()
}
func (me *Int64) ToString() string {
return fmt.Sprintf("%d", me.Load())
}
type UInt64 struct {
atomic.Uint64
}
func (me *UInt64) Set(v uint64) uint64 {
return me.Swap(v)
}
func (me *UInt64) Inc() uint64 {
return me.Add(1)
}
func (me *UInt64) Dec() uint64 {
return 0
}
func (me *UInt64) Get() uint64 {
return me.Load()
}
func (me *UInt64) ToString() string {
return fmt.Sprintf("%d", me.Load())
}
// ////////////////////////////Bool////////////////////////////
type Bool struct {
atomic.Bool
}
func (me *Bool) Set(v bool) bool {
return me.Swap(v)
}
func (me *Bool) Get() bool {
return me.Load()
}
////////////////////////////string////////////////////////////////
type String struct {
atomic.Pointer[string]
}
func (me *String) Set(v string) string {
ptr := me.Swap(&v)
if ptr != nil {
return *ptr
}
return ""
}
func (me *String) Get() string {
return *me.Load()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xlm516/xtool.git
git@gitee.com:xlm516/xtool.git
xlm516
xtool
xtool
0222c888a734

Search