Coder Social home page Coder Social logo

tevino / abool Goto Github PK

View Code? Open in Web Editor NEW
264.0 264.0 25.0 67 KB

:bulb: Atomic Boolean library for cleaner Go code, optimized for performance yet simple to use.

License: MIT License

Go 100.00%
atomic atomicbool clean-code go golang goroutine-safe synchronization

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

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

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)
}

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.