Soft real-time scheduling
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.
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:
| Value | Name | Meaning |
|---|---|---|
| 0 | EC_SRT_MODE_UNSUPPORTED | Platform / reserved cores / privilege not available |
| 1 | EC_SRT_MODE_INACTIVE | Capability present but not in effect (policy off, or waiting for the PDO thread to be created) |
| 2 | EC_SRT_MODE_ACTIVE | The PDO thread is bound and promoted, or the process is already in the REALTIME priority class |
| 3 | EC_SRT_MODE_KERNEL_OFFLOAD | This 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;
| Field | Type | Description |
|---|---|---|
| mode | ec_srt_mode_t | Current mode (see the table above) |
| supported | BOOL | Whether this machine / process has the user-mode soft real-time capability |
| process_realtime | BOOL | Whether the process priority class is currently REALTIME_PRIORITY_CLASS (read live) |
| applied_by_sdk | BOOL | Whether the process real-time class was set by this SDK (decides whether SoftRealtimeDisable restores it) |
| reserved_core_count | uint32_t | Number of reserved processor cores; 0 = no reserved cores configured |
| reserved_mask | uint64_t | Low 64 bits of the reserved core bitmap (LSB-first, bit0 = core 0) |
| pdo_core | int | Core 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 |
| reason | char[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):
| Value | Behaviour |
|---|---|
| unset / empty / unrecognised | auto: takes effect only when the real-time transport is unavailable + reserved cores are valid + the process holds SeIncreaseBasePriorityPrivilege |
| off | hard off: not applied even when explicitly requested |
| on | force the attempt |
| auto | same 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
| Dimension | Thread level (default) | Process level (strict) |
|---|---|---|
| Entry points | SoftRealtimeApply / SoftRealtimeApplyAuto / SoftRealtimeEnsure | SoftRealtimeEnable / SoftRealtimeEnsureProcess |
| Target | That master's PDO thread | Every thread in the whole process |
| Process priority class | Not modified | Set to REALTIME_PRIORITY_CLASS |
| Intrusiveness | Small: host UI / GC / other threads are unaffected | Large: NORMAL effective priorities are lifted into the real-time range |
| CPU affinity | Binds the PDO thread only | Also 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
| Category | Function | Return type | Read/write | Description |
|---|---|---|---|---|
| Status query | SoftRealtimeQuery(mi, out) | BOOL | read-only | Fills ec_srt_status_t; TRUE = filled, which does not mean enabled |
| ec_srt_policy_probe(mi) | int | read-only | Reads the policy only: 0 = auto, 1 = off, 2 = on; a failed query is treated as auto | |
| Thread-level apply | SoftRealtimeApply(mi) | BOOL | write-only | Binds the PDO thread to a reserved core + thread base priority 31; does not change the process priority class |
| SoftRealtimeApplyAuto(mi) | BOOL | write-only | Same action as SoftRealtimeApply, with the policy handled inside the runtime | |
| SoftRealtimeEnsure(mi) | BOOL | write-only | Alias for SoftRealtimeApply | |
| Process-level apply | SoftRealtimeEnable() | BOOL | write-only | Strict mode: sets the whole process to the REALTIME priority class |
| SoftRealtimeEnsureProcess() | BOOL | write-only | Alias for SoftRealtimeEnable | |
| Disable | SoftRealtimeDisable() | void | write-only | Clears all master registrations; restores only the process real-time class set by this SDK |
| Compatibility entry | SoftRealtimeOptIn() | BOOL | read-only | Old-signature compatibility; does not change any scheduling state |
| Polling | SoftRealtimePoll(mi) | BOOL | read-only | TRUE = 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 =
outis 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
modebefore any soft real-time decision; after any call returns FALSE, readreasonto locate the cause. Withmaster_index = 0it 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
SoftRealtimeQueryfor the reason). The test ismode == ACTIVEonly; when the state isEC_SRT_MODE_KERNEL_OFFLOADit 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.
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
SoftRealtimeQueryfor 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
modefromSoftRealtimeQuery. - 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 < 0andreserved_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
SoftRealtimeQueryseparately 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
reasonfor the reason text. - Real-time transport takes priority: when the low-level real-time transport is available, the mode only reports
KERNEL_OFFLOADand nothing is changed in user mode. - Policy off is a hard off:
SoftRealtimeApply/SoftRealtimeApplyAutodo not take effect when it is off, andSoftRealtimePollreturns TRUE immediately so the caller does not spin forever. SoftRealtimeOptInis not a status test: it always returns TRUE and only means no opt-in is needed; whether it is enabled is decided only bymodefromSoftRealtimeQuery.SoftRealtimePollreturns FALSE on a failed query: that is "state unknown", not "done"; callSoftRealtimeQueryseparately when you need to tell them apart.SoftRealtimeDisabledoes 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.