Skip to content

Commit b2b8a48

Browse files
committed
Pass cancellation token further down in multithreaded code, #922
1 parent b137e19 commit b2b8a48

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Lucene.Net/Search/IndexSearcher.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ protected virtual TopDocs Search(Weight weight, ScoreDoc? after, int nDocs, Canc
539539

540540
for (int i = 0; i < m_leafSlices.Length; i++) // search each sub
541541
{
542-
runner.Submit(new SearcherCallableNoSort(@lock, this, m_leafSlices[i], weight, after, nDocs, hq).Call);
542+
runner.Submit(new SearcherCallableNoSort(@lock, this, m_leafSlices[i], weight, after, nDocs, hq, cancellationToken).Call);
543543
}
544544

545545
int totalHits = 0;
@@ -649,7 +649,7 @@ protected virtual TopFieldDocs Search(Weight weight, FieldDoc? after, int nDocs,
649649

650650
for (int i = 0; i < m_leafSlices.Length; i++) // search each leaf slice
651651
{
652-
runner.Submit(new SearcherCallableWithSort(@lock, this, m_leafSlices[i], weight, after, nDocs, topCollector, sort, doDocScores, doMaxScore).Call);
652+
runner.Submit(new SearcherCallableWithSort(@lock, this, m_leafSlices[i], weight, after, nDocs, topCollector, sort, doDocScores, doMaxScore, cancellationToken).Call);
653653
}
654654

655655
int totalHits = 0;
@@ -756,6 +756,8 @@ protected virtual void Search(IList<AtomicReaderContext> leaves, Weight weight,
756756
}
757757
}
758758
}
759+
760+
cancellationToken.ThrowIfCancellationRequested(); // LUCENENET specific - cancellation support
759761
}
760762

761763
/// <summary>
@@ -859,21 +861,23 @@ public virtual Weight CreateNormalizedWeight(Query query)
859861
private readonly int nDocs;
860862
private readonly HitQueue hq;
861863
private readonly LeafSlice slice;
864+
private readonly CancellationToken cancellationToken;
862865

863-
public SearcherCallableNoSort(ReentrantLock @lock, IndexSearcher searcher, LeafSlice slice, Weight weight, ScoreDoc? after, int nDocs, HitQueue hq)
866+
public SearcherCallableNoSort(ReentrantLock @lock, IndexSearcher searcher, LeafSlice slice, Weight weight, ScoreDoc? after, int nDocs, HitQueue hq, CancellationToken cancellationToken)
864867
{
865868
this.@lock = @lock;
866869
this.searcher = searcher;
867870
this.weight = weight;
868871
this.after = after;
869872
this.nDocs = nDocs;
870873
this.hq = hq;
874+
this.cancellationToken = cancellationToken;
871875
this.slice = slice;
872876
}
873877

874878
public TopDocs Call()
875879
{
876-
TopDocs docs = searcher.Search(slice.Leaves, weight, after, nDocs);
880+
TopDocs docs = searcher.Search(slice.Leaves, weight, after, nDocs, cancellationToken);
877881
ScoreDoc[] scoreDocs = docs.ScoreDocs;
878882
//it would be so nice if we had a thread-safe insert
879883
@lock.Lock();
@@ -911,8 +915,9 @@ public TopDocs Call()
911915
private readonly FieldDoc? after;
912916
private readonly bool doDocScores;
913917
private readonly bool doMaxScore;
918+
private readonly CancellationToken cancellationToken;
914919

915-
public SearcherCallableWithSort(ReentrantLock @lock, IndexSearcher searcher, LeafSlice slice, Weight weight, FieldDoc? after, int nDocs, TopFieldCollector hq, Sort sort, bool doDocScores, bool doMaxScore)
920+
public SearcherCallableWithSort(ReentrantLock @lock, IndexSearcher searcher, LeafSlice slice, Weight weight, FieldDoc? after, int nDocs, TopFieldCollector hq, Sort sort, bool doDocScores, bool doMaxScore, CancellationToken cancellationToken)
916921
{
917922
this.@lock = @lock;
918923
this.searcher = searcher;
@@ -924,14 +929,15 @@ public SearcherCallableWithSort(ReentrantLock @lock, IndexSearcher searcher, Lea
924929
this.after = after;
925930
this.doDocScores = doDocScores;
926931
this.doMaxScore = doMaxScore;
932+
this.cancellationToken = cancellationToken;
927933
}
928934

929935
private readonly FakeScorer fakeScorer = new FakeScorer();
930936

931937
public TopFieldDocs Call()
932938
{
933939
if (Debugging.AssertsEnabled) Debugging.Assert(slice.Leaves.Length == 1);
934-
TopFieldDocs docs = searcher.Search(slice.Leaves, weight, after, nDocs, sort, true, doDocScores || sort.NeedsScores, doMaxScore);
940+
TopFieldDocs docs = searcher.Search(slice.Leaves, weight, after, nDocs, sort, true, doDocScores || sort.NeedsScores, doMaxScore, cancellationToken);
935941
@lock.Lock();
936942
try
937943
{
@@ -991,7 +997,7 @@ public void Dispose()
991997

992998
public void Submit(Func<T> task)
993999
{
994-
this.service.Submit(task);
1000+
this.service.Submit(task, cancellationToken);
9951001
++numTasks;
9961002
}
9971003

src/Lucene.Net/Support/Threading/TaskSchedulerCompletionService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
namespace Lucene.Net.Support.Threading
@@ -31,9 +32,9 @@ public TaskSchedulerCompletionService(TaskScheduler scheduler)
3132
this.factory = new TaskFactory<T>(scheduler ?? TaskScheduler.Default);
3233
}
3334

34-
public Task<T> Submit(Func<T> task)
35+
public Task<T> Submit(Func<T> task, CancellationToken cancellationToken = default)
3536
{
36-
var t = factory.StartNew(task);
37+
var t = factory.StartNew(task, cancellationToken);
3738
taskQueue.Enqueue(t);
3839
return t;
3940
}
@@ -43,4 +44,4 @@ public Task<T> Take()
4344
return taskQueue.Dequeue();
4445
}
4546
}
46-
}
47+
}

0 commit comments

Comments
 (0)