Improves thread safety of lazy initializations #641

Merged
scott-ferguson-unity merged 3 commits from improve-thread-safety into master 2020-01-28 10:44:26 -06:00
scott-ferguson-unity commented 2020-01-24 08:59:09 -06:00 (Migrated from github.com)

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.

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.
jbevain (Migrated from github.com) requested changes 2020-01-24 11:41:57 -06:00
jbevain (Migrated from github.com) left a comment

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.

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.
@ -53,7 +53,12 @@ namespace Mono.Cecil.Cil {
}
jbevain (Migrated from github.com) commented 2020-01-24 11:30:14 -06:00

Nit pick: please add a space before the opening parentheses in new InstructionCollection ( to match the overall codestyle.

Nit pick: please add a space before the opening parentheses in `new InstructionCollection (` to match the overall codestyle.
jbevain (Migrated from github.com) commented 2020-01-24 11:30:29 -06:00

nit: same.

nit: same.
@ -144,7 +158,7 @@ namespace Mono.Cecil {
if (!HasImage || signature == 0)
jbevain (Migrated from github.com) commented 2020-01-24 11:35:34 -06:00

Nit: indent looks wrong (and uses spaces instead of tabs)

Nit: indent looks wrong (and uses spaces instead of tabs)
@ -19,7 +20,13 @@ namespace Mono.Collections.Generic {
static ReadOnlyCollection<T> empty;
jbevain (Migrated from github.com) commented 2020-01-24 11:38:17 -06:00

Nit: should be on the get line.

Nit: should be on the get line.
jbevain commented 2020-01-24 11:43:07 -06:00 (Migrated from github.com)

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.

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.
scott-ferguson-unity (Migrated from github.com) reviewed 2020-01-24 12:10:50 -06:00
@ -53,7 +53,12 @@ namespace Mono.Cecil.Cil {
}
scott-ferguson-unity (Migrated from github.com) commented 2020-01-24 12:10:50 -06:00

updated

updated
scott-ferguson-unity (Migrated from github.com) reviewed 2020-01-24 12:10:56 -06:00
@ -53,7 +53,12 @@ namespace Mono.Cecil.Cil {
}
scott-ferguson-unity (Migrated from github.com) commented 2020-01-24 12:10:56 -06:00

updated

updated
scott-ferguson-unity (Migrated from github.com) reviewed 2020-01-24 12:11:00 -06:00
@ -144,7 +158,7 @@ namespace Mono.Cecil {
if (!HasImage || signature == 0)
scott-ferguson-unity (Migrated from github.com) commented 2020-01-24 12:11:00 -06:00

updated

updated
scott-ferguson-unity (Migrated from github.com) reviewed 2020-01-24 12:11:07 -06:00
@ -19,7 +20,13 @@ namespace Mono.Collections.Generic {
static ReadOnlyCollection<T> empty;
scott-ferguson-unity (Migrated from github.com) commented 2020-01-24 12:11:07 -06:00

updated

updated
scott-ferguson-unity (Migrated from github.com) reviewed 2020-01-24 12:11:36 -06:00
scott-ferguson-unity (Migrated from github.com) left a comment

Updated the formatting issues. The tests do all pass when I run them locally.

Updated the formatting issues. The tests do all pass when I run them locally.
jbevain (Migrated from github.com) approved these changes 2020-01-24 12:14:05 -06:00
scott-ferguson-unity commented 2020-01-27 09:40:50 -06:00 (Migrated from github.com)

The failing tests on Windows were my fault - AssemblyNameReference.public_key_token would sometimes be Array<byte>.Empty instead of null. I updated so the initialized value is null.

I didn't think that breaks anything and keeps the pattern the same as the other cases.

The failing tests on Windows were my fault - `AssemblyNameReference.public_key_token` would sometimes be `Array<byte>.Empty` instead of null. I updated so the initialized value is `null`. I didn't think that breaks anything and keeps the pattern the same as the other cases.
jbevain commented 2020-01-27 12:19:34 -06:00 (Migrated from github.com)

@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?

@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?
scott-ferguson-unity commented 2020-01-27 12:53:17 -06:00 (Migrated from github.com)

No, I don't believe anything else needs to be added.

No, I don't believe anything else needs to be added.
jbevain commented 2020-01-28 10:44:33 -06:00 (Migrated from github.com)

Thanks for the PR!

Thanks for the PR!
teo-tsirpanis commented 2020-01-31 06:10:44 -06:00 (Migrated from github.com)

@scott-ferguson-unity , there is actually a dedicated Lazy class for that which is thread-safe and does the exact thing. This PR brought unnecessary code duplication.

@scott-ferguson-unity , there is actually a [dedicated `Lazy` class](https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1) for that which is thread-safe and does the exact thing. This PR brought unnecessary code duplication.
ltrzesniewski commented 2020-01-31 07:04:15 -06:00 (Migrated from github.com)

@teo-tsirpanis using Lazy<T> would cause increased GC pressure, which I guess would quickly become a problem given these are hot code paths.

@teo-tsirpanis using `Lazy<T>` would cause increased GC pressure, which I guess would quickly become a problem given these are hot code paths.
teo-tsirpanis commented 2020-01-31 07:12:25 -06:00 (Migrated from github.com)
[`LazyInitializer.EnsureInitialized`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.lazyinitializer.ensureinitialized)
ltrzesniewski commented 2020-01-31 07:25:10 -06:00 (Migrated from github.com)

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).

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).
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-jbevain/cecil!641
No description provided.