-
-
Notifications
You must be signed in to change notification settings - Fork 632
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
Improves thread safety of lazy initializations #641
Improves thread safety of lazy initializations #641
Conversation
The Interlocked.Exchange calls will ensure that the same object is returned from each call even under a race. That's not necessarily required, but it should also make sure that no write re-ordering issues occur on platforms that allow it. And it also makes the possibility of thread safety issues explicit.
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.
Thank you for your contribution. I've only seen a bunch of style issues that need to be addressed but that's otherwise good to go.
Also, I'm not quite sure what is up with the failing test on Windows. That might be a by-product of the change, even though it looks unrelated to the PR functionality. |
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.
Updated the formatting issues. The tests do all pass when I run them locally.
Intialize it to null to match pattern for the other references. Fixes the case were the lazy initialization would break when public_key_token was Empty<byte>.Array.
The failing tests on Windows were my fault - I didn't think that breaks anything and keeps the pattern the same as the other cases. |
@scott-ferguson-unity thanks for getting this green. This is good to merge on my end. Do you want to add anything before I merge? |
No, I don't believe anything else needs to be added. |
Thanks for the PR! |
@scott-ferguson-unity , there is actually a dedicated |
@teo-tsirpanis using |
That one is much better, but I think it would need to be profiled (it uses either reflection or a lambda, depending on the overload you choose). |
The Interlocked.Exchange calls will ensure that the same object is
returned from each call even under a race. That's not necessarily
required, but it should also make sure that no write re-ordering issues
occur on platforms that allow it. And it also makes the possibility
of thread safety issues explicit.