-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Make DependentHandle public #54246
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
Make DependentHandle public #54246
Changes from 29 commits
57fe91c
b1f54b5
b331b8f
a96fa3f
16fdf0e
748d88e
247fa5a
66d2ac5
4067ac3
1670339
96cfc91
6a8db56
1601d88
359938b
312851a
0145a76
4925877
1664a95
ca515b6
08df598
4e2b624
4e03297
25b34c2
01f32a3
b3963f2
34e1bcb
9fd1da4
d7146e0
c9c6325
c463d54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,259 @@ | ||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||
|
|
||||||||
| using System.Runtime.CompilerServices; | ||||||||
| #if !DEBUG | ||||||||
| using Internal.Runtime.CompilerServices; | ||||||||
| #endif | ||||||||
|
|
||||||||
| namespace System.Runtime | ||||||||
| { | ||||||||
| /// <summary> | ||||||||
| /// Represents a dependent GC handle, which will conditionally keep a dependent object instance alive | ||||||||
| /// as long as a target object instance is alive as well, without representing a strong reference to the | ||||||||
| /// target object instance. That is, a <see cref="DependentHandle"/> value with a given object instance as | ||||||||
| /// target will not cause the target to be kept alive if there are no other strong references to it, but | ||||||||
| /// it will do so for the dependent object instance as long as the target is alive. | ||||||||
| /// <para> | ||||||||
| /// This type is conceptually equivalent to having a weak reference to a given target object instance A, with | ||||||||
| /// that object having a field or property (or some other strong reference) to a dependent object instance B. | ||||||||
| /// </para> | ||||||||
| /// </summary> | ||||||||
| /// <remarks> | ||||||||
| /// The <see cref="DependentHandle"/> type is not thread-safe, and consumers are responsible for ensuring that | ||||||||
| /// <see cref="Dispose"/> is not called concurrently with other APIs. Not doing so results in undefined behavior. | ||||||||
| /// <para>The <see cref="Target"/> and <see cref="Dependent"/> properties are instead thread-safe.</para> | ||||||||
| /// </remarks> | ||||||||
| public struct DependentHandle : IDisposable | ||||||||
|
||||||||
| { | ||||||||
| // ========================================================================================= | ||||||||
| // This struct collects all operations on native DependentHandles. The DependentHandle | ||||||||
| // merely wraps an IntPtr so this struct serves mainly as a "managed typedef." | ||||||||
| // | ||||||||
| // DependentHandles exist in one of two states: | ||||||||
| // | ||||||||
| // IsAllocated == false | ||||||||
| // No actual handle is allocated underneath. Illegal to get Target, Dependent | ||||||||
| // or GetTargetAndDependent(). Ok to call Dispose(). | ||||||||
| // | ||||||||
| // Initializing a DependentHandle using the nullary ctor creates a DependentHandle | ||||||||
| // that's in the !IsAllocated state. | ||||||||
| // (! Right now, we get this guarantee for free because (IntPtr)0 == NULL unmanaged handle. | ||||||||
| // ! If that assertion ever becomes false, we'll have to add an _isAllocated field | ||||||||
| // ! to compensate.) | ||||||||
| // | ||||||||
| // | ||||||||
| // IsAllocated == true | ||||||||
| // There's a handle allocated underneath. You must call Dispose() on this eventually | ||||||||
| // or you cause a native handle table leak. | ||||||||
| // | ||||||||
| // This struct intentionally does no self-synchronization. It's up to the caller to | ||||||||
jkotas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| // to use DependentHandles in a thread-safe way. | ||||||||
| // ========================================================================================= | ||||||||
|
|
||||||||
| private IntPtr _handle; | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Initializes a new instance of the <see cref="DependentHandle"/> struct with the specified arguments. | ||||||||
| /// </summary> | ||||||||
| /// <param name="target">The target object instance to track.</param> | ||||||||
| /// <param name="dependent">The dependent object instance to associate with <paramref name="target"/>.</param> | ||||||||
| public DependentHandle(object? target, object? dependent) | ||||||||
| { | ||||||||
| // no need to check for null result: nInitialize expected to throw OOM. | ||||||||
| _handle = InternalInitialize(target, dependent); | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets a value indicating whether this handle has been allocated or not. | ||||||||
|
||||||||
| /// Gets a value indicating whether this handle has been allocated or not. | |
| /// Gets a value indicating whether this instance was constructed with | |
| /// <see cref="DependentHandle(object, object)"/> and has not yet been disposed. |
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.
IsAllocated is missing a similar thread-safety remark.
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.
Thread safety remark?
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.
The details are nice, but this is way too long for a summary, which should be at most one sentence (it's what pops up in IntelliSense for a method). Can you move everything but the first sentence to remarks, editing it appropriately? Thanks!
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.
Fixed this in c463d54, as well as all the other review comments below 🙂