-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
491 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace Mocha.Common; | ||
|
||
public sealed class TaskPool<T> | ||
{ | ||
public delegate Task TaskCallback( T[] taskQueue ); | ||
public delegate void Continuation(); | ||
|
||
private readonly Task[] _tasks; | ||
|
||
private TaskPool( T[] queue, TaskCallback taskStart ) | ||
{ | ||
var maxTasks = (int)(Environment.ProcessorCount * 0.5) - 1; | ||
var batchSize = queue.Length / maxTasks; | ||
|
||
if ( batchSize == 0 ) | ||
batchSize = 1; | ||
|
||
var batched = queue | ||
.Select( ( value, index ) => new | ||
{ | ||
Value = value, | ||
Index = index | ||
} ) | ||
.GroupBy( p => p.Index / batchSize ) | ||
.Select( g => g.Select( p => p.Value ).ToArray() ) | ||
.ToArray(); | ||
|
||
_tasks = new Task[batched.Length]; | ||
for ( int i = 0; i < batched.Length; i++ ) | ||
{ | ||
var taskQueue = batched[i]; | ||
|
||
_tasks[i] = Task.Run( () => taskStart( taskQueue ) ); | ||
} | ||
} | ||
|
||
public static TaskPool<T> Dispatch( T[] queue, TaskCallback taskStart ) | ||
{ | ||
return new TaskPool<T>( queue, taskStart ); | ||
} | ||
|
||
public static TaskPool<T> Dispatch( IEnumerable<T> queue, TaskCallback taskStart ) | ||
{ | ||
return new TaskPool<T>( queue.ToArray(), taskStart ); | ||
} | ||
|
||
public TaskPool<T> Then( Continuation continuation ) | ||
{ | ||
Task.WhenAll( _tasks ).ContinueWith( t => continuation() ).Wait(); | ||
return this; | ||
} | ||
|
||
public async Task WaitForCompleteAsync() | ||
{ | ||
await Task.WhenAll( _tasks ); | ||
} | ||
|
||
public void WaitForComplete() | ||
{ | ||
WaitForCompleteAsync().Wait(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace MochaTool.InteropGen.Parsing; | ||
|
||
/// <summary> | ||
/// Builds a representation of a C++ container. | ||
/// </summary> | ||
internal sealed class ContainerBuilder | ||
{ | ||
/// <summary> | ||
/// The type of container that is being built. | ||
/// </summary> | ||
internal ContainerType Type { get; } | ||
/// <summary> | ||
/// The name of the container. | ||
/// </summary> | ||
internal string Name { get; } | ||
|
||
/// <summary> | ||
/// Whether or not the container has any items within it. | ||
/// </summary> | ||
internal bool IsEmpty => fields.Count == 0 && methods.Count == 0; | ||
|
||
private readonly ImmutableArray<Variable>.Builder fields; | ||
private readonly ImmutableArray<Method>.Builder methods; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="ContainerBuilder"/>. | ||
/// </summary> | ||
/// <param name="type">The type of the container.</param> | ||
/// <param name="name">The name of the container.</param> | ||
internal ContainerBuilder( ContainerType type, string name ) | ||
{ | ||
Type = type; | ||
Name = name; | ||
|
||
fields = ImmutableArray.CreateBuilder<Variable>(); | ||
methods = ImmutableArray.CreateBuilder<Method>(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a new field to the container. | ||
/// </summary> | ||
/// <param name="field">The field to add.</param> | ||
/// <returns>The same instance of <see cref="ContainerBuilder"/>.</returns> | ||
internal ContainerBuilder AddField( Variable field ) | ||
{ | ||
fields.Add( field ); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a new method to the container. | ||
/// </summary> | ||
/// <param name="method">The method to add.</param> | ||
/// <returns>The same instance of <see cref="ContainerBuilder"/>.</returns> | ||
internal ContainerBuilder AddMethod( Method method ) | ||
{ | ||
methods.Add( method ); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Constructs a new instance of the container. | ||
/// </summary> | ||
/// <returns>A new container instance that implements the <see cref="IContainerUnit"/> interface.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">Thrown when trying to build a container with an invalid type.</exception> | ||
internal IContainerUnit Build() | ||
{ | ||
fields.Capacity = fields.Count; | ||
methods.Capacity = methods.Count; | ||
|
||
return Type switch | ||
{ | ||
ContainerType.Class => Class.Create( Name, fields.MoveToImmutable(), methods.MoveToImmutable() ), | ||
ContainerType.Namespace => Namespace.Create( Name, fields.MoveToImmutable(), methods.MoveToImmutable() ), | ||
ContainerType.Struct => Struct.Create( Name, fields.MoveToImmutable(), methods.MoveToImmutable() ), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace MochaTool.InteropGen.Parsing; | ||
|
||
/// <summary> | ||
/// Defines a type of container defined in C++. | ||
/// </summary> | ||
internal enum ContainerType : byte | ||
{ | ||
Class, | ||
Namespace, | ||
Struct, | ||
Invalid = 255 | ||
} |
Oops, something went wrong.