Skip to content

Commit

Permalink
分离功能,减少侵入
Browse files Browse the repository at this point in the history
  • Loading branch information
NMSAzulX committed Jun 11, 2024
1 parent c6d81a0 commit 01d17c4
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 272 deletions.
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();
}
}
}
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;

}
}
}
Loading

0 comments on commit 01d17c4

Please sign in to comment.