|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace TLS
{
//=============================================================================================
abstract class TestBase
{
//-----------------------------------------------------------------------------------------
public abstract int Factor { get; }
public abstract bool Multithreaded { get; }
public virtual bool Block { get { return false; } }
public virtual long TotalWork { get { return Glob.Repetitions * Factor * ( Multithreaded ? Environment.ProcessorCount : 1 ); } }
public virtual long Done { get; protected set; }
//-----------------------------------------------------------------------------------------
// start and finish
public void Run()
{
if ( Block ) WorkMaster();
else new Thread( new ThreadStart( WorkMaster ) ) { IsBackground = true }.Start();
}
void WorkMaster()
{
Done = 0;
Work();
RaiseFinished();
}
protected abstract void Work();
public event EventHandler Finished;
protected void RaiseFinished()
{
var d = Finished;
if ( d != null ) d( null, EventArgs.Empty );
}
//-----------------------------------------------------------------------------------------
// shared work members
void DoWorkCore( Action<long, State> work, State state )
{
var oldPriority = Thread.CurrentThread.Priority;
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
try
{
Glob.Stop = false;
var max = Glob.Repetitions * Factor;
for ( long i = 0 ; i < max && !Glob.Stop ; ++i ) work( i, state );
}
finally
{
Thread.CurrentThread.Priority = oldPriority;
}
}
//-----------------------------------------------------------------------------------------
// non-TLS work members
public delegate void WorkDelegate( long i );
public Action[] CreateActions( WorkDelegate work )
{
var actions = new Action[ Environment.ProcessorCount ];
for ( int i = 0 ; i < actions.Length ; i++ )
actions[ i ] = new Action( delegate { DoWork( work ); } );
return actions;
}
public void DoWork( WorkDelegate work )
{
DoWorkCore( ( i, state ) => work( i ), null );
}
//-----------------------------------------------------------------------------------------
// TLS work members
protected class State
{
public long Done = 0;
}
Common.TLS<State> _TLS = null;
protected Common.TLS<State> TLS { get { return _TLS; } set { _TLS = value; } }
public delegate void WorkDelegate<DATA>( long i, DATA state );
protected Action[] CreateActions( WorkDelegate<State> work )
{
var actions = new Action[ Environment.ProcessorCount ];
for ( int i = 0 ; i < actions.Length ; i++ )
actions[ i ] = new Action( delegate { DoWork( work ); } );
return actions;
}
protected void DoWork( WorkDelegate<State> work )
{
DoWorkCore( ( i, state ) => work( i, state ), _TLS.Current );
}
//-----------------------------------------------------------------------------------------
}
//=============================================================================================
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since
In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.
I now live near idyllic Bournemouth in England.
I can work 'virtually' anywhere!