Skip to content

Make sure that counter is decremented even when Arc::clone panics - #4

Open
Kritzefitz wants to merge 1 commit into
w0xel:mainfrom
Kritzefitz:fix/decrement-count-on-panic
Open

Make sure that counter is decremented even when Arc::clone panics#4
Kritzefitz wants to merge 1 commit into
w0xel:mainfrom
Kritzefitz:fix/decrement-count-on-panic

Conversation

@Kritzefitz

Copy link
Copy Markdown

This handles the case discussed in #3 where an implementation of Arc::clone that panics could cause the count to increase more than it is decreased, eventually causing an overflow. To ensure the counter is decremented even in case of panics, I use scopeguard::defer.

This of course implies a dependency on the scopeguard crate. If you don't want this, I've also made another version of this fix without the dependency on scopeguard. It is of course considerably more verbose and awkwardly tries to separate the counter into it's own struct, but the separation breaks down, because the counter can't really know which orderings the surrounding code needs, so now there are a bunch of Ordering parameters all over the place. Overall, feel free to merge this, or take inspiration from this or the other solution to write your own solution.

A third option would be to use the CAS loop I've mentioned in #1. That should be safe in all cases, even the practically impossible case that there are more than usize::MAX simultaneous threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant