Giter VIP home page Giter VIP logo

abool's Introduction

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/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

abool's People

Contributors

ajbouh avatar barryz avatar lucasrouckhout avatar schicho avatar tevino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

abool's Issues

Release tag

Would it be possible to cut a release tag, so that everyone has a clear version number to point to in their go.mod?

Toggle breaks SetToIf

The following piece of code demonstrates the bug.
I initially added this as a test, but was then unable to solve the bug, using only one atomic function.

func TestSetToIfAfterMultipleToggles(t *testing.T) {
	v := New() // false

	v.Toggle() // true
	v.Toggle() // false
	v.Toggle() // true

	// As v is true, it should now be flipped to false
	v.SetToIf(true, false)
	expected := false

	if v.IsSet() != expected {
		t.Fatalf("Toggling the value atleast 3 times, until it's true, `SetToIf(true, false)` should flip v to false, expected: %v, got %v", expected, v.IsSet())
	}
}

This happens, as Toggle just increases the int32 by +1 every time, but SetToIf only compares the int32 against 1 and 0 (instead of odd and even).

TestAndSet functionality

Hey, I've been using something similar to your atomicbool - adapted from https://gist.github.com/mahan/6256149

Why I think this is a valid use case

sync.Mutex doesn't allow us to examine state. There are cases where we want to examine a lock and if we cannot acquire it, just return.
Consider the scenario where multiple goroutines call a function, and only one of them needs to proceed. The others can return out immediately (like a http handler that needs to do admin work, what if the request was called multiple times, something to say "already in progress" . this atomic bool is perfect for it ).

func (s *Server) DoSomethingImportant(w http.ResponseWriter, r *http.Request) {
     // check if we can set it to true and proceed
      canProceed := s.DoSomethingImportantInProgress.TestAndSet()
      if !canProceed {
             // message that we cannot proceed
             return
      }

      defer s.DoSomethingImportantInProgress.UnSet()

      // do the rest...
}

Essentially its just the following code. Would you like me to send it as a PR instead ?

func (ab *AtomicBool) TestAndSet() bool {
      return atomic.CompareAndSwapInt32((*int32)(ab), 0, 1)
}

invalid gomod for v2

The command go mod download github.com/tevino/abool/v2 fails with the following error :

go: github.com/tevino/abool/[email protected]: go.mod has non-.../v2 module path "github.com/tevino/abool" (and .../v2/go.mod does not exist) at revision v2.0.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.