You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by di...@apache.org on 2009/12/22 22:24:30 UTC
svn commit: r893322 - in /incubator/lucene.net/trunk/C#/src:
Lucene.Net/Search/FieldCacheImpl.cs Lucene.Net/SupportClass.cs
Test/TestSupportClass.cs
Author: digy
Date: Tue Dec 22 21:24:29 2009
New Revision: 893322
URL: http://svn.apache.org/viewvc?rev=893322&view=rev
Log: (empty)
Modified:
incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/FieldCacheImpl.cs
incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs
incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/FieldCacheImpl.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Search/FieldCacheImpl.cs?rev=893322&r1=893321&r2=893322&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/FieldCacheImpl.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/FieldCacheImpl.cs Tue Dec 22 21:24:29 2009
@@ -51,17 +51,18 @@
{
lock (this)
{
- caches = new System.Collections.Hashtable(7);
- caches[System.Type.GetType("System.SByte")] = new ByteCache(this);
- caches[System.Type.GetType("System.Int16")] = new ShortCache(this);
- caches[System.Type.GetType("System.Int32")] = new IntCache(this);
- caches[System.Type.GetType("System.Single")] = new FloatCache(this);
- caches[System.Type.GetType("System.Int64")] = new LongCache(this);
- caches[System.Type.GetType("System.Double")] = new DoubleCache(this);
- caches[typeof(System.String)] = new StringCache(this);
- caches[typeof(StringIndex)] = new StringIndexCache(this);
- caches[typeof(System.IComparable)] = new CustomCache(this);
- caches[typeof(System.Object)] = new AutoCache(this);
+ System.Collections.Hashtable caches2 = new System.Collections.Hashtable(7);
+ caches2[System.Type.GetType("System.SByte")] = new ByteCache(this);
+ caches2[System.Type.GetType("System.Int16")] = new ShortCache(this);
+ caches2[System.Type.GetType("System.Int32")] = new IntCache(this);
+ caches2[System.Type.GetType("System.Single")] = new FloatCache(this);
+ caches2[System.Type.GetType("System.Int64")] = new LongCache(this);
+ caches2[System.Type.GetType("System.Double")] = new DoubleCache(this);
+ caches2[typeof(System.String)] = new StringCache(this);
+ caches2[typeof(StringIndex)] = new StringIndexCache(this);
+ caches2[typeof(System.IComparable)] = new CustomCache(this);
+ caches2[typeof(System.Object)] = new AutoCache(this);
+ caches = caches2;
}
}
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/SupportClass.cs?rev=893322&r1=893321&r2=893322&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs Tue Dec 22 21:24:29 2009
@@ -1306,16 +1306,19 @@
private void Clean()
{
ArrayList keysToDelete = new ArrayList();
- foreach (WeakKey wtk in base.Keys)
+ lock (this)
{
- if (!wtk.IsAlive)
+ foreach (WeakKey wtk in base.Keys)
{
- keysToDelete.Add(wtk);
+ if (!wtk.IsAlive)
+ {
+ keysToDelete.Add(wtk);
+ }
}
- }
- foreach (WeakKey wtk in keysToDelete)
- Remove(wtk);
+ foreach (WeakKey wtk in keysToDelete)
+ Remove(wtk);
+ }
}
@@ -1325,12 +1328,20 @@
public override void Add(object key, object value)
{
CleanIfNeeded();
- base.Add(new WeakKey(key), value);
+ lock (this)
+ {
+ base.Add(new WeakKey(key), value);
+ }
}
public override IDictionaryEnumerator GetEnumerator()
{
- return new WeakDictionaryEnumerator(base.GetEnumerator());
+ Hashtable tmp = null;
+ lock (this)
+ {
+ tmp = (Hashtable)base.Clone();
+ }
+ return new WeakDictionaryEnumerator(tmp.GetEnumerator());
}
/// <summary>
@@ -1341,11 +1352,15 @@
get
{
ArrayList keys = new ArrayList(Count);
- foreach (WeakKey key in base.Keys)
+ lock (this)
{
- object realKey = key.Target;
- if (realKey != null)
- keys.Add(realKey);
+
+ foreach (WeakKey key in base.Keys)
+ {
+ object realKey = key.Target;
+ if (realKey != null)
+ keys.Add(realKey);
+ }
}
return keys;
}
@@ -1360,7 +1375,10 @@
set
{
CleanIfNeeded();
- base[new WeakKey(key)] = value;
+ lock (this)
+ {
+ base[new WeakKey(key)] = value;
+ }
}
}
Modified: incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/TestSupportClass.cs?rev=893322&r1=893321&r2=893322&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs Tue Dec 22 21:24:29 2009
@@ -17,6 +17,7 @@
using System;
using System.Collections;
+using System.Threading;
using NUnit.Framework;
@@ -492,6 +493,86 @@
}
}
+ [TestFixture]
+ public class TestWeakHashTableMultiThreadAccess
+ {
+ SupportClass.WeakHashTable wht = new SupportClass.WeakHashTable();
+ Exception AnyException = null;
+ bool EndOfTest = false;
+
+ [Test]
+ public void Test()
+ {
+ CreateThread(Add);
+ CreateThread(Enum);
+
+ int count = 200;
+ while (count-- > 0)
+ {
+ Thread.Sleep(50);
+ if (AnyException != null)
+ {
+ EndOfTest = true;
+ Thread.Sleep(50);
+ Assert.Fail(AnyException.Message);
+ }
+ }
+ }
+
+ void CreateThread(ThreadStart fxn)
+ {
+ Thread t = new Thread(fxn);
+ t.IsBackground = true;
+ t.Start();
+ }
+
+
+ void Add()
+ {
+ try
+ {
+ long count = 0;
+ while (EndOfTest==false)
+ {
+ wht.Add(count.ToString(), count.ToString());
+ Thread.Sleep(1);
+
+ string toReplace = (count - 10).ToString();
+ if (wht.Contains(toReplace))
+ {
+ wht[toReplace] = "aa";
+ }
+
+ count++;
+ }
+ }
+ catch (Exception ex)
+ {
+ AnyException = ex;
+ }
+ }
+
+ void Enum()
+ {
+ try
+ {
+ while (EndOfTest==false)
+ {
+ System.Collections.IEnumerator e = wht.Keys.GetEnumerator();
+ while (e.MoveNext())
+ {
+ string s = (string)e.Current;
+ }
+ Thread.Sleep(1);
+ }
+ }
+ catch (Exception ex)
+ {
+ AnyException = ex;
+ }
+ }
+ }
+
class CollisionTester
{
int id;