This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move parts of WeakReference to shared partition (#24800)
- Loading branch information
1 parent
c690dae
commit 2b08a11
Showing
6 changed files
with
102 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace System | ||
{ | ||
[Serializable] | ||
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
public partial class WeakReference : ISerializable | ||
{ | ||
// If you fix bugs here, please fix them in WeakReference<T> at the same time. | ||
|
||
// Creates a new WeakReference that keeps track of target. | ||
// Assumes a Short Weak Reference (ie TrackResurrection is false.) | ||
// | ||
public WeakReference(object? target) | ||
: this(target, false) | ||
{ | ||
} | ||
|
||
public WeakReference(object? target, bool trackResurrection) | ||
{ | ||
Create(target, trackResurrection); | ||
} | ||
|
||
protected WeakReference(SerializationInfo info, StreamingContext context) | ||
{ | ||
if (info == null) | ||
{ | ||
throw new ArgumentNullException(nameof(info)); | ||
} | ||
|
||
object? target = info.GetValue("TrackedObject", typeof(object)); // Do not rename (binary serialization) | ||
bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization) | ||
|
||
Create(target, trackResurrection); | ||
} | ||
|
||
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
if (info == null) | ||
{ | ||
throw new ArgumentNullException(nameof(info)); | ||
} | ||
info.AddValue("TrackedObject", Target, typeof(object)); // Do not rename (binary serialization) | ||
info.AddValue("TrackResurrection", IsTrackResurrection()); // Do not rename (binary serialization) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/System.Private.CoreLib/src/System/WeakReference.T.CoreCLR.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.Serialization; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System | ||
{ | ||
public sealed partial class WeakReference<T> : ISerializable | ||
where T : class? | ||
{ | ||
// This field is not a regular GC handle. It can have a special values that are used to prevent a race condition between setting the target and finalization. | ||
internal IntPtr m_handle; | ||
|
||
public void SetTarget(T target) | ||
{ | ||
this.Target = target; | ||
} | ||
|
||
// This is property for better debugging experience (VS debugger shows values of properties when you hover over the variables) | ||
private extern T Target | ||
{ | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
get; | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
set; | ||
} | ||
|
||
// Free all system resources associated with this reference. | ||
// | ||
// Note: The WeakReference<T> finalizer is not usually run, but | ||
// treated specially in gc.cpp's ScanForFinalization | ||
// This is needed for subclasses deriving from WeakReference<T>, however. | ||
// Additionally, there may be some cases during shutdown when we run this finalizer. | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
extern ~WeakReference(); | ||
|
||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
private extern void Create(T target, bool trackResurrection); | ||
|
||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
private extern bool IsTrackResurrection(); | ||
} | ||
} |