Skip to content
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

Use one atomic operation in (*Cache).decreaseSize #7786

Merged
merged 1 commit into from
Jan 6, 2017

Conversation

mark-rushakoff
Copy link
Contributor

@mark-rushakoff mark-rushakoff commented Jan 3, 2017

The previous implementation was susceptible to a race condition (of
correctness) since c.decreaseSize is called without a lock in
(*Cache).WriteMulti.

There were already tests which asserted the correctness of the result of
decreaseSize, so no tests were added or modified.

  • Rebased/mergable
  • Tests pass
  • CHANGELOG.md updated

@mark-rushakoff mark-rushakoff added this to the 1.2.0 milestone Jan 3, 2017
@mark-rushakoff mark-rushakoff force-pushed the mr-cache-decrease-size branch 2 times, most recently from e2bebf3 to cd10f18 Compare January 3, 2017 22:28
atomic.StoreUint64(&c.size, size-delta)
// Since there isn't an explicit atomic operation for subtraction,
// use addition and overflow to effectively subtract delta from c.size.
atomic.AddUint64(&c.size, math.MaxUint64-delta+1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this is still correct, but the docs suggest a slightly shorter version:

atomic.AddUint64(&c.size, ^(delta-1))

https://golang.org/pkg/sync/atomic/#AddUint64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I overlooked that in the docs. I just pushed an amended commit to use that approach.

The previous implementation was susceptible to a race condition (of
correctness) since c.decreaseSize is called without a lock in
(*Cache).WriteMulti.

There were already tests which asserted the correctness of the result of
decreaseSize, so no tests were added or modified.
@mark-rushakoff mark-rushakoff force-pushed the mr-cache-decrease-size branch from cd10f18 to 89a587e Compare January 4, 2017 21:13
@mark-rushakoff mark-rushakoff merged commit 153277c into master Jan 6, 2017
@mark-rushakoff mark-rushakoff deleted the mr-cache-decrease-size branch January 6, 2017 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants