From a967774f496c6b2b6a74cc0c1d9f2bb211c6a65d Mon Sep 17 00:00:00 2001 From: "jonathan.reichardt" Date: Wed, 10 Jun 2026 23:35:00 +0200 Subject: [PATCH] Add oals::rt platform shim Extract RT primitives (SCHED_FIFO, SCHED_RR, CPU affinity, clock_nanosleep) into oals::rt namespace in netutils/platform/rt.h+cpp Linux behavior unchanged. macOS no-op stubs (since there is no real RT support on mac in the same way as on Linux) --- .github/workflows/ci.yaml | 32 ++++++++++++++++++ .gitignore | 1 + netutils/CMakeLists.txt | 2 ++ netutils/rt.cpp | 71 +++++++++++++++++++++++++++++++++++++++ netutils/rt.h | 20 +++++++++++ 5 files changed, 126 insertions(+) create mode 100644 .github/workflows/ci.yaml create mode 100644 .gitignore create mode 100644 netutils/rt.cpp create mode 100644 netutils/rt.h diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..00b1db1 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,32 @@ +on: + push: + branches: ['**'] + pull_request: + branches: ['**'] + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + + - name: Install deps (Linux) + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends cmake ninja-build + + - name: Install deps (macOS) + if: runner.os == 'macOS' + run: brew install cmake ninja + + - name: Configure + run: cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=Debug + + - name: Build + run: cmake --build build \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e864704 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/build/ \ No newline at end of file diff --git a/netutils/CMakeLists.txt b/netutils/CMakeLists.txt index 2837448..b353cbb 100644 --- a/netutils/CMakeLists.txt +++ b/netutils/CMakeLists.txt @@ -3,6 +3,8 @@ set(OAN_UTILS_SOURCES udp.h LowLatSocket.cpp LowLatSocket.h + rt.h + rt.cpp ) if(EMBEDDED_BUILD) diff --git a/netutils/rt.cpp b/netutils/rt.cpp new file mode 100644 index 0000000..3ef8803 --- /dev/null +++ b/netutils/rt.cpp @@ -0,0 +1,71 @@ +// This file is part of the Open Audio Live System project, a live audio environment +// Copyright (c) 2026 - Jonathan D Reichardt +// +// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0 + + +#include "rt.h" + + +#ifdef __linux__ +#include +#include +#include +#else +#include +#endif + +namespace oals::rt { +#ifdef __linux__ + void set_thread_realtime(uint8_t prio) { + sched_param sparams{}; + sparams.sched_priority = prio; + + if (sched_setscheduler(0, SCHED_FIFO, &sparams) != 0) { + std::cerr << "[oals::rt] set_thread_realtime failed" << std::endl; + } + } + void set_running_cpu(int cpu_id) { + cpu_set_t cs{}; + CPU_ZERO(&cs); + CPU_SET(cpu_id, &cs); + + if (sched_setaffinity(0, sizeof(cpu_set_t), &cs) != 0) { + std::cerr << "[oals::rt] set_running_cpu failed" << std::endl; + } + } + + void set_process_scheduler_rr(int prio) { + sched_param params{}; + params.sched_priority = prio; + if (sched_setscheduler(0, SCHED_RR, ¶ms) != 0) { + std::cerr << "[oals::rt] set_process_scheduler_rr failed" << std::endl; + } + } + + void precise_sleep(long ns) { + timespec ts{}; + ts.tv_sec = ns / 1'000'000'000; + ts.tv_nsec = ns % 1'000'000'000; + clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr); + } + + +#else + void set_thread_realtime(uint8_t) { + } + + void set_running_cpu(int) { + } + + void set_process_scheduler_rr(int) { + } + + void precise_sleep(long ns) { + timespec ts{}; + ts.tv_sec = ns / 1'000'000'000; + ts.tv_nsec = ns % 1'000'000'000; + nanosleep(&ts, nullptr); + } +#endif +} diff --git a/netutils/rt.h b/netutils/rt.h new file mode 100644 index 0000000..2acbdd3 --- /dev/null +++ b/netutils/rt.h @@ -0,0 +1,20 @@ +// This file is part of the Open Audio Live System project, a live audio environment +// Copyright (c) 2026 - Jonathan D Reichardt +// +// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0 + + +#ifndef OPENAUDIONETWORK_RT_H +#define OPENAUDIONETWORK_RT_H +#include + + +namespace oals::rt { + void set_thread_realtime(uint8_t); + void set_running_cpu(int); + void set_process_scheduler_rr(int); + void precise_sleep(long); +} + + +#endif //OPENAUDIONETWORK_RT_H