2016-06-01 13:38:57 +03:00
|
|
|
# ABool :bulb:
|
2020-07-16 08:10:44 +03:00
|
|
|
|
2023-01-18 21:52:46 +03:00
|
|
|
[![Go Report Card](https://goreportcard.com/badge/git.internal/re)](https://goreportcard.com/report/git.internal/re)
|
|
|
|
[![GoDoc](https://godoc.org/git.internal/re?status.svg)](https://godoc.org/git.internal/re)
|
2016-05-25 07:00:32 +03:00
|
|
|
|
2020-07-16 09:02:27 +03:00
|
|
|
Atomic Boolean package for Go, optimized for performance yet simple to use.
|
2016-05-25 06:42:19 +03:00
|
|
|
|
2020-07-16 09:02:27 +03:00
|
|
|
Designed for cleaner code.
|
2016-05-25 07:06:29 +03:00
|
|
|
|
2016-05-25 06:49:42 +03:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
```go
|
2023-01-18 21:52:46 +03:00
|
|
|
import "git.internal/re/v2"
|
2016-05-25 06:49:42 +03:00
|
|
|
|
2020-07-16 08:35:52 +03:00
|
|
|
cond := abool.New() // default to false
|
|
|
|
|
2020-07-16 08:57:01 +03:00
|
|
|
cond.Set() // Sets to true
|
2020-07-16 08:35:52 +03:00
|
|
|
cond.IsSet() // Returns true
|
2020-07-16 08:57:01 +03:00
|
|
|
cond.UnSet() // Sets to false
|
2020-07-16 09:46:58 +03:00
|
|
|
cond.IsNotSet() // Returns true
|
2020-07-16 08:57:01 +03:00
|
|
|
cond.SetTo(any) // Sets to whatever you want
|
2021-09-12 01:45:43 +03:00
|
|
|
cond.SetToIf(old, new) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
2022-05-30 16:42:03 +03:00
|
|
|
cond.Toggle() // Flip the value of `cond`, returns the value before flipping
|
2016-05-25 06:49:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
// embedding
|
|
|
|
type Foo struct {
|
|
|
|
cond *abool.AtomicBool // always use pointer to avoid copy
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-30 16:46:49 +03:00
|
|
|
## Benchmark
|
2016-05-25 06:42:19 +03:00
|
|
|
|
2021-09-05 07:25:08 +03:00
|
|
|
```
|
2022-05-30 16:46:49 +03:00
|
|
|
go: v1.18.2
|
|
|
|
goos: darwin
|
|
|
|
goarch: amd64
|
|
|
|
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
|
|
|
|
|
2016-05-25 06:42:19 +03:00
|
|
|
# Read
|
2022-05-31 06:15:25 +03:00
|
|
|
BenchmarkMutexRead-12 100000000 10.24 ns/op
|
2022-05-30 16:46:49 +03:00
|
|
|
BenchmarkAtomicValueRead-12 1000000000 0.4690 ns/op
|
|
|
|
BenchmarkAtomicBoolRead-12 1000000000 0.2345 ns/op # <--- This package
|
2016-05-25 06:42:19 +03:00
|
|
|
|
|
|
|
# Write
|
2022-05-30 16:46:49 +03:00
|
|
|
BenchmarkMutexWrite-12 100000000 10.19 ns/op
|
|
|
|
BenchmarkAtomicValueWrite-12 164918696 7.235 ns/op
|
|
|
|
BenchmarkAtomicBoolWrite-12 278729533 4.341 ns/op # <--- This package
|
2016-06-02 06:47:41 +03:00
|
|
|
|
|
|
|
# CAS
|
2022-05-30 16:46:49 +03:00
|
|
|
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
|
2016-05-25 06:42:19 +03:00
|
|
|
```
|
2020-07-07 10:47:35 +03:00
|
|
|
|
2020-07-16 08:10:44 +03:00
|
|
|
## Special thanks to contributors
|
2020-07-07 10:47:35 +03:00
|
|
|
|
2021-09-05 07:46:55 +03:00
|
|
|
- [barryz](https://github.com/barryz)
|
2020-07-16 08:10:44 +03:00
|
|
|
- Added the `Toggle` method
|
2021-09-05 07:46:55 +03:00
|
|
|
- [Lucas Rouckhout](https://github.com/LucasRouckhout)
|
2021-05-08 18:54:15 +03:00
|
|
|
- Implemented JSON Unmarshal and Marshal interface
|
2021-09-05 07:47:57 +03:00
|
|
|
- [Sebastian Schicho](https://github.com/schicho)
|
|
|
|
- Reported a regression with test case
|
2022-05-31 06:32:12 +03:00
|
|
|
- Hide the underlying `int`
|
2022-05-30 16:42:03 +03:00
|
|
|
- Reintroduced the `Toggle` method
|