using System; using System.Runtime.InteropServices; namespace RobotNet10.Realtime; /// /// Provides real-time scheduling policy management for Linux preempt_rt. /// public static class RealtimeScheduler { /// /// Sets the scheduling policy and priority for the current thread. /// /// Scheduling policy (SCHED_FIFO, SCHED_RR, etc.) /// Priority (1-99 for real-time policies) /// Thrown when the operation fails. public static void SetSchedulingPolicy(RealtimeSchedulingPolicy policy, int priority) { /*if (priority < LinuxNative.MIN_PRIORITY || priority > LinuxNative.MAX_PRIORITY) { throw new ArgumentOutOfRangeException(nameof(priority), $"Priority must be between {LinuxNative.MIN_PRIORITY} and {LinuxNative.MAX_PRIORITY}"); } var param = new LinuxNative.sched_param { sched_priority = priority }; int result = LinuxNative.sched_setscheduler(0, (int)policy, ref param); if (result != 0) { LinuxNative.ThrowLastError("sched_setscheduler"); }*/ } /// /// Gets the current scheduling policy for the current thread. /// /// The current scheduling policy. public static RealtimeSchedulingPolicy GetSchedulingPolicy() { int policy = LinuxNative.sched_getscheduler(0); if (policy < 0) { LinuxNative.ThrowLastError("sched_getscheduler"); } return (RealtimeSchedulingPolicy)policy; } /// /// Gets the current priority for the current thread. /// /// The current priority. public static int GetPriority() { var param = new LinuxNative.sched_param(); int result = LinuxNative.sched_getparam(0, ref param); if (result != 0) { LinuxNative.ThrowLastError("sched_getparam"); } return param.sched_priority; } /// /// Sets the priority for the current thread (without changing policy). /// /// Priority (1-99 for real-time policies) public static void SetPriority(int priority) { if (priority < LinuxNative.MIN_PRIORITY || priority > LinuxNative.MAX_PRIORITY) { throw new ArgumentOutOfRangeException(nameof(priority), $"Priority must be between {LinuxNative.MIN_PRIORITY} and {LinuxNative.MAX_PRIORITY}"); } var param = new LinuxNative.sched_param { sched_priority = priority }; int result = LinuxNative.sched_setparam(0, ref param); if (result != 0) { LinuxNative.ThrowLastError("sched_setparam"); } } /// /// Gets the minimum priority for a scheduling policy. /// /// Scheduling policy /// Minimum priority public static int GetMinPriority(RealtimeSchedulingPolicy policy) { return LinuxNative.sched_get_priority_min((int)policy); } /// /// Gets the maximum priority for a scheduling policy. /// /// Scheduling policy /// Maximum priority public static int GetMaxPriority(RealtimeSchedulingPolicy policy) { return LinuxNative.sched_get_priority_max((int)policy); } /// /// Yields the CPU to allow other threads to run. /// public static void Yield() { int result = LinuxNative.sched_yield(); if (result != 0) { LinuxNative.ThrowLastError("sched_yield"); } } } /// /// Linux real-time scheduling policies. /// public enum RealtimeSchedulingPolicy { /// /// Normal scheduling policy (default). /// Normal = LinuxNative.SCHED_NORMAL, /// /// First-In-First-Out real-time scheduling policy. /// Threads with higher priority always run before threads with lower priority. /// Fifo = LinuxNative.SCHED_FIFO, /// /// Round-Robin real-time scheduling policy. /// Similar to SCHED_FIFO but threads are time-sliced. /// RoundRobin = LinuxNative.SCHED_RR, /// /// Batch scheduling policy (for batch jobs). /// Batch = LinuxNative.SCHED_BATCH, /// /// Idle scheduling policy (lowest priority). /// Idle = LinuxNative.SCHED_IDLE, /// /// Deadline scheduling policy (for deadline-based scheduling). /// Deadline = LinuxNative.SCHED_DEADLINE, }