// $Id$
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Org.Apache.Etch.Bindings.Csharp.Support
{
///
/// A free implementation of pool
///
public class FreePool : Pool
{
///
/// Constructs a FreePool with specified maxSize.
///
/// maximum number of free threads at one time
/// after which reject requests.
public FreePool( int maxSize )
{
this.maxSize = maxSize;
}
///
/// Constructs a FreePool with maxSize 50
///
public FreePool()
: this( 50 )
{ }
private readonly int maxSize;
private readonly Dictionary group = new Dictionary();
private Boolean open = true;
///
/// Closes the pool. This just marks the pool as being closed, it doesn't
/// actually do anything to the currently running thread. But no more
/// threads are allowed to start.
///
public void Close()
{
open = false;
}
///
/// Joins each of the threads in this pool until there
/// are none left. The pool will be closed first.
/// Exception:
/// throws ThreadInterruptedException
///
public void Join()
{
Close();
while (true)
{
Thread x;
lock (group)
{
Dictionary.Enumerator e = group.GetEnumerator();
if (!e.MoveNext())
break;
x = e.Current.Key;
group.Remove(x);
}
x.Join();
}
}
///
/// Finds the number of active threads in this pool
///
/// the number of active threads
public int ActiveCount()
{
return group.Count;
}
#region Pool Members
[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");
Thread t = new Thread(
delegate()
{
try
{
d1();
}
catch (Exception e)
{
d2(e);
}
finally
{
lock (group)
{
group.Remove(Thread.CurrentThread);
}
}
});
lock (group)
{
group.Add(t, t);
}
t.Start();
}
#endregion
}
}