atomic

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2021 License: BSD-3-Clause Imports: 1 Imported by: 0

README

atomic

Atomic operations as methods on types

Go Reference Build Status

example usage

package main

import (
	"fmt"

	"github.com/nightlyone/atomic"
)

type Service struct {
	Health atomic.Bool
}

func main() {
	service := new(Service)
	isHealthy := service.Health.Value()
	fmt.Printf("service is healthy? %t\n", isHealthy)
	// Output: service is healthy? false
}

LICENSE

BSD-3-Clause

Contributing

Pull requests and github issues welcome.

Documentation

Overview

Package atomic implements atomic operations as methods on types so they can be used more easily.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	// contains filtered or unexported fields
}

Bool is an atomic boolean. It can be flipped from known states to newly expected states and queries. A Bool can be created as part of other structures. The zero value for a Bool is false. A Bool must not be copied after first use.

Example
package main

import (
	"fmt"

	"github.com/nightlyone/atomic"
)

// Service is a flaky service.
type Service struct {
	health atomic.Bool
}

func main() {
	service := new(Service)
	isHealthy := service.health.Value()
	fmt.Printf("service is healthy? %t\n", isHealthy)
}
Output:

service is healthy? false

func NewBool

func NewBool(value bool) *Bool

NewBool returns a new atomic boolean set inititially to value.

func (*Bool) CompareAndSwap

func (b *Bool) CompareAndSwap(old, new bool) (swapped bool)

CompareAndSwap executes the compare-and-swap operation on bool, which is the atomic equivalent of:

if *b == old {
	*b = new
	return true
}
return false

func (*Bool) Value

func (b *Bool) Value() bool

Value returns the current boolean value.

type Int32

type Int32 struct {
	// contains filtered or unexported fields
}

Int32 provides AddInt32, StoreInt32, LoadInt32 and CompareAndSwapInt32 from the package sync/atomic as convenient methods on a type. An Int32 can be created as part of other structures. The zero value for an Int32 is 0. An Int32 must not be copied after first use.

func NewInt32

func NewInt32(value int32) *Int32

NewInt32 returns a new atomic int32 set inititially to value.

func (*Int32) Add

func (i32 *Int32) Add(delta int32) int32

Add is the atomic equivalent of:

*i32 += delta
return *i32

func (*Int32) CompareAndSwap

func (i32 *Int32) CompareAndSwap(old, new int32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation on i32, which is the atomic equivalent of:

if *i32 == old {
	*i32 = new
	return true
}
return false

func (*Int32) Load

func (i32 *Int32) Load() int32

Load is the atomic equivalent of:

return *i32

func (*Int32) Store

func (i32 *Int32) Store(value int32)

Store is the atomic equivalent of:

*i32 = value

type Int64

type Int64 struct {
	// contains filtered or unexported fields
}

Int64 provides AddInt64, StoreInt64, LoadInt64 and CompareAndSwapInt64 from the package sync/atomic as convenient methods on a type. An Int64 can be created as part of other structures. The zero value for an Int64 is 0. An Int64 must not be copied after first use.

func NewInt64

func NewInt64(value int64) *Int64

NewInt64 returns a new atomic int64 set inititially to value.

func (*Int64) Add

func (i64 *Int64) Add(delta int64) int64

Add is the atomic equivalent of:

*i64 += delta
return *i64

func (*Int64) CompareAndSwap

func (i64 *Int64) CompareAndSwap(old, new int64) (swapped bool)

CompareAndSwap executes the compare-and-swap operation on i64, which is the atomic equivalent of:

if *i64 == old {
	*i64 = new
	return true
}
return false

func (*Int64) Load

func (i64 *Int64) Load() int64

Load is the atomic equivalent of:

return *i64

func (*Int64) Store

func (i64 *Int64) Store(value int64)

Store is the atomic equivalent of:

*i64 = value

type Uint32

type Uint32 struct {
	// contains filtered or unexported fields
}

Uint32 provides AddUint32, StoreUint32, LoadUint32 and CompareAndSwapUint32 from the package sync/atomic as convenient methods on a type. An Uint32 can be created as part of other structures. The zero value for an Uint32 is 0. An Uint32 must not be copied after first use.

func NewUint32

func NewUint32(value uint32) *Uint32

NewUint32 returns a new atomic uint32 set inititially to value.

func (*Uint32) Add

func (u32 *Uint32) Add(delta uint32) uint32

Add is the atomic equivalent of:

*u32 += delta
return *u32

func (*Uint32) CompareAndSwap

func (u32 *Uint32) CompareAndSwap(old, new uint32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation on u32, which is the atomic equivalent of:

if *u32 == old {
	*u32 = new
	return true
}
return false

func (*Uint32) Load

func (u32 *Uint32) Load() uint32

Load is the atomic equivalent of:

return *u32

func (*Uint32) Store

func (u32 *Uint32) Store(value uint32)

Store is the atomic equivalent of:

*u32 = value

type Uint64

type Uint64 struct {
	// contains filtered or unexported fields
}

Uint64 provides AddUint64, StoreUint64, LoadUint64 and CompareAndSwapUint64 from the package sync/atomic as convenient methods on a type. An Uint64 can be created as part of other structures. The zero value for an Uint64 is 0. An Uint64 must not be copied after first use.

func NewUint64

func NewUint64(value uint64) *Uint64

NewUint64 returns a new atomic uint64 set inititially to value.

func (*Uint64) Add

func (u64 *Uint64) Add(delta uint64) uint64

Add is the atomic equivalent of:

*u64 += delta
return *u64

func (*Uint64) CompareAndSwap

func (u64 *Uint64) CompareAndSwap(old, new uint64) (swapped bool)

CompareAndSwap executes the compare-and-swap operation on u64, which is the atomic equivalent of:

if *u64 == old {
	*u64 = new
	return true
}
return false

func (*Uint64) Load

func (u64 *Uint64) Load() uint64

Load is the atomic equivalent of:

return *u64

func (*Uint64) Store

func (u64 *Uint64) Store(value uint64)

Store is the atomic equivalent of:

*u64 = value

Notes

Bugs

  • The implementation just wraps sync/atomic from the Go standard library, so it suffers the same implementation limitations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL