1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Threading ;
4
5
using System . Threading . Tasks ;
@@ -64,9 +65,8 @@ public ParseCoordinator(
64
65
}
65
66
66
67
// 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 ( ) ;
70
70
71
71
private readonly object _cancellationSyncObject = new object ( ) ;
72
72
private readonly object _parsingRunSyncObject = new object ( ) ;
@@ -77,9 +77,17 @@ private void ReparseRequested(object sender, EventArgs e)
77
77
lock ( _cancellationSyncObject )
78
78
{
79
79
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 ;
81
88
}
82
89
90
+
83
91
if ( ! _isTestScope )
84
92
{
85
93
Task . Run ( ( ) => ParseAll ( sender , token ) , token ) ;
@@ -92,15 +100,19 @@ private void ReparseRequested(object sender, EventArgs e)
92
100
93
101
private void Cancel ( bool createNewTokenSource = true )
94
102
{
95
- lock ( _cancellationTokens [ 0 ] )
103
+ lock ( _cancellationSyncObject )
96
104
{
97
- _cancellationTokens [ 0 ] . Cancel ( ) ;
98
- _cancellationTokens [ 0 ] . Dispose ( ) ;
99
- if ( createNewTokenSource )
105
+ if ( _currentCancellationTokenSource == null )
100
106
{
101
- _cancellationTokens . Add ( new CancellationTokenSource ( ) ) ;
107
+ Logger . Error ( "Tried to cancel a parse after the final cancellation." ) ;
108
+ return ;
102
109
}
103
- _cancellationTokens . RemoveAt ( 0 ) ;
110
+
111
+ var oldTokenSource = _currentCancellationTokenSource ;
112
+ _currentCancellationTokenSource = createNewTokenSource ? new CancellationTokenSource ( ) : null ;
113
+
114
+ oldTokenSource . Cancel ( ) ;
115
+ oldTokenSource . Dispose ( ) ;
104
116
}
105
117
}
106
118
@@ -114,18 +126,17 @@ public void Parse(CancellationTokenSource token)
114
126
ParseInternal ( token . Token ) ;
115
127
}
116
128
117
- private void SetSavedCancellationTokenSource ( CancellationTokenSource token )
129
+ /// <summary>
130
+ /// For the use of tests only
131
+ /// </summary>
132
+ ///
133
+ private void SetSavedCancellationTokenSource ( CancellationTokenSource tokenSource )
118
134
{
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 ( ) ;
129
140
}
130
141
131
142
private void ParseInternal ( CancellationToken token )
0 commit comments