161 lines
5.0 KiB
C#
161 lines
5.0 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace RobotNet10.Realtime;
|
|
|
|
/// <summary>
|
|
/// Provides real-time scheduling policy management for Linux preempt_rt.
|
|
/// </summary>
|
|
public static class RealtimeScheduler
|
|
{
|
|
/// <summary>
|
|
/// Sets the scheduling policy and priority for the current thread.
|
|
/// </summary>
|
|
/// <param name="policy">Scheduling policy (SCHED_FIFO, SCHED_RR, etc.)</param>
|
|
/// <param name="priority">Priority (1-99 for real-time policies)</param>
|
|
/// <exception cref="InvalidOperationException">Thrown when the operation fails.</exception>
|
|
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");
|
|
}*/
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current scheduling policy for the current thread.
|
|
/// </summary>
|
|
/// <returns>The current scheduling policy.</returns>
|
|
public static RealtimeSchedulingPolicy GetSchedulingPolicy()
|
|
{
|
|
int policy = LinuxNative.sched_getscheduler(0);
|
|
if (policy < 0)
|
|
{
|
|
LinuxNative.ThrowLastError("sched_getscheduler");
|
|
}
|
|
return (RealtimeSchedulingPolicy)policy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current priority for the current thread.
|
|
/// </summary>
|
|
/// <returns>The current priority.</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the priority for the current thread (without changing policy).
|
|
/// </summary>
|
|
/// <param name="priority">Priority (1-99 for real-time policies)</param>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the minimum priority for a scheduling policy.
|
|
/// </summary>
|
|
/// <param name="policy">Scheduling policy</param>
|
|
/// <returns>Minimum priority</returns>
|
|
public static int GetMinPriority(RealtimeSchedulingPolicy policy)
|
|
{
|
|
return LinuxNative.sched_get_priority_min((int)policy);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the maximum priority for a scheduling policy.
|
|
/// </summary>
|
|
/// <param name="policy">Scheduling policy</param>
|
|
/// <returns>Maximum priority</returns>
|
|
public static int GetMaxPriority(RealtimeSchedulingPolicy policy)
|
|
{
|
|
return LinuxNative.sched_get_priority_max((int)policy);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Yields the CPU to allow other threads to run.
|
|
/// </summary>
|
|
public static void Yield()
|
|
{
|
|
int result = LinuxNative.sched_yield();
|
|
if (result != 0)
|
|
{
|
|
LinuxNative.ThrowLastError("sched_yield");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Linux real-time scheduling policies.
|
|
/// </summary>
|
|
public enum RealtimeSchedulingPolicy
|
|
{
|
|
/// <summary>
|
|
/// Normal scheduling policy (default).
|
|
/// </summary>
|
|
Normal = LinuxNative.SCHED_NORMAL,
|
|
|
|
/// <summary>
|
|
/// First-In-First-Out real-time scheduling policy.
|
|
/// Threads with higher priority always run before threads with lower priority.
|
|
/// </summary>
|
|
Fifo = LinuxNative.SCHED_FIFO,
|
|
|
|
/// <summary>
|
|
/// Round-Robin real-time scheduling policy.
|
|
/// Similar to SCHED_FIFO but threads are time-sliced.
|
|
/// </summary>
|
|
RoundRobin = LinuxNative.SCHED_RR,
|
|
|
|
/// <summary>
|
|
/// Batch scheduling policy (for batch jobs).
|
|
/// </summary>
|
|
Batch = LinuxNative.SCHED_BATCH,
|
|
|
|
/// <summary>
|
|
/// Idle scheduling policy (lowest priority).
|
|
/// </summary>
|
|
Idle = LinuxNative.SCHED_IDLE,
|
|
|
|
/// <summary>
|
|
/// Deadline scheduling policy (for deadline-based scheduling).
|
|
/// </summary>
|
|
Deadline = LinuxNative.SCHED_DEADLINE,
|
|
}
|
|
|