Skip to content
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

Bug Fix: Re-ported ControlledRealTimeReopenThread and added 2 new tests #513

Merged
merged 7 commits into from
Aug 18, 2021
Prev Previous commit
Added Dispose(b) to ControlledRealTimeReopenThread
Added back a Dispose(bool) method to the ControlledRealTimeReopenThread class since it's a non-sealed class.
  • Loading branch information
rclabo committed Aug 18, 2021
commit ca9850e05cd607855e47e631b760829ec1a001b2
56 changes: 42 additions & 14 deletions src/Lucene.Net/Search/ControlledRealTimeReopenThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public class ControlledRealTimeReopenThread<T> : ThreadJob, IDisposable
private long waitingGen;
private long searchingGen;
private long refreshStartGen;

private bool isDisposed = false;
private readonly EventWaitHandle intrinsic = new ManualResetEvent(false); // LUCENENET specific: used to mimic intrinsic monitor used by java wait and notifyAll keywords.
private readonly EventWaitHandle reopenCond = new AutoResetEvent(false); // LUCENENET NOTE: unlick java, in c# we don't need to lock reopenCond when calling methods on it.
private readonly EventWaitHandle reopenCond = new AutoResetEvent(false); // LUCENENET NOTE: unlike java, in c# we don't need to lock reopenCond when calling methods on it.


/// <summary>
Expand Down Expand Up @@ -119,27 +119,53 @@ private void RefreshDone()
/// thread so that when this method returns the thread is no longer alive.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}



/// <summary>
/// Kills the thread and releases all resources used by the
/// <see cref="ControlledRealTimeReopenThread{T}"/>. Also joins to the
/// thread so that when this method returns the thread is no longer alive.
/// </summary>
// LUCENENET specific - Support for Dispose(bool) since this is a non-sealed class.
protected virtual void Dispose(bool disposing)
{
lock (this)
{
finish = true;
if (isDisposed)
{
return;
}

if (disposing)
{

finish = true;

// So thread wakes up and notices it should finish:
reopenCond.Set();
// So thread wakes up and notices it should finish:
reopenCond.Set();

Join();
// LUCENENET NOTE: No need to catch and rethrow same excepton type ThreadInterruptedException
Join();
// LUCENENET NOTE: No need to catch and rethrow same excepton type ThreadInterruptedException

// Max it out so any waiting search threads will return:
searchingGen = long.MaxValue;
intrinsic.Set(); //LUCENENET NOTE: notify all the waiting threads to wake them up.
// Max it out so any waiting search threads will return:
searchingGen = long.MaxValue;
intrinsic.Set(); //LUCENENET NOTE: notify all the waiting threads to wake them up.

// LUCENENET specific: dispose reset event
reopenCond.Dispose();
intrinsic.Dispose();
// LUCENENET specific: dispose reset event
reopenCond.Dispose();
intrinsic.Dispose();

}

isDisposed = true;
}
}


/// <summary>
/// Waits for the target generation to become visible in
Expand Down Expand Up @@ -292,5 +318,7 @@ public override void Run()

}
}


}
}