Skip to content

Commit

Permalink
fixes native loading, add native libraries as deps, fixes threading
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Jun 12, 2024
1 parent 71ce867 commit 3761b78
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 98 deletions.
94 changes: 87 additions & 7 deletions runtime/ishtar.vm.libuv/LibUV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace ishtar.vm.libuv;
using System.Runtime.InteropServices;


public static class LibUV
public static unsafe class LibUV
{
public const string LIBNAME = "libuv";

Expand All @@ -16,20 +16,39 @@ public static class LibUV
[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nint uv_loop_new();

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_loop_init(nint loop);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_run(nint loop, uv_run_mode mode);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void uv_stop(nint loop);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_async_init(nint loop, nint handle, uv_async_cb cb);
public static extern int uv_async_init(nint loop, nint handle, uv_async_cb asyncCallback);
[StructLayout(LayoutKind.Sequential)]
public struct uv_async_t
{
public void* data;
public void* loop;
}
//[StructLayout(LayoutKind.Sequential)]
//public struct uv_loop_t
//{
// public void* data;
// public uint active_handles;
//}


[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void uv_loop_set_data(nint loop, void* data);
[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void* uv_loop_get_data(nint loop);


[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_async_send(nint async);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_async_send(nint handle);
public static extern int uv_loop_close(nint loop);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void uv_close(nint handle, uv_close_cb cb);
Expand Down Expand Up @@ -76,13 +95,19 @@ public static class LibUV
[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void uv_mutex_destroy(ref uv_mutex_t handle);

[DllImport(LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int uv_replace_allocator(uv_alloc_cb alloc, uv_free_cb free);

public delegate void uv_async_cb(nint handle);
public delegate void uv_close_cb(nint handle);
public delegate void uv_timer_cb(nint handle);
public delegate void uv_thread_cb(nint arg);
public delegate void uv_after_work_cb(nint req, int status);
public delegate void uv_work_cb(nint req);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate nint uv_alloc_cb(nint size, nint align, nint zero_fill);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_free_cb(nint ptr);

public enum uv_run_mode
{
Expand Down Expand Up @@ -120,4 +145,59 @@ public struct uv_mutex_t
{
private nint handle;
}

public struct uv_buf_t
{
private readonly IntPtr _field0;
private readonly IntPtr _field1;

public uv_buf_t(IntPtr memory, int len, bool IsWindows)
{
if (IsWindows)
{
_field0 = (IntPtr)len;
_field1 = memory;
}
else
{
_field0 = memory;
_field1 = (IntPtr)len;
}
}
}

public enum HandleType
{
Unknown = 0,
ASYNC,
CHECK,
FS_EVENT,
FS_POLL,
HANDLE,
IDLE,
NAMED_PIPE,
POLL,
PREPARE,
PROCESS,
STREAM,
TCP,
TIMER,
TTY,
UDP,
SIGNAL,
}

public enum RequestType
{
Unknown = 0,
REQ,
CONNECT,
WRITE,
SHUTDOWN,
UDP_SEND,
FS,
WORK,
GETADDRINFO,
GETNAMEINFO,
}
}
3 changes: 0 additions & 3 deletions runtime/ishtar.vm.libuv/ishtar.vm.libuv.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Libuv" Version="1.10.0" />
</ItemGroup>
</Project>
38 changes: 5 additions & 33 deletions runtime/ishtar.vm/ishtar.vm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<EnablePreviewFeatures>True</EnablePreviewFeatures>
<GenerateRequiresPreviewFeaturesAttribute>True</GenerateRequiresPreviewFeaturesAttribute>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x64;win-arm64;osx-x64;osx-arm64;linux-x64;linux-arm64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PublishAot>true</PublishAot>
Expand All @@ -19,9 +19,8 @@
<IlcTrimMetadata>false</IlcTrimMetadata>
<IlcDisableReflection>false</IlcDisableReflection>
<DebugType>embedded</DebugType>
<DefineConstants>$(DefineConstants);EXPERIMENTAL_JIT;BOEHM_GC;VALIDATE_RUNTIME_TOKEN;USE_MANAGED_COLLECTIONS</DefineConstants>
<SingleAppImage>true</SingleAppImage>

<DefineConstants>$(DefineConstants);EXPERIMENTAL_JIT;BOEHM_GC;VALIDATE_RUNTIME_TOKEN;USE_MANAGED_COLLECTIONS;DEBUG</DefineConstants>
<SingleAppImage>false</SingleAppImage>
</PropertyGroup>


Expand All @@ -45,15 +44,10 @@
<DefineConstants>$(DefineConstants);EXPERIMENTAL_JIT;BOEHM_GC;VALIDATE_RUNTIME_TOKEN;USE_MANAGED_COLLECTIONS</DefineConstants>
</PropertyGroup>


<ItemGroup Condition="$(RuntimeIdentifier.StartsWith('win'))">
<DirectPInvoke Include="gc" />
<NativeLibrary Include="includes\gc.lib" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Iced" Version="1.20.0" />
<PackageReference Include="ishtar.bdwgc" Version="8.2.6.40" GeneratePathProperty="true" />
<PackageReference Include="ishtar.libuv" Version="1.48.0.10" GeneratePathProperty="true" />
<PackageReference Include="LLVMSharp" Version="16.0.0" />
<PackageReference Include="LLVMSharp.Interop" Version="16.0.0" />
</ItemGroup>
Expand All @@ -65,27 +59,5 @@
<ProjectReference Include="..\ishtar.base\ishtar.base.csproj" />
<ProjectReference Include="..\ishtar.vm.libuv\ishtar.vm.libuv.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="includes\gc.lib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="includes\libgc.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="includes\libgc.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="includes\libgc_arm64.dylib">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="includes\libgc_x64.dylib">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="sample_native_library.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>



</Project>
34 changes: 17 additions & 17 deletions runtime/ishtar.vm/runtime/gc/BoehmGCLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ public class Native
{
public static void Load(RuntimeInfo info)
{
switch (info)
{
case { isWindows: true, Architecture: Architecture.X64 }:
NativeLibrary.Load("./includes/libgc.dll");
break;
case { isLinux: true, Architecture: Architecture.X64 }:
NativeLibrary.Load("./includes/libgc.so");
break;
case { isOSX: true, Architecture: Architecture.X64 }:
NativeLibrary.Load("./includes/libgc_x64.dylib");
break;
case { isOSX: true, Architecture: Architecture.Arm64 }:
NativeLibrary.Load("./includes/libgc_arm64.dylib");
break;
default:
throw new NotSupportedException($"Platform is not support gc loading");
}
//switch (info)
//{
// case { isWindows: true, Architecture: Architecture.X64 }:
// NativeLibrary.Load("libgc");
// break;
// case { isLinux: true, Architecture: Architecture.X64 }:
// NativeLibrary.Load("./includes/libgc.so");
// break;
// case { isOSX: true, Architecture: Architecture.X64 }:
// NativeLibrary.Load("./includes/libgc_x64.dylib");
// break;
// case { isOSX: true, Architecture: Architecture.Arm64 }:
// NativeLibrary.Load("./includes/libgc_arm64.dylib");
// break;
// default:
// throw new NotSupportedException($"Platform is not support gc loading");
//}
}

public const string LIBNAME = "libgc";
Expand Down
15 changes: 15 additions & 0 deletions runtime/ishtar.vm/runtime/gc/IshtarGC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ishtar.runtime.gc
using vein.runtime;
using static vein.runtime.VeinTypeCode;
using LLVMSharp;
using ishtar.vm.libuv;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void IshtarFinalizationProc(nint p, nint i);
Expand Down Expand Up @@ -638,6 +639,20 @@ public static void FreeConcurrentDictionary<TKey, TValue>(NativeConcurrentDictio
where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged
=> NativeConcurrentDictionary<TKey, TValue>.Free(list, _allocator);

public static void uv_replace_allocators()
{
var result = LibUV.uv_replace_allocator(uv_alloc, uv_free);
}

private static nint uv_alloc(nint size, nint align, nint zero_fill)
=> (nint)gcLayout.alloc((uint)size);

private static void uv_free(nint ptr)
{
void* p = (void*)ptr;
gcLayout.free(&p);
}

public static T* AllocateImmortal<T>() where T : unmanaged
{
//return (T*)NativeMemory.AllocZeroed((uint)sizeof(T));
Expand Down
6 changes: 3 additions & 3 deletions runtime/ishtar.vm/runtime/io/IshtarRawThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace ishtar.io;

public readonly unsafe struct IshtarRawThread(
LibUV.uv_thread_t threadId,
delegate*<nint, void> frame,
delegate*<IshtarRawThread*, void> frame,
RuntimeIshtarModule* mainModule)
{
public readonly RuntimeIshtarModule* MainModule = mainModule;
public readonly LibUV.uv_thread_t threadId = threadId;
public readonly delegate*<nint, void> callFrame = frame;
}
public readonly delegate*<IshtarRawThread*, void> callFrame = frame;
}
5 changes: 3 additions & 2 deletions runtime/ishtar.vm/runtime/io/IshtarThreading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ishtar.io;

public unsafe struct IshtarThreading
{
public IshtarRawThread* CreateRawThread(RuntimeIshtarModule* mainModule, delegate*<nint, void> frame)
public IshtarRawThread* CreateRawThread(RuntimeIshtarModule* mainModule, delegate*<IshtarRawThread*, void> frame)
{
var thread = IshtarGC.AllocateImmortal<IshtarRawThread>();
LibUV.uv_thread_create(out var threadId, executeRaw, (IntPtr)thread);
Expand Down Expand Up @@ -52,11 +52,12 @@ private static void execute(nint arg)

private static void executeRaw(nint arg)
{
var thread = (IshtarRawThread*)arg;
var stackbase = new GC_stack_base();

BoehmGCLayout.Native.GC_get_stack_base(&stackbase);
BoehmGCLayout.Native.GC_register_my_thread(&stackbase);
((IshtarRawThread*)arg)->callFrame(arg);
thread->callFrame(thread);
BoehmGCLayout.Native.GC_unregister_my_thread();
}
}
Loading

0 comments on commit 3761b78

Please sign in to comment.