abool

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: MIT Imports: 2 Imported by: 0

README

ABool 💡

Atomic Boolean package for Go, optimized for performance yet simple to use.

Designed for cleaner code.

Usage

package abool


cond := abool.New()     // default to false

cond.Set()              // Sets to true
cond.IsSet()            // Returns true
cond.UnSet()            // Sets to false
cond.IsNotSet()         // Returns true
cond.SetTo(any)         // Sets to whatever you want
cond.SetToIf(old, new)  // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded


// embedding
type Foo struct {
    cond *abool.AtomicBool  // always use pointer to avoid copy
}

Benchmark:

  • Go 1.6.2
  • OS X 10.11.4
  • Intel CPU (to be specified)
# Read
BenchmarkMutexRead-48                   656360493                8.691 ns/op           0 B/op          0 allocs/op
BenchmarkAtomicValueRead-48             1000000000               0.2631 ns/op          0 B/op          0 allocs/op
BenchmarkAtomicBoolRead-48              1000000000               0.2515 ns/op          0 B/op          0 allocs/op

# Write
BenchmarkMutexWrite-48                  693644023                8.755 ns/op           0 B/op          0 allocs/op
BenchmarkAtomicValueWrite-48            980383506                5.572 ns/op           0 B/op          0 allocs/op
BenchmarkAtomicBoolWrite-48             1000000000               4.468 ns/op           0 B/op          0 allocs/op

# CAS
BenchmarkMutexCAS-48                    316539304               18.47 ns/op            0 B/op          0 allocs/op
BenchmarkAtomicBoolCAS-48               1000000000               4.179 ns/op           0 B/op          0 allocs/op




Special thanks to contributors

Documentation

Overview

Package abool provides atomic Boolean type for cleaner code and better performance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBool

type AtomicBool int32

AtomicBool is an atomic Boolean. Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously. Note: When embedding into a struct one should always use *AtomicBool to avoid copy.

func New

func New() *AtomicBool

New creates an AtomicBool with default set to false.

func NewBool

func NewBool(ok bool) *AtomicBool

NewBool creates an AtomicBool with given default value.

func (*AtomicBool) IsNotSet added in v1.3.0

func (ab *AtomicBool) IsNotSet() bool

IsNotSet returns whether the Boolean is false.

func (*AtomicBool) IsSet

func (ab *AtomicBool) IsSet() bool

IsSet returns whether the Boolean is true.

func (*AtomicBool) MarshalJSON added in v1.3.0

func (ab *AtomicBool) MarshalJSON() ([]byte, error)

MarshalJSON behaves the same as if the AtomicBool is a builtin.bool. NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.

func (*AtomicBool) Set

func (ab *AtomicBool) Set()

Set sets the Boolean to true.

func (*AtomicBool) SetTo

func (ab *AtomicBool) SetTo(yes bool)

SetTo sets the boolean with given Boolean.

func (*AtomicBool) SetToIf

func (ab *AtomicBool) SetToIf(old, new bool) (set bool)

SetToIf sets the Boolean to new only if the Boolean matches the old. Returns whether the set was done.

func (*AtomicBool) UnSet

func (ab *AtomicBool) UnSet()

UnSet sets the Boolean to false.

func (*AtomicBool) UnmarshalJSON added in v1.3.0

func (ab *AtomicBool) UnmarshalJSON(b []byte) error

UnmarshalJSON behaves the same as if the AtomicBool is a builtin.bool. NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.

Jump to

Keyboard shortcuts

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