-
Notifications
You must be signed in to change notification settings - Fork 747
PyObject finalizer #692
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
PyObject finalizer #692
Changes from 1 commit
8083f3b
af33e74
6d9f897
7140fd0
f66697d
799d37e
cb55163
bfc0392
59b614d
569cd94
f4f5032
0967a12
f071c55
cfda491
f6c6e42
0d96641
34713f7
b4e30ac
f836ffa
a4bb82d
21da86d
5254c65
916e85e
eee3683
cee8e17
247e2d9
90c67ca
6d68d70
4eff81e
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 |
---|---|---|
|
@@ -22,7 +22,8 @@ public class CollectArgs : EventArgs | |
|
||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
private delegate int PedingCall(IntPtr arg); | ||
private PedingCall _collectAction; | ||
private readonly PedingCall _collectAction; | ||
|
||
private bool _pending = false; | ||
private readonly object _collectingLock = new object(); | ||
public int Threshold { get; set; } | ||
|
@@ -32,7 +33,7 @@ private Finalizer() | |
{ | ||
Enable = true; | ||
Threshold = 200; | ||
_collectAction = OnCollect; | ||
_collectAction = OnPendingCollect; | ||
} | ||
|
||
public void CallPendingFinalizers() | ||
|
@@ -44,6 +45,14 @@ public void CallPendingFinalizers() | |
Runtime.Py_MakePendingCalls(); | ||
} | ||
|
||
public void Collect() | ||
{ | ||
using (var gilState = new Py.GILState()) | ||
{ | ||
DisposeAll(); | ||
} | ||
} | ||
|
||
public List<WeakReference> GetCollectedObjects() | ||
{ | ||
return _objQueue.Select(T => new WeakReference(T)).ToList(); | ||
|
@@ -65,7 +74,7 @@ internal void AddFinalizedObject(IDisposable obj) | |
GC.ReRegisterForFinalize(obj); | ||
if (_objQueue.Count >= Threshold) | ||
{ | ||
Collect(); | ||
AddPendingCollect(); | ||
} | ||
} | ||
|
||
|
@@ -76,7 +85,7 @@ internal static void Shutdown() | |
Runtime.PyErr_Clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you calling this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure no garbage left and clear the pending calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. emmm...It seems I should expose the exceptions in DisposeAll after each |
||
} | ||
|
||
private void Collect() | ||
private void AddPendingCollect() | ||
{ | ||
lock (_collectingLock) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lock should probably encompass the whole function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why it needs to be lock whole function, the flag already set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow, either you expect multiple concurrent executions, then the lock needs to surround the whole function, or you don't, then you don't need any locking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, yes, thank you for you reminder, I neglect that there is another store at line 114. |
||
{ | ||
|
@@ -94,19 +103,19 @@ private void Collect() | |
} | ||
} | ||
|
||
private int OnCollect(IntPtr arg) | ||
private int OnPendingCollect(IntPtr arg) | ||
{ | ||
CollectOnce?.Invoke(this, new CollectArgs() | ||
{ | ||
ObjectCount = _objQueue.Count | ||
}); | ||
DisposeAll(); | ||
Collect(); | ||
_pending = false; | ||
return 0; | ||
} | ||
|
||
private void DisposeAll() | ||
{ | ||
CollectOnce?.Invoke(this, new CollectArgs() | ||
{ | ||
ObjectCount = _objQueue.Count | ||
}); | ||
IDisposable obj; | ||
while (_objQueue.TryDequeue(out obj)) | ||
{ | ||
|
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.
PendingCall
?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 calling type for Py_AddPendingCall
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.
I know, there's just a typo in the name ;)