Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7b134c

Browse files
authored
Merge pull request #4 from Unity-Technologies/soft-shutdown-review-comments-3
* Remove compile-time check on NETSTANDARD * Extract method `ClearCLRData`
2 parents 12c0206 + ff956e4 commit c7b134c

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

src/runtime/BorrowedReference.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ readonly ref struct BorrowedReference
1010
readonly IntPtr pointer;
1111
public bool IsNull => this.pointer == IntPtr.Zero;
1212

13+
1314
/// <summary>Gets a raw pointer to the Python object</summary>
14-
public IntPtr DangerousGetAddress() => this.pointer;
15+
public IntPtr DangerousGetAddress()
16+
=> this.IsNull ? throw new NullReferenceException() : this.pointer;
1517

1618
/// <summary>
1719
/// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe.

src/runtime/runtime.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,12 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
151151
{
152152
PyEval_InitThreads();
153153
}
154-
if (mode == ShutdownMode.Soft)
155-
{
156-
RuntimeState.Save();
157-
}
158-
#if !NETSTANDARD
159154
// XXX: Reload mode may reduct to Soft mode,
160155
// so even on Reload mode it still needs to save the RuntimeState
161-
else if (mode == ShutdownMode.Reload)
156+
if (mode == ShutdownMode.Soft || mode == ShutdownMode.Reload)
162157
{
163158
RuntimeState.Save();
164159
}
165-
#endif
166160
}
167161
else if (!fromPython)
168162
{
@@ -190,13 +184,11 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
190184

191185
// Initialize modules that depend on the runtime class.
192186
AssemblyManager.Initialize();
193-
#if !NETSTANDARD
194187
if (mode == ShutdownMode.Reload && RuntimeData.HasStashData())
195188
{
196189
RuntimeData.RestoreRuntimeData();
197190
}
198191
else
199-
#endif
200192
{
201193
PyCLRMetaType = MetaType.Initialize(); // Steal a reference
202194
ImportHook.Initialize();
@@ -342,9 +334,7 @@ static ShutdownMode TryDowngradeShutdown(ShutdownMode mode)
342334
if (
343335
mode == Runtime.ShutdownMode
344336
|| mode == ShutdownMode.Normal
345-
#if !NETSTANDARD
346337
|| (mode == ShutdownMode.Soft && Runtime.ShutdownMode == ShutdownMode.Reload)
347-
#endif
348338
)
349339
{
350340
return mode;
@@ -374,12 +364,10 @@ internal static void Shutdown(ShutdownMode mode)
374364
{
375365
RunExitFuncs();
376366
}
377-
#if !NETSTANDARD
378367
if (mode == ShutdownMode.Reload)
379368
{
380369
RuntimeData.Stash();
381370
}
382-
#endif
383371
AssemblyManager.Shutdown();
384372
ImportHook.Shutdown();
385373

@@ -2141,13 +2129,13 @@ internal static void Py_CLEAR(ref IntPtr ob)
21412129
//====================================================================
21422130

21432131
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
2144-
internal static extern IntPtr PyCapsule_New(IntPtr pointer, string name, IntPtr destructor);
2132+
internal static extern NewReference PyCapsule_New(IntPtr pointer, string name, IntPtr destructor);
21452133

21462134
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
2147-
internal static extern IntPtr PyCapsule_GetPointer(IntPtr capsule, string name);
2135+
internal static extern IntPtr PyCapsule_GetPointer(BorrowedReference capsule, string name);
21482136

21492137
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
2150-
internal static extern int PyCapsule_SetPointer(IntPtr capsule, IntPtr pointer);
2138+
internal static extern int PyCapsule_SetPointer(BorrowedReference capsule, IntPtr pointer);
21512139

21522140
//====================================================================
21532141
// Miscellaneous
@@ -2206,9 +2194,7 @@ public enum ShutdownMode
22062194
Default,
22072195
Normal,
22082196
Soft,
2209-
#if !NETSTANDARD
22102197
Reload,
2211-
#endif
22122198
}
22132199

22142200

src/runtime/runtime_data.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ public static Type FormatterType
3131

3232
public static ICLRObjectStorer WrappersStorer { get; set; }
3333

34+
/// <summary>
35+
/// Clears the old "clr_data" entry if a previous one is present.
36+
/// </summary>
37+
static void ClearCLRData ()
38+
{
39+
BorrowedReference capsule = PySys_GetObject("clr_data");
40+
if (!capsule.IsNull)
41+
{
42+
IntPtr oldData = PyCapsule_GetPointer(capsule, null);
43+
PyMem_Free(oldData);
44+
PyCapsule_SetPointer(capsule, IntPtr.Zero);
45+
}
46+
}
47+
3448
internal static void Stash()
3549
{
3650
var metaStorage = new RuntimeDataStorage();
@@ -70,16 +84,11 @@ internal static void Stash()
7084
Marshal.WriteIntPtr(mem, (IntPtr)ms.Length);
7185
Marshal.Copy(data, 0, mem + IntPtr.Size, (int)ms.Length);
7286

73-
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddress();
74-
if (capsule != IntPtr.Zero)
75-
{
76-
IntPtr oldData = PyCapsule_GetPointer(capsule, null);
77-
PyMem_Free(oldData);
78-
PyCapsule_SetPointer(capsule, IntPtr.Zero);
79-
}
80-
capsule = PyCapsule_New(mem, null, IntPtr.Zero);
81-
PySys_SetObject("clr_data", capsule);
82-
XDecref(capsule);
87+
ClearCLRData();
88+
NewReference capsule = PyCapsule_New(mem, null, IntPtr.Zero);
89+
PySys_SetObject("clr_data", capsule.DangerousGetAddress());
90+
// Let the dictionary own the reference
91+
capsule.Dispose();
8392
}
8493

8594
internal static void RestoreRuntimeData()
@@ -96,8 +105,8 @@ internal static void RestoreRuntimeData()
96105

97106
private static void RestoreRuntimeDataImpl()
98107
{
99-
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddress();
100-
if (capsule == IntPtr.Zero)
108+
BorrowedReference capsule = PySys_GetObject("clr_data");
109+
if (capsule.IsNull)
101110
{
102111
return;
103112
}

src/runtime/typemanager.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ internal static SlotsHolder SetupMetaSlots(Type impl, IntPtr type)
459459

460460
Marshal.WriteIntPtr(type, TypeOffset.tp_methods, mdefStart);
461461

462-
#if !NETSTANDARD
463462
// XXX: Hard code with mode check.
464463
if (Runtime.ShutdownMode != ShutdownMode.Reload)
465464
{
@@ -470,7 +469,6 @@ internal static SlotsHolder SetupMetaSlots(Type impl, IntPtr type)
470469
Marshal.WriteIntPtr(t, offset, IntPtr.Zero);
471470
});
472471
}
473-
#endif
474472
return slotsHolder;
475473
}
476474

@@ -480,10 +478,8 @@ private static IntPtr AddCustomMetaMethod(string name, IntPtr type, IntPtr mdef,
480478
ThunkInfo thunkInfo = Interop.GetThunk(mi, "BinaryFunc");
481479
slotsHolder.KeeapAlive(thunkInfo);
482480

483-
#if !NETSTANDARD
484481
// XXX: Hard code with mode check.
485482
if (Runtime.ShutdownMode != ShutdownMode.Reload)
486-
#endif
487483
{
488484
IntPtr mdefAddr = mdef;
489485
slotsHolder.AddDealloctor(() =>

0 commit comments

Comments
 (0)