💡 Atomic Boolean library for cleaner Go code, optimized for performance yet simple to use.
Go to file
re 6b20e088c6 Update 'v2/go.mod' 2023-11-25 11:51:18 +03:00
v2 Update 'v2/go.mod' 2023-11-25 11:51:18 +03:00
.gitignore Initial commit 2016-05-25 11:28:42 +08:00
LICENSE Initial commit 2016-05-25 11:28:42 +08:00
README.md fix 2023-01-18 21:57:25 +03:00
bool.go remove unnecessary casts 2021-09-05 17:56:57 +02:00
bool_test.go reintroduce Toggle() 2021-09-05 17:29:37 +02:00
go.mod Update 'go.mod' 2023-11-25 11:50:47 +03:00

README.md

ABool 💡

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

Designed for cleaner code.

Usage

import "git.internal/re/abool/v2"

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
cond.Toggle()           // Flip the value of `cond`, returns the value before flipping


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

Benchmark

go: v1.18.2
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz

# Read
BenchmarkMutexRead-12           	100000000	        10.24   ns/op
BenchmarkAtomicValueRead-12     	1000000000	         0.4690 ns/op
BenchmarkAtomicBoolRead-12      	1000000000	         0.2345 ns/op  # <--- This package

# Write
BenchmarkMutexWrite-12          	100000000	        10.19  ns/op
BenchmarkAtomicValueWrite-12    	164918696	         7.235 ns/op
BenchmarkAtomicBoolWrite-12     	278729533	         4.341 ns/op  # <--- This package

# CAS
BenchmarkMutexCAS-12            	57333123	        20.26  ns/op
BenchmarkAtomicBoolCAS-12       	203575494	         5.755 ns/op  # <--- This package

# Toggle
BenchmarkAtomicBoolToggle-12    	145249862	         8.196 ns/op  # <--- This package

Special thanks to contributors

  • barryz
    • Added the Toggle method
  • Lucas Rouckhout
    • Implemented JSON Unmarshal and Marshal interface
  • Sebastian Schicho
    • Reported a regression with test case
    • Hide the underlying int
    • Reintroduced the Toggle method