pax_global_header00006660000000000000000000000064135764112310014515gustar00rootroot0000000000000052 comment=426cdfdb52f260f02bbec020471e42d1a66b6783 golang-github-tevino-abool-0.0~git20170917.9b9efcf/000077500000000000000000000000001357641123100214625ustar00rootroot00000000000000golang-github-tevino-abool-0.0~git20170917.9b9efcf/.gitignore000066400000000000000000000004121357641123100234470ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof golang-github-tevino-abool-0.0~git20170917.9b9efcf/LICENSE000066400000000000000000000020661357641123100224730ustar00rootroot00000000000000The 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. golang-github-tevino-abool-0.0~git20170917.9b9efcf/README.md000066400000000000000000000026201357641123100227410ustar00rootroot00000000000000# 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 Go, 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: - Go 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 ``` golang-github-tevino-abool-0.0~git20170917.9b9efcf/bool.go000066400000000000000000000026121357641123100227450ustar00rootroot00000000000000// 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) } golang-github-tevino-abool-0.0~git20170917.9b9efcf/bool_test.go000066400000000000000000000051611357641123100240060ustar00rootroot00000000000000package abool import ( "sync" "sync/atomic" "testing" ) func TestBool(t *testing.T) { v := NewBool(true) if !v.IsSet() { t.Fatal("NewValue(true) failed") } v = NewBool(false) if v.IsSet() { t.Fatal("NewValue(false) failed") } v = New() if v.IsSet() { t.Fatal("Empty value of AtomicBool should be false") } v.Set() if !v.IsSet() { t.Fatal("AtomicBool.Set() failed") } v.UnSet() if v.IsSet() { t.Fatal("AtomicBool.UnSet() failed") } v.SetTo(true) if !v.IsSet() { t.Fatal("AtomicBool.SetTo(true) failed") } v.SetTo(false) if v.IsSet() { t.Fatal("AtomicBool.SetTo(false) failed") } if set := v.SetToIf(true, false); set || v.IsSet() { t.Fatal("AtomicBool.SetTo(true, false) failed") } if set := v.SetToIf(false, true); !set || !v.IsSet() { t.Fatal("AtomicBool.SetTo(false, true) failed") } } func TestRace(t *testing.T) { repeat := 10000 var wg sync.WaitGroup wg.Add(repeat * 3) v := New() // Writer go func() { for i := 0; i < repeat; i++ { v.Set() wg.Done() } }() // Reader go func() { for i := 0; i < repeat; i++ { v.IsSet() wg.Done() } }() // Writer go func() { for i := 0; i < repeat; i++ { v.UnSet() wg.Done() } }() wg.Wait() } func ExampleAtomicBool() { cond := 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 } // Benchmark Read func BenchmarkMutexRead(b *testing.B) { var m sync.RWMutex var v bool b.ResetTimer() for i := 0; i < b.N; i++ { m.RLock() _ = v m.RUnlock() } } func BenchmarkAtomicValueRead(b *testing.B) { var v atomic.Value b.ResetTimer() for i := 0; i < b.N; i++ { _ = v.Load() != nil } } func BenchmarkAtomicBoolRead(b *testing.B) { v := New() b.ResetTimer() for i := 0; i < b.N; i++ { _ = v.IsSet() } } // Benchmark Write func BenchmarkMutexWrite(b *testing.B) { var m sync.RWMutex var v bool b.ResetTimer() for i := 0; i < b.N; i++ { m.RLock() v = true m.RUnlock() } b.StopTimer() _ = v } func BenchmarkAtomicValueWrite(b *testing.B) { var v atomic.Value b.ResetTimer() for i := 0; i < b.N; i++ { v.Store(true) } } func BenchmarkAtomicBoolWrite(b *testing.B) { v := New() b.ResetTimer() for i := 0; i < b.N; i++ { v.Set() } } // Benchmark CAS func BenchmarkMutexCAS(b *testing.B) { var m sync.RWMutex var v bool b.ResetTimer() for i := 0; i < b.N; i++ { m.Lock() if !v { v = true } m.Unlock() } b.StopTimer() } func BenchmarkAtomicBoolCAS(b *testing.B) { v := New() b.ResetTimer() for i := 0; i < b.N; i++ { v.SetToIf(false, true) } }