You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by sc...@apache.org on 2008/12/01 20:58:44 UTC
svn commit: r722205 -
/incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs
Author: sccomer
Date: Mon Dec 1 11:58:44 2008
New Revision: 722205
URL: http://svn.apache.org/viewvc?rev=722205&view=rev
Log:
fix for ETCH-23: FreePool has synchronization issues with quick back to back calls to Run.
Modified:
incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs
Modified: incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs?rev=722205&r1=722204&r2=722205&view=diff
==============================================================================
--- incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs (original)
+++ incubator/etch/trunk/binding-csharp/runtime/src/main/csharp/Etch/Support/FreePool.cs Mon Dec 1 11:58:44 2008
@@ -15,6 +15,7 @@
// under the License.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
@@ -33,7 +34,6 @@
public FreePool( int maxSize )
{
this.maxSize = maxSize;
- myThreads = new Thread[ maxSize ];
}
/// <summary>
@@ -43,14 +43,9 @@
: this( 50 )
{ }
- private int maxSize;
- private int currentThreads = 0;
- Thread[] myThreads;
-
- void Run()
- {
- //donothing
- }
+ private readonly int maxSize;
+
+ private readonly Dictionary<Thread, Thread> group = new Dictionary<Thread, Thread>();
private Boolean open = true;
@@ -73,10 +68,19 @@
public void Join()
{
Close();
- for ( int i = 0; i < currentThreads; i++ )
+ while (true)
{
- if ( myThreads[ i ].IsAlive )
- myThreads[ i ].Join();
+ Thread x;
+ lock (group)
+ {
+ Dictionary<Thread, Thread>.Enumerator e = group.GetEnumerator();
+ if (!e.MoveNext())
+ break;
+
+ x = e.Current.Key;
+ group.Remove(x);
+ }
+ x.Join();
}
}
@@ -86,13 +90,7 @@
/// <returns>the number of active threads</returns>
public int ActiveCount()
{
- int count = 0;
- for ( int i = 0; i < currentThreads; i++ )
- {
- if ( myThreads[ i ].IsAlive )
- count++;
- }
- return count;
+ return group.Count;
}
#region Pool Members
@@ -100,24 +98,35 @@
[MethodImpl( MethodImplOptions.Synchronized )]
public void Run( RunDelegate d1, ExceptionDelegate d2 )
{
- if ( !open || ActiveCount() >= maxSize )
- throw new Exception( "free pool thread count exceeded or pool closed" );
+ if (!open || ActiveCount() >= maxSize)
+ throw new Exception("free pool thread count exceeded or pool closed");
+
+ Thread t = new Thread(
+ delegate()
+ {
+ try
+ {
+ d1();
+ }
+ catch (Exception e)
+ {
+ d2(e);
+ }
+ finally
+ {
+ lock (group)
+ {
+ group.Remove(Thread.CurrentThread);
+ }
+ }
+ });
- myThreads[ currentThreads ] = new Thread(
- new ThreadStart( delegate()
- {
- try
- {
- d1();
- }
- catch ( Exception e )
- {
- d2( e );
- }
- } ) );
+ lock (group)
+ {
+ group.Add(t, t);
+ }
- myThreads[ currentThreads ].Start();
- currentThreads++;
+ t.Start();
}
#endregion