This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit c49fd9a26f027ab9b16123d2fc07f144ee97e694
Author: Shad Storhaug <[email protected]>
AuthorDate: Sun May 14 20:32:50 2023 +0700

    Lucene.Net.Store (Directory + MMapDirectory + NIOFSDirectory + 
SimpleFSDirectory): Upgraded all FSDirectories to allow multiple Dispose() 
calls on IndexInputSlicer and BufferedIndexInput subclasses. See #265.
---
 src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs | 13 ++++++-------
 src/Lucene.Net/Store/Directory.cs                          |  7 +++++++
 src/Lucene.Net/Store/MMapDirectory.cs                      |  8 +++++++-
 src/Lucene.Net/Store/NIOFSDirectory.cs                     |  8 ++++++++
 src/Lucene.Net/Store/SimpleFSDirectory.cs                  |  8 ++++++++
 5 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs 
b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
index fc2b70004..dc226fa69 100644
--- a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
@@ -1399,17 +1399,16 @@ namespace Lucene.Net.Store
                 this.delegateHandle = delegateHandle;
             }
 
-            private int disposed = 0;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             protected override void Dispose(bool disposing)
             {
-                if (0 == Interlocked.CompareExchange(ref this.disposed, 1, 0))
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
+                if (disposing)
                 {
-                    if (disposing)
-                    {
-                        delegateHandle.Dispose();
-                        outerInstance.RemoveOpenFile(this, name);
-                    }
+                    delegateHandle.Dispose();
+                    outerInstance.RemoveOpenFile(this, name);
                 }
             }
 
diff --git a/src/Lucene.Net/Store/Directory.cs 
b/src/Lucene.Net/Store/Directory.cs
index f84a16a4c..f58bfc864 100644
--- a/src/Lucene.Net/Store/Directory.cs
+++ b/src/Lucene.Net/Store/Directory.cs
@@ -2,6 +2,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Threading;
 
 namespace Lucene.Net.Store
 {
@@ -253,6 +254,7 @@ namespace Lucene.Net.Store
         private sealed class IndexInputSlicerAnonymousClass : IndexInputSlicer
         {
             private readonly IndexInput @base;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             public IndexInputSlicerAnonymousClass(IndexInput @base)
             {
@@ -266,6 +268,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing)
                 {
                     @base.Dispose();
@@ -323,6 +327,7 @@ namespace Lucene.Net.Store
             private IndexInput @base;
             private long fileOffset;
             private long length;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             internal SlicedIndexInput(string sliceDescription, IndexInput 
@base, long fileOffset, long length)
                 : this(sliceDescription, @base, fileOffset, length, 
BufferedIndexInput.BUFFER_SIZE)
@@ -377,6 +382,8 @@ namespace Lucene.Net.Store
             /// </summary>
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing)
                 {
                     @base.Dispose();
diff --git a/src/Lucene.Net/Store/MMapDirectory.cs 
b/src/Lucene.Net/Store/MMapDirectory.cs
index 5a87d2a74..ea5e6a0f9 100644
--- a/src/Lucene.Net/Store/MMapDirectory.cs
+++ b/src/Lucene.Net/Store/MMapDirectory.cs
@@ -5,6 +5,7 @@ using Lucene.Net.Diagnostics;
 using System;
 using System.IO;
 using System.IO.MemoryMappedFiles;
+using System.Threading;
 
 namespace Lucene.Net.Store
 {
@@ -196,8 +197,8 @@ namespace Lucene.Net.Store
         private sealed class IndexInputSlicerAnonymousClass : IndexInputSlicer
         {
             private readonly MMapDirectory outerInstance;
-
             private readonly MMapIndexInput full;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             public IndexInputSlicerAnonymousClass(MMapDirectory outerInstance, 
MMapIndexInput full)
             {
@@ -220,6 +221,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing)
                 {
                     full.Dispose();
@@ -231,6 +234,7 @@ namespace Lucene.Net.Store
         {
             internal MemoryMappedFile memoryMappedFile; // .NET port: this is 
equivalent to FileChannel.map
             private readonly FileStream fc;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             internal MMapIndexInput(MMapDirectory outerInstance, string 
resourceDescription, FileStream fc)
                 : base(resourceDescription, null, fc.Length, 
outerInstance.chunkSizePower, true)
@@ -241,6 +245,8 @@ namespace Lucene.Net.Store
 
             protected override sealed void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 try
                 {
                     if (disposing)
diff --git a/src/Lucene.Net/Store/NIOFSDirectory.cs 
b/src/Lucene.Net/Store/NIOFSDirectory.cs
index 0b918ddb9..684f42b68 100644
--- a/src/Lucene.Net/Store/NIOFSDirectory.cs
+++ b/src/Lucene.Net/Store/NIOFSDirectory.cs
@@ -3,6 +3,7 @@ using Lucene.Net.Diagnostics;
 using Lucene.Net.Support.IO;
 using System;
 using System.IO;
+using System.Threading;
 
 namespace Lucene.Net.Store
 {
@@ -119,6 +120,7 @@ namespace Lucene.Net.Store
             private readonly IOContext context;
             private readonly FileInfo path;
             private readonly FileStream descriptor;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             public IndexInputSlicerAnonymousClass(IOContext context, FileInfo 
path, FileStream descriptor)
             {
@@ -129,6 +131,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing)
                 {
                     descriptor.Dispose();
@@ -184,6 +188,8 @@ namespace Lucene.Net.Store
 
             private ByteBuffer byteBuf; // wraps the buffer for NIO
 
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
+
             public NIOFSIndexInput(string resourceDesc, FileStream fc, 
IOContext context)
                 : base(resourceDesc, context)
             {
@@ -203,6 +209,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing && !isClone)
                 {
                     m_channel.Dispose();
diff --git a/src/Lucene.Net/Store/SimpleFSDirectory.cs 
b/src/Lucene.Net/Store/SimpleFSDirectory.cs
index 61536f68c..923ca685b 100644
--- a/src/Lucene.Net/Store/SimpleFSDirectory.cs
+++ b/src/Lucene.Net/Store/SimpleFSDirectory.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Support.Threading;
 using System;
 using System.IO;
+using System.Threading;
 
 namespace Lucene.Net.Store
 {
@@ -115,6 +116,7 @@ namespace Lucene.Net.Store
             private readonly IOContext context;
             private readonly FileInfo file;
             private readonly FileStream descriptor;
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
 
             public IndexInputSlicerAnonymousClass(IOContext context, FileInfo 
file, FileStream descriptor)
             {
@@ -125,6 +127,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing)
                 {
                     descriptor.Dispose();
@@ -156,6 +160,8 @@ namespace Lucene.Net.Store
         /// </summary>
         protected internal class SimpleFSIndexInput : BufferedIndexInput
         {
+            private int disposed = 0; // LUCENENET specific - allow 
double-dispose
+
             // LUCENENET specific: chunk size not needed
             ///// <summary>
             ///// The maximum chunk size is 8192 bytes, because <seealso 
cref="RandomAccessFile"/> mallocs
@@ -199,6 +205,8 @@ namespace Lucene.Net.Store
 
             protected override void Dispose(bool disposing)
             {
+                if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) 
return; // LUCENENET specific - allow double-dispose
+
                 if (disposing && !IsClone)
                 {
                     m_file.Dispose();

Reply via email to