Skip to content

Commit 86b6f6c

Browse files
committed
Simplified cancellation logistics in the ParseCoordinator.
1 parent 8439296 commit 86b6f6c

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

Rubberduck.Parsing/VBA/ParseCoordinator.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -64,9 +65,8 @@ public ParseCoordinator(
6465
}
6566

6667
// Do not access this from anywhere but ReparseRequested.
67-
// ReparseRequested needs to have a reference to all the cancellation tokens,
68-
// but the cancelees need to use their own token.
69-
private readonly List<CancellationTokenSource> _cancellationTokens = new List<CancellationTokenSource> { new CancellationTokenSource() };
68+
// ReparseRequested needs to have a reference to the cancellation token.
69+
private CancellationTokenSource _currentCancellationTokenSource = new CancellationTokenSource();
7070

7171
private readonly object _cancellationSyncObject = new object();
7272
private readonly object _parsingRunSyncObject = new object();
@@ -77,9 +77,17 @@ private void ReparseRequested(object sender, EventArgs e)
7777
lock (_cancellationSyncObject)
7878
{
7979
Cancel();
80-
token = _cancellationTokens[0].Token;
80+
81+
if (_currentCancellationTokenSource == null)
82+
{
83+
Logger.Error("Tried to request a parse after the final cancellation.");
84+
return;
85+
}
86+
87+
token = _currentCancellationTokenSource.Token;
8188
}
8289

90+
8391
if (!_isTestScope)
8492
{
8593
Task.Run(() => ParseAll(sender, token), token);
@@ -92,15 +100,19 @@ private void ReparseRequested(object sender, EventArgs e)
92100

93101
private void Cancel(bool createNewTokenSource = true)
94102
{
95-
lock (_cancellationTokens[0])
103+
lock (_cancellationSyncObject)
96104
{
97-
_cancellationTokens[0].Cancel();
98-
_cancellationTokens[0].Dispose();
99-
if (createNewTokenSource)
105+
if (_currentCancellationTokenSource == null)
100106
{
101-
_cancellationTokens.Add(new CancellationTokenSource());
107+
Logger.Error("Tried to cancel a parse after the final cancellation.");
108+
return;
102109
}
103-
_cancellationTokens.RemoveAt(0);
110+
111+
var oldTokenSource = _currentCancellationTokenSource;
112+
_currentCancellationTokenSource = createNewTokenSource ? new CancellationTokenSource() : null;
113+
114+
oldTokenSource.Cancel();
115+
oldTokenSource.Dispose();
104116
}
105117
}
106118

@@ -114,18 +126,17 @@ public void Parse(CancellationTokenSource token)
114126
ParseInternal(token.Token);
115127
}
116128

117-
private void SetSavedCancellationTokenSource(CancellationTokenSource token)
129+
/// <summary>
130+
/// For the use of tests only
131+
/// </summary>
132+
///
133+
private void SetSavedCancellationTokenSource(CancellationTokenSource tokenSource)
118134
{
119-
if (_cancellationTokens.Any())
120-
{
121-
_cancellationTokens[0].Cancel();
122-
_cancellationTokens[0].Dispose();
123-
_cancellationTokens[0] = token;
124-
}
125-
else
126-
{
127-
_cancellationTokens.Add(token);
128-
}
135+
var oldTokenSource = _currentCancellationTokenSource;
136+
_currentCancellationTokenSource = tokenSource;
137+
138+
oldTokenSource?.Cancel();
139+
oldTokenSource?.Dispose();
129140
}
130141

131142
private void ParseInternal(CancellationToken token)

0 commit comments

Comments
 (0)