-
Notifications
You must be signed in to change notification settings - Fork 272
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
Do not atomic decrement in drop when refcount == 1 #88
Conversation
I had this code initially the same as std's |
By thread lint you mean I need to set up a linux to check it. |
This is not correct, it's not how memory orderings work. The whole point of "release" is to make changes in memory visible to the final thread, otherwise it can cause use-after-free bugs. |
@alexcrichton sorry, I don't understand. Release should make it visible to some other thread, but there are no other threads if refcount == 1, this thread is the only thread referencing the memory. |
Ah yes I misinterpreted, but this is unfortunately still not how the memory orderings work I believe. There's some special clause or something like in the C11 standard (IIRC) that makes this exact construction work but no other. The |
Anyway, I can use I believe code is correct with |
It's not obvious to me that even using an acquire load is correct. Could you explain the the synchronization points and the happens before dependencies that the change in memory order creates in order to guarantee correctness? |
Well, I'm not 100% sure either, and I'm not an expert in C++11 memory model.
But you probably know all that yourself. |
This has a very large doc block in boost (the original source here), and I have to imagine that such an optimization would have already been conceived by the original authors. I would not personally know how to verify or deny claims that the construction proposed in this PR is safe. |
That's good point. |
Found it! Reading libc++ source code. They cannot do this optimization for strong counters, because
|
Could we reopen the issue please? |
Hmm, I can't actually re-open it for some reason. Would you mind creating an issue that references this PR to continue the discussion? |
Synthetic benchmark which becomes 5% faster (on MacBook Pro 2,2 GHz Intel Core i7) is included.