|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Anonymus
{
public class ThreadParams
{
public int Index;
public ThreadParams(int Index)
{
this.Index = Index;
}
}
public class MTCompiler
{
public CompilerState State;
public GlobalScopeNode GlobalScope;
public List<Identifier> Identifiers = new List<Identifier>();
public int ProcessedFuncs = 0;
public Barrier Barrier;
bool Result = true;
object SyncObject = new object();
public MTCompiler(CompilerState State, GlobalScopeNode GlobalScope)
{
this.State = State;
this.GlobalScope = GlobalScope;
}
public bool Run()
{
var SingleThread = true;//false;
var ThreadCount = SingleThread ? 1 : Environment.ProcessorCount;
if (ThreadCount != Environment.ProcessorCount || SingleThread)
State.Messages.Add(MessageId.CoreCountWarning);
if (ThreadCount != 1)
Barrier = new Barrier(ThreadCount);
else Barrier = null;
var Threads = new Thread[ThreadCount - 1];
var Params = (ThreadParams)null;
for (var i = 1; i < ThreadCount; i++)
{
Threads[i - 1] = new Thread(CompilerThread);
Params = new ThreadParams(i);
Threads[i - 1].Start(Params);
}
Params = new ThreadParams(0);
CompilerThread(Params);
for (var i = 0; i < ThreadCount - 1; i++)
Threads[i].Join();
return Result;
}
public void CompilerThread(object p_oParams)
{
var Params = p_oParams as ThreadParams;
var Index = Params.Index;
var PlugIn = GlobalScope.GetPlugIn();
foreach (var Id in MIdentifier())
ProcessId(PlugIn, Id);
Block();
if (!Result) return;
if (Index == 0) ProcessedFuncs = 0;
Block();
foreach (var Id in MIdentifier())
{
var Func = Id as Function;
if (Func != null && Func.DeclInThis && Func.FunctionScope != null)
Func.FunctionScope.CreateAsmCode();
}
}
private void Block()
{
if (Barrier != null) Barrier.SignalAndWait();
}
private void ProcessId(MultiPlugIn PlugIn, Identifier Id)
{
if (!Id.DeclInThis)
return;
if (Id is Function)
{
var Func = Id as Function;
if (Func.FunctionScope != null && !Func.FunctionScope.ProcessCode())
Result = false;
}
else if (Id is Variable)
{
var Var = Id as Variable;
if (Var.InitString != null && !Var.CalcValue(PlugIn))
Result = false;
}
}
public Identifier GetIdentifier()
{
lock (SyncObject)
{
var Ret = (Identifier)null;
if (ProcessedFuncs < Identifiers.Count)
{
Ret = Identifiers[ProcessedFuncs];
ProcessedFuncs++;
}
return Ret;
}
}
public IEnumerable<Identifier> MIdentifier()
{
var Id = (Identifier)null;
while (true)
{
Id = GetIdentifier();
if (Id != null) yield return Id;
else break;
}
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.