|
1 | 1 | using System; |
2 | 2 | using System.Collections.Concurrent; |
3 | | -using System.Threading; |
4 | | -using System.Threading.Tasks; |
5 | 3 |
|
6 | 4 | namespace Python.Runtime |
7 | 5 | { |
8 | 6 | internal class PyReferenceDecrementer: IDisposable |
9 | 7 | { |
10 | | - private readonly BlockingCollection<IntPtr> _asyncDecRefQueue = new BlockingCollection<IntPtr>(); |
11 | 8 | private readonly ConcurrentQueue<IntPtr> _decRefQueue = new ConcurrentQueue<IntPtr>(); |
12 | | - private CancellationTokenSource _cts; |
13 | | - private CancellationToken _ct; |
14 | | - private Task _backgroundWorkerTask; |
15 | | - private bool _multipleThreadsAllowed; |
16 | 9 |
|
17 | | - public void ScheduleDecRef(IntPtr pyRef) |
18 | | - { |
19 | | - if (_multipleThreadsAllowed) |
20 | | - { |
21 | | - // ReSharper disable once MethodSupportsCancellation |
22 | | - _asyncDecRefQueue.Add(pyRef); |
23 | | - } |
24 | | - else |
25 | | - { |
26 | | - _decRefQueue.Enqueue(pyRef); |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - public void AllowMultipleThreads() |
| 10 | + public void TryPerformScheduledDecrements() |
31 | 11 | { |
32 | | - DecReferences(); |
33 | | - _multipleThreadsAllowed = true; |
34 | | - InitDecRefThread(); |
| 12 | + //Trying to not affecting performance. |
| 13 | + DecReferences(1000); |
35 | 14 | } |
36 | 15 |
|
37 | | - public void DisableMultipleThreads() |
| 16 | + public void ScheduleDecRef(IntPtr pyRef) |
38 | 17 | { |
39 | | - ShutdownDecRefThread(); |
40 | | - _multipleThreadsAllowed = false; |
41 | | - } |
42 | | - |
43 | | - public void TryPerformScheduledDecrements() |
44 | | - { |
45 | | - if (!_multipleThreadsAllowed) |
46 | | - { |
47 | | - DecReferences(); |
48 | | - } |
| 18 | + _decRefQueue.Enqueue(pyRef); |
49 | 19 | } |
50 | 20 |
|
51 | 21 | public void FlushDecrementQueue() |
52 | 22 | { |
53 | | - if (_multipleThreadsAllowed) |
| 23 | + IntPtr gs = PythonEngine.AcquireLock(); |
| 24 | + try |
54 | 25 | { |
55 | 26 | DecReferences(); |
56 | 27 | } |
57 | | - else |
| 28 | + finally |
58 | 29 | { |
59 | | - ShutdownDecRefThread(); |
60 | | - InitDecRefThread(); |
| 30 | + PythonEngine.ReleaseLock(gs); |
61 | 31 | } |
62 | 32 | } |
63 | 33 |
|
64 | | - private void ShutdownDecRefThread() |
65 | | - { |
66 | | - _cts?.Cancel(); |
67 | | - try |
68 | | - { |
69 | | - _backgroundWorkerTask.Wait(); |
70 | | - } |
71 | | - catch |
72 | | - { |
73 | | - // Just stopping background thread. |
74 | | - } |
75 | | - _cts = null; |
76 | | - _ct = default(CancellationToken); |
77 | | - |
78 | | - _backgroundWorkerTask = null; |
79 | | - } |
80 | | - |
81 | | - private void InitDecRefThread() |
82 | | - { |
83 | | - _cts = new CancellationTokenSource(); |
84 | | - _ct = _cts.Token; |
85 | | - _backgroundWorkerTask = Task.Factory.StartNew(WorkerThread, TaskCreationOptions.LongRunning); |
86 | | - } |
87 | | - |
88 | | - private void DecReferences() |
| 34 | + private void DecReferences(int? maxDecreases = null) |
89 | 35 | { |
90 | 36 | try |
91 | 37 | { |
| 38 | + int decreasedCount = 0; |
92 | 39 | IntPtr refToDecrease; |
93 | 40 | while (_decRefQueue.TryDequeue(out refToDecrease)) |
94 | 41 | { |
95 | 42 | Runtime.XDecref(refToDecrease); |
96 | | - } |
97 | | - } |
98 | | - catch |
99 | | - { |
100 | | - // Do nothing. |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - private void WorkerThread() |
105 | | - { |
106 | | - try |
107 | | - { |
108 | | - while (true) |
109 | | - { |
110 | | - IntPtr refToDecrease; |
111 | | - |
112 | | - refToDecrease = _asyncDecRefQueue.Take(_ct); |
113 | | - |
114 | | - try |
| 43 | + decreasedCount++; |
| 44 | + if (maxDecreases != null && decreasedCount > maxDecreases) |
115 | 45 | { |
116 | | - IntPtr gs = PythonEngine.AcquireLock(); |
117 | | - try |
118 | | - { |
119 | | - do |
120 | | - { |
121 | | - Runtime.XDecref(refToDecrease); |
122 | | - } while (_asyncDecRefQueue.TryTake(out refToDecrease)); |
123 | | - } |
124 | | - finally |
125 | | - { |
126 | | - PythonEngine.ReleaseLock(gs); |
127 | | - } |
128 | | - } |
129 | | - catch |
130 | | - { |
131 | | - // Nothing to do in this case. |
| 46 | + break; |
132 | 47 | } |
133 | 48 | } |
134 | 49 | } |
135 | | - catch (OperationCanceledException) |
| 50 | + catch |
136 | 51 | { |
137 | | - // This is normal exit pass. |
| 52 | + // Do nothing. |
138 | 53 | } |
139 | 54 | } |
140 | 55 |
|
141 | 56 | public void Dispose() |
142 | 57 | { |
143 | | - if (_multipleThreadsAllowed) |
144 | | - { |
145 | | - ShutdownDecRefThread(); |
146 | | - } |
147 | | - else |
148 | | - { |
149 | | - DecReferences(); |
150 | | - } |
| 58 | + DecReferences(); |
151 | 59 | } |
152 | 60 | } |
153 | | - |
154 | 61 | } |
0 commit comments