+56
-19
lines changedFilter options
+56
-19
lines changed Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
1
+
// Originally from: https://github.com/mono/mono/blob/8266c5604b8c03882f2b06af27fdea46b142d6b9/mono/mini/TestHelpers.cs#L12
2
+
3
+
using System;
4
+
using System.Threading;
5
+
6
+
namespace Java.InteropTests
7
+
{
8
+
// False pinning cases are still possible. For example the thread can die
9
+
// and its stack reused by another thread. It also seems that a thread that
10
+
// does a GC can keep on the stack references to objects it encountered
11
+
// during the collection which are never released afterwards. This would
12
+
// be more likely to happen with the interpreter which reuses more stack.
13
+
static class FinalizerHelpers
14
+
{
15
+
private static IntPtr aptr;
16
+
17
+
private static unsafe void NoPinActionHelper (int depth, Action act)
18
+
{
19
+
// Avoid tail calls
20
+
int* values = stackalloc int [20];
21
+
aptr = new IntPtr (values);
22
+
23
+
if (depth <= 0) {
24
+
//
25
+
// When the action is called, this new thread might have not allocated
26
+
// anything yet in the nursery. This means that the address of the first
27
+
// object that would be allocated would be at the start of the tlab and
28
+
// implicitly the end of the previous tlab (address which can be in use
29
+
// when allocating on another thread, at checking if an object fits in
30
+
// this other tlab). We allocate a new dummy object to avoid this type
31
+
// of false pinning for most common cases.
32
+
//
33
+
new object ();
34
+
act ();
35
+
} else {
36
+
NoPinActionHelper (depth - 1, act);
37
+
}
38
+
}
39
+
40
+
public static void PerformNoPinAction (Action act)
41
+
{
42
+
Thread thr = new Thread (() => NoPinActionHelper (128, act));
43
+
thr.Start ();
44
+
thr.Join ();
45
+
}
46
+
}
47
+
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
1
1
using System;
2
-
using System.Threading;
3
2
4
3
using Java.Interop;
5
4
@@ -18,13 +17,11 @@ public void JavaReferencedInstanceSurvivesCollection ()
18
17
using (var t = new JniType ("java/lang/Object")) {
19
18
var oldHandle = IntPtr.Zero;
20
19
var array = new JavaObjectArray<JavaObject> (1);
21
-
var w = new Thread (() => {
20
+
FinalizerHelpers.PerformNoPinAction (() => {
22
21
var v = new JavaObject ();
23
22
oldHandle = v.PeerReference.Handle;
24
23
array [0] = v;
25
24
});
26
-
w.Start ();
27
-
w.Join ();
28
25
JniEnvironment.Runtime.ValueManager.CollectPeers ();
29
26
GC.WaitForPendingFinalizers ();
30
27
GC.WaitForPendingFinalizers ();
@@ -80,13 +77,11 @@ public void UnreferencedInstanceIsCollected ()
80
77
{
81
78
JniObjectReference oldHandle = new JniObjectReference ();
82
79
WeakReference r = null;
83
-
var t = new Thread (() => {
80
+
FinalizerHelpers.PerformNoPinAction (() => {
84
81
var v = new JavaObject ();
85
82
oldHandle = v.PeerReference.NewWeakGlobalRef ();
86
83
r = new WeakReference (v);
87
84
});
88
-
t.Start ();
89
-
t.Join ();
90
85
JniEnvironment.Runtime.ValueManager.CollectPeers ();
91
86
GC.WaitForPendingFinalizers ();
92
87
GC.WaitForPendingFinalizers ();
@@ -110,20 +105,17 @@ public void Dispose ()
110
105
111
106
#if !NO_GC_BRIDGE_SUPPORT
112
107
[Test]
113
-
// See: https://github.com/dotnet/runtime/issues/60638
114
-
[Category ("IgnoreInterpreter")]
115
108
public void Dispose_Finalized ()
116
109
{
117
110
var d = false;
118
111
var f = false;
119
-
var t = new Thread (() => {
120
-
var v = new JavaDisposedObject (() => d = true, () => f = true);
121
-
GC.KeepAlive (v);
112
+
FinalizerHelpers.PerformNoPinAction (() => {
113
+
FinalizerHelpers.PerformNoPinAction (() => {
114
+
var v = new JavaDisposedObject (() => d = true, () => f = true);
115
+
GC.KeepAlive (v);
116
+
});
117
+
JniEnvironment.Runtime.ValueManager.CollectPeers ();
122
118
});
123
-
t.Start ();
124
-
t.Join ();
125
-
JniEnvironment.Runtime.ValueManager.CollectPeers ();
126
-
GC.WaitForPendingFinalizers ();
127
119
JniEnvironment.Runtime.ValueManager.CollectPeers ();
128
120
GC.WaitForPendingFinalizers ();
129
121
Assert.IsFalse (d);
@@ -185,11 +177,9 @@ public void Ctor_Exceptions ()
185
177
public void CrossThreadSharingRequresRegistration ()
186
178
{
187
179
JavaObject o = null;
188
-
var t = new Thread (() => {
180
+
FinalizerHelpers.PerformNoPinAction (() => {
189
181
o = new JavaObject ();
190
182
});
191
-
t.Start ();
192
-
t.Join ();
193
183
o.ToString ();
194
184
o.Dispose ();
195
185
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4