Skip to main content

Soft real-time scheduling

Properties and state machine

Soft real-time scheduling for the Windows user-mode PDO thread: bind the PDO thread to a reserved core and raise its thread base priority. It does not change the process priority class or the process CPU affinity by default; when the capability is missing, the policy is off, or the real-time transport already owns the cycle, every call is a no-op with a reason text — no exception, no effect on PDO frames.

Applied automatically when the thread starts

The PDO thread applies soft real-time itself when it starts (reserved core + thread base priority 31) and applies it again after the thread is recreated, so the caller does not have to repeat it. Most applications only need SoftRealtimeQuery() to observe the state; an explicit SoftRealtimeApply() suits the case where you must confirm early in startup that it has taken effect.

Modes​

ec_srt_mode_t has the same values as the runtime implementation; do not renumber them:

ValueNameMeaning
0EC_SRT_MODE_UNSUPPORTEDPlatform / reserved cores / privilege not available
1EC_SRT_MODE_INACTIVECapability present but not in effect (policy off, or waiting for the PDO thread to be created)
2EC_SRT_MODE_ACTIVEThe PDO thread is bound and promoted, or the process is already in the REALTIME priority class
3EC_SRT_MODE_KERNEL_OFFLOADThis master's cycle is handled by the real-time transport thread (process data offload)

Status structure​

#define EC_SRT_REASON_MAX 256

typedef struct ec_srt_status {
ec_srt_mode_t mode;
BOOL supported;
BOOL process_realtime;
BOOL applied_by_sdk;
uint32_t reserved_core_count;
uint64_t reserved_mask;
int pdo_core;
char reason[EC_SRT_REASON_MAX];
} ec_srt_status_t;
FieldTypeDescription
modeec_srt_mode_tCurrent mode (see the table above)
supportedBOOLWhether this machine / process has the user-mode soft real-time capability
process_realtimeBOOLWhether the process priority class is currently REALTIME_PRIORITY_CLASS (read live)
applied_by_sdkBOOLWhether the process real-time class was set by this SDK (decides whether SoftRealtimeDisable restores it)
reserved_core_countuint32_tNumber of reserved processor cores; 0 = no reserved cores configured
reserved_maskuint64_tLow 64 bits of the reserved core bitmap (LSB-first, bit0 = core 0)
pdo_coreintCore bound to this master's PDO thread; -1 = not bound / unreadable. With master_index = 0 it reports the core used by the most recent successful binding
reasonchar[256]Reason text in Chinese (UTF-8, fixed static text, never empty)

reason is the single authoritative explanation for troubleshooting: any request that did not take effect answers "why not" through it, so do not treat a FALSE return as an error code to be guessed.

Environment policy​

The enabling policy is controlled by the DARRA_ECAT_SRT environment variable (the variable name constant is EC_SRT_ENV_NAME):

ValueBehaviour
unset / empty / unrecognisedauto: takes effect only when the real-time transport is unavailable + reserved cores are valid + the process holds SeIncreaseBasePriorityPrivilege
offhard off: not applied even when explicitly requested
onforce the attempt
autosame as the default, goes through the capability gate

Capability test and core selection​

All three conditions must hold for supported:

  • Windows platform;
  • the reserved processor core count is 1..N-1;
  • the process token holds SeIncreaseBasePriorityPrivilege (administrators hold it by default).

Core selection: take the (master_index - 1)-th set core counting the reserved cores from high to low; when the master number exceeds the reserved core count it wraps around and never lands on a non-reserved core.

Thread level and process level​

DimensionThread level (default)Process level (strict)
Entry pointsSoftRealtimeApply / SoftRealtimeApplyAuto / SoftRealtimeEnsureSoftRealtimeEnable / SoftRealtimeEnsureProcess
TargetThat master's PDO threadEvery thread in the whole process
Process priority classNot modifiedSet to REALTIME_PRIORITY_CLASS
IntrusivenessSmall: host UI / GC / other threads are unaffectedLarge: NORMAL effective priorities are lifted into the real-time range
CPU affinityBinds the PDO thread onlyAlso does not change the process CPU affinity

Use the process-level entry points only when the whole host can tolerate real-time range scheduling; ordinary applications stay at thread level. Neither form changes the process CPU affinity, so the existing placement of state guards, diagnostics and asynchronous notification threads is not pushed aside.

Function overview​

CategoryFunctionReturn typeRead/writeDescription
Status querySoftRealtimeQuery(mi, out)BOOLread-onlyFills ec_srt_status_t; TRUE = filled, which does not mean enabled
ec_srt_policy_probe(mi)intread-onlyReads the policy only: 0 = auto, 1 = off, 2 = on; a failed query is treated as auto
Thread-level applySoftRealtimeApply(mi)BOOLwrite-onlyBinds the PDO thread to a reserved core + thread base priority 31; does not change the process priority class
SoftRealtimeApplyAuto(mi)BOOLwrite-onlySame action as SoftRealtimeApply, with the policy handled inside the runtime
SoftRealtimeEnsure(mi)BOOLwrite-onlyAlias for SoftRealtimeApply
Process-level applySoftRealtimeEnable()BOOLwrite-onlyStrict mode: sets the whole process to the REALTIME priority class
SoftRealtimeEnsureProcess()BOOLwrite-onlyAlias for SoftRealtimeEnable
DisableSoftRealtimeDisable()voidwrite-onlyClears all master registrations; restores only the process real-time class set by this SDK
Compatibility entrySoftRealtimeOptIn()BOOLread-onlyOld-signature compatibility; does not change any scheduling state
PollingSoftRealtimePoll(mi)BOOLread-onlyTRUE = nothing left to retry; FALSE = still worth waiting (a failed query also returns FALSE)

Function reference​

SoftRealtimeQuery()​

BOOL SoftRealtimeQuery(uint16_t master_index, ec_srt_status_t* out);

Queries the soft real-time scheduling state; read-only, changes nothing.

  • Return test: TRUE = filled (which does not mean enabled); FALSE = out is NULL or the query failed.
  • Idempotence: a read-only snapshot, callable at any rate and suitable for a status refresh loop.
  • When to use it: check mode before any soft real-time decision; after any call returns FALSE, read reason to locate the cause. With master_index = 0 it reports only the process-level state.

SoftRealtimeEnable()​

BOOL SoftRealtimeEnable(void);

Process-level enable (idempotent, strict mode): sets the whole process to REALTIME_PRIORITY_CLASS.

  • Return test: TRUE = the process is now in the REALTIME priority class; FALSE = not achieved (see SoftRealtimeQuery for the reason). The test is mode == ACTIVE only; when the state is EC_SRT_MODE_KERNEL_OFFLOAD it does not return TRUE.
  • Idempotence: yes, repeated calls have no side effects.
  • When to use it: when you have confirmed that every thread in the host can tolerate real-time range scheduling. To promote only the PDO thread, use SoftRealtimeApply / SoftRealtimeEnsure.
Intrusiveness

Process level lifts threads with a NORMAL effective priority into the real-time range as well, so UI, GC and logging threads may preempt the real-time thread. Use the thread-level entry points unless there is a clear reason.

SoftRealtimeDisable()​

void SoftRealtimeDisable(void);

Disables soft real-time (process level): clears every master registration and restores the priority class saved before enabling, but only when the process REALTIME class was set by this SDK (applied_by_sdk).

  • Return test: no return value.
  • Idempotence: yes, harmless when nothing was enabled.
  • When to use it: explicit cleanup before the process exits, or when scheduling control has to be handed back to the host.

The affinity and priority already bound to the PDO thread live as long as the thread and are not rolled back here (there is no "reset the thread onto all cores" semantics).

SoftRealtimeApply()​

BOOL SoftRealtimeApply(uint16_t master_index);

Applies per master (default thread level): binds that master's PDO thread to a reserved core + thread base priority 31 without changing the process priority class. If the thread does not exist yet the request is registered as pending and takes effect automatically when the thread starts.

  • Return test: TRUE = in effect or already owned by the real-time transport (KernelOffload); FALSE = not achieved (see SoftRealtimeQuery for the reason).
  • Idempotence: yes, repeated calls do not conflict with the automatic apply after a thread rebuild.
  • When to use it: the default entry point. Call it once early in startup; you do not need to poll for the thread to be created.

SoftRealtimeApplyAuto()​

BOOL SoftRealtimeApplyAuto(uint16_t master_index);

Automatic-mode entry point: the same action as SoftRealtimeApply, with the off / on / auto policy of DARRA_ECAT_SRT handled uniformly inside the runtime.

  • Return test: same as SoftRealtimeApply.
  • Idempotence: same as SoftRealtimeApply.
  • When to use it: when the call site wants the semantics to say explicitly that the environment policy decides whether it really applies; behaviour is identical to SoftRealtimeApply.

SoftRealtimeOptIn()​

BOOL SoftRealtimeOptIn(void);

An entry point kept for the old signature (formerly the "process-level opt-in flag"). The current implementation no longer needs an opt-in, and this function changes no scheduling state.

  • Return test: always TRUE, which only states the fact that no opt-in is needed; it does not mean soft real-time is in effect. Whether it is in effect is always decided by mode from SoftRealtimeQuery.
  • Idempotence: yes (it never changed state anyway).
  • When to use it: source compatibility only. New code must not use its return value as proof that it is enabled.

SoftRealtimeEnsure()​

BOOL SoftRealtimeEnsure(uint16_t master_index);

The explicitly requested thread-level entry point; it is an alias for SoftRealtimeApply and its semantics follow the runtime implementation.

  • Return test: same as SoftRealtimeApply.
  • Idempotence: same as SoftRealtimeApply.
  • When to use it: equivalent to SoftRealtimeApply; pick the name that reads better in your code.

SoftRealtimeEnsureProcess()​

BOOL SoftRealtimeEnsureProcess(void);

The process-level Ensure, an alias for SoftRealtimeEnable.

  • Return test: same as SoftRealtimeEnable.
  • Idempotence: same as SoftRealtimeEnable.
  • When to use it: same as SoftRealtimeEnable; pick the name that reads better in your code.

SoftRealtimePoll()​

BOOL SoftRealtimePoll(uint16_t master_index);

Advances a pending binding (old-signature compatibility). The runtime applies the binding automatically when the PDO thread starts and has no background retry table, so this function is equivalent to "probe once whether it is already done".

  • Return test:
    • TRUE = nothing left to retry: already in effect (ACTIVE), owned by the real-time transport (KERNEL_OFFLOAD), capability missing (UNSUPPORTED), policy hard off (off), or already core-bound;
    • FALSE = still worth waiting: capability present, policy not off, PDO thread not yet created (pdo_core < 0 and reserved_core_count > 0).
  • Idempotence: a read-only probe, callable repeatedly.
  • When to use it: poll at your own pace when you need to "wait for the thread to be created" early in startup. A failed query also returns FALSE (unknown state is not completion), so set your own retry limit; to distinguish "query failed" from "still waiting", call SoftRealtimeQuery separately first.

ec_srt_policy_probe()​

int ec_srt_policy_probe(uint16_t master_index);

Reads the DARRA_ECAT_SRT policy only and changes no state.

  • Return test: 0 = auto, 1 = off (hard off), 2 = on; a failed query is treated as auto (returns 0).
  • Idempotence: read-only, callable repeatedly.
  • When to use it: when diagnosing "why did it not take effect" to tell "policy off" apart from "capability missing". Ordinary applications and polling do not need it.

Full example​

Query the state, apply on demand when it is not in effect, and on failure only read reason — no exception handling and no retry storm:

#include "master/soft_realtime.h"

static void ensure_soft_realtime(uint16_t mi)
{
ec_srt_status_t st;

if (!SoftRealtimeQuery(mi, &st)) {
printf("软实时状态查询失败\n"); /* Soft real-time status query failed */
return;
}

printf("mode=%d supported=%d process_realtime=%d pdo_core=%d reason=%s\n",
(int)st.mode, (int)st.supported, (int)st.process_realtime,
st.pdo_core, st.reason);

/* Already in effect or already taken over by the kernel: nothing more to do*/
if (st.mode == EC_SRT_MODE_ACTIVE || st.mode == EC_SRT_MODE_KERNEL_OFFLOAD)
return;

/* The policy is hard-off / the capability is absent: the application would also be a no-op, so just return*/
if (st.mode == EC_SRT_MODE_UNSUPPORTED || ec_srt_policy_probe(mi) == 1)
return;

/* Thread-level application: no exception is thrown on failure, only the reason is written into reason*/
if (!SoftRealtimeApply(mi)) {
if (SoftRealtimeQuery(mi, &st))
printf("未能应用软实时: %s\n", st.reason); /* Could not apply soft real-time: %s */
return;
}

(void)SoftRealtimeQuery(mi, &st);
printf("已应用: mode=%d pdo_core=%d\n", (int)st.mode, st.pdo_core); /* Applied: mode=%d pdo_core=%d */
}

When you need process level, replace SoftRealtimeApply(mi) with SoftRealtimeEnable() and accept its effect on every thread in the host.

Usage notes​

  • Failure is a no-op, not an exception: no entry point throws or affects PDO frames; when a request is not achieved, read reason for the reason text.
  • Real-time transport takes priority: when the low-level real-time transport is available, the mode only reports KERNEL_OFFLOAD and nothing is changed in user mode.
  • Policy off is a hard off: SoftRealtimeApply / SoftRealtimeApplyAuto do not take effect when it is off, and SoftRealtimePoll returns TRUE immediately so the caller does not spin forever.
  • SoftRealtimeOptIn is not a status test: it always returns TRUE and only means no opt-in is needed; whether it is enabled is decided only by mode from SoftRealtimeQuery.
  • SoftRealtimePoll returns FALSE on a failed query: that is "state unknown", not "done"; call SoftRealtimeQuery separately when you need to tell them apart.
  • SoftRealtimeDisable does not roll back threads: it only clears the master registrations and restores the process real-time class set by this SDK; affinity and priority already bound to a thread live as long as the thread.