using System.Runtime.InteropServices; namespace XFESystem.NativeKernel; public static unsafe class ThreadBridge { [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_physical_bytes")] public static ulong PhysicalBytes() => PhysicalMemory.TotalPages * 4096; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_free_physical_bytes")] public static ulong FreePhysicalBytes() => PhysicalMemory.AvailablePages * 4096; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_id")] public static ulong CurrentThread() => Threads.Ready ? Threads.Current->Id : 0; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_stack_bounds")] public static int StackBounds(void** low, void** high) => Threads.StackBounds(low, high) ? 1 : 0; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_start")] public static int Start(delegate* unmanaged entry, void* argument, ulong stackBytes) { if (!Threads.Ready || entry == null) return 0; ulong tls = Native.ThreadTlsCreate(); if (tls == 0) return 0; // Full runtime absent: refuse, never fabricate TLS. ulong id = Threads.Create(entry, argument, stackBytes, tls, true); if (id == 0) Native.ThreadTlsRelease(tls); return id == 0 ? 0 : 1; } [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_sleep")] public static void Sleep(uint milliseconds) => Threads.Sleep(milliseconds); [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_yield")] public static void Yield() => Threads.Yield(); [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_create")] public static void* CreateEvent(int manual, int initial) => (void*)KernelEvents.Create(manual != 0, initial != 0); [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_close")] public static int CloseEvent(void* handle) => KernelEvents.Close((ulong)handle) ? 1 : 0; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_set")] public static int SetEvent(void* handle) => KernelEvents.Set((ulong)handle) ? 1 : 0; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_reset")] public static int ResetEvent(void* handle) => KernelEvents.Reset((ulong)handle) ? 1 : 0; [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_wait")] public static uint WaitEvent(void* handle, uint milliseconds) { ulong value = (ulong)handle; return KernelEvents.Wait(&value, 1, milliseconds); } [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_event_wait_any")] public static uint WaitAny(void** handles, uint count, uint milliseconds) => KernelEvents.Wait((ulong*)handles, count, milliseconds); [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_attach")] public static void Attach(void* runtimeThread) => Threads.Attach(runtimeThread); [UnmanagedCallersOnly(EntryPoint = "xfe_runtime_thread_gc_hijack")] public static int RequestHijack(void* runtimeThread) => Threads.RequestHijack(runtimeThread); }