Skip to content
Snippets Groups Projects
Commit a134b254 authored by Alessio Caiazza's avatar Alessio Caiazza Committed by Alessio Caiazza
Browse files

Updating govendor dependencies

parent 080e3f1c
Branches
Tags
No related merge requests found
The MIT License (MIT)
Copyright (c) 2016 Tevin Zhang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# ABool :bulb:
[![Go Report Card](https://goreportcard.com/badge/github.com/tevino/abool)](https://goreportcard.com/report/github.com/tevino/abool)
[![GoDoc](https://godoc.org/github.com/tevino/abool?status.svg)](https://godoc.org/github.com/tevino/abool)
Atomic Boolean library for Golang, optimized for performance yet simple to use.
Use this for cleaner code.
## Usage
```go
import "github.com/tevino/abool"
cond := abool.New() // default to false
cond.Set() // Set to true
cond.IsSet() // Returns true
cond.UnSet() // Set to false
cond.SetTo(true) // Set to whatever you want
cond.SetToIf(false, true) // Set to true if it is false, returns false(not set)
// embedding
type Foo struct {
cond *abool.AtomicBool // always use pointer to avoid copy
}
```
## Benchmark:
- Golang 1.6.2
- OS X 10.11.4
```shell
# Read
BenchmarkMutexRead-4 100000000 21.0 ns/op
BenchmarkAtomicValueRead-4 200000000 6.30 ns/op
BenchmarkAtomicBoolRead-4 300000000 4.21 ns/op # <--- This package
# Write
BenchmarkMutexWrite-4 100000000 21.6 ns/op
BenchmarkAtomicValueWrite-4 30000000 43.4 ns/op
BenchmarkAtomicBoolWrite-4 200000000 9.87 ns/op # <--- This package
# CAS
BenchmarkMutexCAS-4 30000000 44.9 ns/op
BenchmarkAtomicBoolCAS-4 100000000 11.7 ns/op # <--- This package
```
// Package abool provides atomic Boolean type for cleaner code and
// better performance.
package abool
import "sync/atomic"
// New creates an AtomicBool with default to false
func New() *AtomicBool {
return new(AtomicBool)
}
// NewBool creates an AtomicBool with given default value
func NewBool(ok bool) *AtomicBool {
ab := New()
if ok {
ab.Set()
}
return ab
}
// 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
type AtomicBool int32
// Set sets the Boolean to true
func (ab *AtomicBool) Set() {
atomic.StoreInt32((*int32)(ab), 1)
}
// UnSet sets the Boolean to false
func (ab *AtomicBool) UnSet() {
atomic.StoreInt32((*int32)(ab), 0)
}
// IsSet returns whether the Boolean is true
func (ab *AtomicBool) IsSet() bool {
return atomic.LoadInt32((*int32)(ab)) == 1
}
// SetTo sets the boolean with given Boolean
func (ab *AtomicBool) SetTo(yes bool) {
if yes {
atomic.StoreInt32((*int32)(ab), 1)
} else {
atomic.StoreInt32((*int32)(ab), 0)
}
}
// SetToIf sets the Boolean to new only if the Boolean matches the old
// Returns whether the set was done
func (ab *AtomicBool) SetToIf(old, new bool) (set bool) {
var o, n int32
if old {
o = 1
}
if new {
n = 1
}
return atomic.CompareAndSwapInt32((*int32)(ab), o, n)
}
...@@ -901,6 +901,12 @@ ...@@ -901,6 +901,12 @@
"revision": "b1f989447a57594c728884458a39abf3a73447f7", "revision": "b1f989447a57594c728884458a39abf3a73447f7",
"revisionTime": "2017-07-13T17:13:24Z" "revisionTime": "2017-07-13T17:13:24Z"
}, },
{
"checksumSHA1": "gHQ2dBOhcQ77GlEtXSOOkoV7e7A=",
"path": "github.com/tevino/abool",
"revision": "3c25f2fe7cd0ef3eabefce1d90efd69a65d35b12",
"revisionTime": "2016-06-28T10:11:33Z"
},
{ {
"checksumSHA1": "VDdgQz6FoZx6+4WrfrnrpBJIaDU=", "checksumSHA1": "VDdgQz6FoZx6+4WrfrnrpBJIaDU=",
"path": "github.com/ugorji/go/codec", "path": "github.com/ugorji/go/codec",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment