-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bool is not atomic #59
Conversation
This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. We will now review your pull request. |
We already have an "atomicBool" type in this file used for a different bool with a similar issue. I'd prefer to use that instead of adding a Mutex.
See |
That's fine, and I'm happy to change that, but I also think there is a genuine race between inspecting the bool and setting it (lines 112-114 of file.go). I don't think that can be solved by atomicbool. |
OK, I lie. I have extended atomicBool with compareAndSwap, which I think fixes the race and avoids the mutex. |
file.go
Outdated
@@ -107,9 +117,9 @@ func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error) { | |||
|
|||
// closeHandle closes the resources associated with a Win32 handle | |||
func (f *win32File) closeHandle() { | |||
if !f.closing { | |||
swapped := f.closing.compareAndSwap(false, true) | |||
if swapped { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be all one line.
@timou I was about to comment almost the same fix. While compareAndSwap works, as per an offline conversation with @jstarks only a swap is needed here (and is faster).
|
Could you also squash your commits into a single commit before we merge this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There's still a second (blank) merge commit. I'm good to merge if you can remove that. |
The Go race detector doesn't like concurrent access to bool. This change moves the bool to use atomicBool, and extends atomicBool with the swap() method, to handle conditional use.
OK, rebased. Thanks for the nice review! |
The Go race detector doesn't like concurrent access to bool. This change
secures the bool with a mutex.