abool

package module
Version: v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 1 Imported by: 199

README

ABool 💡

Go Report Card GoDoc

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

Designed for cleaner code.

Usage

import "github.com/tevino/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(new, old)  // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
cond.Toggle()           // Inverts the boolean then returns the value before inverting


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

Benchmark

  • Go 1.14.3
  • Linux 4.19.0
goos: linux
goarch: amd64

# Read
BenchmarkMutexRead-4          	86662128	        14.2 ns/op
BenchmarkAtomicValueRead-4    	1000000000	         0.755 ns/op
BenchmarkAtomicBoolRead-4     	1000000000	         0.720 ns/op  # <--- This package


# Write
BenchmarkMutexWrite-4         	76237544	        13.6 ns/op
BenchmarkAtomicValueWrite-4   	79471124	        14.9 ns/op
BenchmarkAtomicBoolWrite-4    	178218270	         6.73 ns/op  # <--- This package

# CAS
BenchmarkMutexCAS-4           	29416574	        34.7 ns/op
BenchmarkAtomicBoolCAS-4      	171900002	         7.14 ns/op  # <--- This package

# Toggle
BenchmarkMutexToggle-4        	35212117	        34.5 ns/op
BenchmarkAtomicBoolToggle-4   	169871972	         7.02 ns/op  # <--- This package

Special thanks to contributors

Documentation

Overview

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

Index

Examples

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.

Example
cond := New() // default to false
any := true
old := any
new := !any

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(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
cond.Toggle()          // Inverts the boolean then returns the value before inverting
Output:

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.2.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) 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) Toggle added in v1.1.0

func (ab *AtomicBool) Toggle() bool

Toggle inverts the Boolean then returns the value before inverting.

func (*AtomicBool) UnSet

func (ab *AtomicBool) UnSet()

UnSet sets the Boolean to false.

Source Files

Jump to

Keyboard shortcuts

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