-
Notifications
You must be signed in to change notification settings - Fork 224
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
5 changed files
with
403 additions
and
272 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.HotExecutor/HECompiler.cs
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,44 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Natasha.CSharp.Compiler; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Natasha.CSharp.Extension.HotExecutor | ||
{ | ||
internal static class HECompiler | ||
{ | ||
private static readonly AssemblyCSharpBuilder _builderCache; | ||
static HECompiler() | ||
{ | ||
_builderCache = new(); | ||
_builderCache.ConfigCompilerOption(opt => opt | ||
.AppendCompilerFlag( | ||
CompilerBinderFlags.IgnoreAccessibility | CompilerBinderFlags.IgnoreCorLibraryDuplicatedTypes | CompilerBinderFlags.GenericConstraintsClause | CompilerBinderFlags.SuppressObsoleteChecks)); | ||
_builderCache | ||
.UseRandomLoadContext() | ||
.UseSmartMode() | ||
.WithoutSemanticCheck() | ||
.WithPreCompilationOptions() | ||
.WithoutPreCompilationReferences() | ||
.WithoutCombineUsingCode(); | ||
} | ||
|
||
public static Assembly ReCompile(IEnumerable<SyntaxTree> trees,bool isRelease) | ||
{ | ||
_builderCache.WithRandomAssenblyName(); | ||
_builderCache.SyntaxTrees.Clear(); | ||
_builderCache.SyntaxTrees.AddRange(trees); | ||
if (isRelease) | ||
{ | ||
_builderCache.WithReleaseCompile(); | ||
} | ||
else | ||
{ | ||
_builderCache.WithDebugCompile(); | ||
} | ||
return _builderCache.GetAssembly(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.HotExecutor/HESpinLock.cs
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,33 @@ | ||
using System.Runtime.CompilerServices; | ||
namespace Natasha.CSharp.Extension.HotExecutor | ||
{ | ||
internal class HESpinLock | ||
{ | ||
|
||
private int _lockCount = 0; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool GetLock() | ||
{ | ||
return Interlocked.CompareExchange(ref _lockCount, 1, 0) == 0; | ||
|
||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void GetAndWaitLock() | ||
{ | ||
while (Interlocked.CompareExchange(ref _lockCount, 1, 0) != 0) | ||
{ | ||
Thread.Sleep(20); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void ReleaseLock() | ||
{ | ||
|
||
_lockCount = 0; | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.