-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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 1 commit
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,100 @@ | ||
// 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; | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
internal struct Ephemeron | ||
{ | ||
public object? key; | ||
public object? value; | ||
} | ||
} | ||
|
||
namespace System.Runtime | ||
{ | ||
// | ||
// Instead of dependent handles, mono uses arrays of Ephemeron objects. | ||
// | ||
internal struct DependentHandle | ||
public struct DependentHandle : IDisposable | ||
{ | ||
private Ephemeron[] data; | ||
|
||
public DependentHandle(object? primary, object? secondary) | ||
public DependentHandle(object? target, object? dependent) | ||
{ | ||
data = new Ephemeron[1]; | ||
data[0].key = primary; | ||
data[0].value = secondary; | ||
data[0].key = target; | ||
data[0].value = dependent; | ||
GC.register_ephemeron_array(data); | ||
} | ||
|
||
public bool IsAllocated => data != null; | ||
|
||
// Getting the secondary object is more expensive than getting the first so | ||
// we provide a separate primary-only accessor for those times we only want the | ||
// primary. | ||
public object? GetPrimary() | ||
public object? Target | ||
{ | ||
get => GetTarget(); | ||
set => SetTarget(value); | ||
} | ||
|
||
public object? Dependent | ||
{ | ||
get => GetDependent(); | ||
set => SetDependent(value); | ||
} | ||
|
||
public (object? Target, object? Dependent) GetTargetAndDependent() | ||
{ | ||
if (!IsAllocated) | ||
throw new NotSupportedException(); | ||
if (data[0].key == GC.EPHEMERON_TOMBSTONE) | ||
return null; | ||
return data[0].key; | ||
{ | ||
return default; | ||
} | ||
return (data[0].key, data[0].value); | ||
} | ||
|
||
public object? GetPrimaryAndSecondary(out object? secondary) | ||
public void Dispose() | ||
{ | ||
data = null!; | ||
} | ||
|
||
private object? GetTarget() | ||
{ | ||
// Getting the secondary object is more expensive than getting the first so | ||
// we provide a separate primary-only accessor for those times we only want the | ||
// primary. | ||
|
||
|
||
if (!IsAllocated) | ||
throw new NotSupportedException(); | ||
if (data[0].key == GC.EPHEMERON_TOMBSTONE) | ||
{ | ||
secondary = null; | ||
return null; | ||
} | ||
secondary = data[0].value; | ||
return data[0].key; | ||
} | ||
|
||
public void SetPrimary(object? primary) | ||
private void SetTarget(object? primary) | ||
{ | ||
if (!IsAllocated) | ||
throw new NotSupportedException(); | ||
data[0].key = primary; | ||
} | ||
|
||
public void SetSecondary(object? secondary) | ||
private object? GetDependent() | ||
{ | ||
if (!IsAllocated) | ||
throw new NotSupportedException(); | ||
data[0].value = secondary; | ||
if (data[0].key == GC.EPHEMERON_TOMBSTONE) | ||
{ | ||
return null; | ||
} | ||
return data[0].value; | ||
} | ||
|
||
public void Free() | ||
private void SetDependent(object? secondary) | ||
{ | ||
data = null!; | ||
if (!IsAllocated) | ||
throw new NotSupportedException(); | ||
data[0].value = secondary; | ||
} | ||
} | ||
} |
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.
Should this be
UnsafeGetDependent
for consistency?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 as part of 64bad6418ff202c60862eabf22468b508dd662f3.