summaryrefslogtreecommitdiff
path: root/lib/decoder_ipc_control.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
committerChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
commit69344560ea394c06ca94acac46a2e4c3e8b7b229 (patch)
treeebc98370a697a9bbd6b973a14fa5113c475c6db7 /lib/decoder_ipc_control.cpp
parent3e9d10a67671d0bd5c7b8c3ca8088f1818322eff (diff)
parent059831534c7e884f0a607ed86b582fd00d821731 (diff)
Update upstream source from tag 'upstream/3.2.0_rc1+dfsg'
Update to upstream version '3.2.0~rc1+dfsg' with Debian dir dbff457fd2c3616d5c77b863c7bfb2186ed86047
Diffstat (limited to 'lib/decoder_ipc_control.cpp')
-rw-r--r--lib/decoder_ipc_control.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/lib/decoder_ipc_control.cpp b/lib/decoder_ipc_control.cpp
new file mode 100644
index 00000000..e72251d7
--- /dev/null
+++ b/lib/decoder_ipc_control.cpp
@@ -0,0 +1,191 @@
+#include "decoder_ipc_control.h"
+
+#include <atomic>
+#include <cstdint>
+#include <limits>
+
+#if defined (_MSC_VER)
+#include <intrin.h>
+#endif
+
+static_assert (sizeof (int) == sizeof (std::int32_t),
+ "decoder IPC requires 32-bit control words");
+
+namespace
+{
+ std::atomic<int const *> progressGeneration {nullptr};
+ std::atomic<int const *> progressState {nullptr};
+ std::atomic<int const *> progressVersion {nullptr};
+ std::atomic<int *> progressCounter {nullptr};
+
+#if defined (_MSC_VER)
+ static_assert (sizeof (long) == sizeof (int),
+ "decoder IPC requires 32-bit MSVC interlocked words");
+
+ int atomicLoad (int const * value)
+ {
+ return static_cast<int> (_InterlockedCompareExchange (
+ reinterpret_cast<volatile long *> (const_cast<int *> (value)), 0, 0));
+ }
+
+ void atomicStore (int * value, int desired)
+ {
+ _InterlockedExchange (reinterpret_cast<volatile long *> (value), desired);
+ }
+
+ bool compareExchange (int * value, int expected, int desired)
+ {
+ return expected == _InterlockedCompareExchange (
+ reinterpret_cast<volatile long *> (value), desired, expected);
+ }
+#else
+ static_assert (__atomic_always_lock_free (sizeof (int), nullptr),
+ "decoder IPC control words must be lock-free");
+
+ int atomicLoad (int const * value)
+ {
+ return __atomic_load_n (value, __ATOMIC_ACQUIRE);
+ }
+
+ void atomicStore (int * value, int desired)
+ {
+ __atomic_store_n (value, desired, __ATOMIC_RELEASE);
+ }
+
+ bool compareExchange (int * value, int expected, int desired)
+ {
+ return __atomic_compare_exchange_n (value, &expected, desired, false,
+ __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
+ }
+#endif
+
+ bool hasCurrentVersion (int const * version)
+ {
+ return DECODER_IPC_VERSION == atomicLoad (version);
+ }
+}
+
+extern "C" int decoder_ipc_atomic_load (int const * value)
+{
+ return atomicLoad (value);
+}
+
+extern "C" void decoder_ipc_control_initialize (int * generation, int * state,
+ int * version, int * progress)
+{
+ atomicStore (generation, 0);
+ atomicStore (progress, 0);
+ atomicStore (version, DECODER_IPC_VERSION);
+ atomicStore (state, DECODER_IPC_IDLE);
+}
+
+extern "C" void decoder_ipc_control_shutdown (int * state,
+ int * legacy_acknowledgment)
+{
+ atomicStore (state, DECODER_IPC_SHUTDOWN);
+ atomicStore (legacy_acknowledgment, 1);
+}
+
+extern "C" int decoder_ipc_control_publish (int * generation, int * state,
+ int const * version,
+ int * progress,
+ int request_generation)
+{
+ if (request_generation <= 0 || !hasCurrentVersion (version)
+ || DECODER_IPC_IDLE != atomicLoad (state))
+ {
+ return 0;
+ }
+
+ atomicStore (progress, 0);
+ atomicStore (generation, request_generation);
+ return compareExchange (state, DECODER_IPC_IDLE, DECODER_IPC_READY);
+}
+
+extern "C" int decoder_ipc_control_try_claim (int * generation, int * state,
+ int const * version,
+ int * claimed_generation)
+{
+ if (!hasCurrentVersion (version)) return DECODER_IPC_CLAIM_INCOMPATIBLE;
+
+ auto const currentState = atomicLoad (state);
+ if (DECODER_IPC_SHUTDOWN == currentState) return DECODER_IPC_CLAIM_SHUTDOWN;
+ if (DECODER_IPC_READY != currentState) return DECODER_IPC_CLAIM_NONE;
+
+ auto const currentGeneration = atomicLoad (generation);
+ if (currentGeneration <= 0) return DECODER_IPC_CLAIM_INVALID;
+
+ if (!compareExchange (state, DECODER_IPC_READY, DECODER_IPC_DECODING))
+ {
+ return DECODER_IPC_CLAIM_NONE;
+ }
+
+ *claimed_generation = currentGeneration;
+ return DECODER_IPC_CLAIMED;
+}
+
+extern "C" int decoder_ipc_control_finish (int const * generation, int * state,
+ int const * version,
+ int request_generation)
+{
+ return request_generation > 0
+ && hasCurrentVersion (version)
+ && request_generation == atomicLoad (generation)
+ && compareExchange (state, DECODER_IPC_DECODING, DECODER_IPC_COMPLETE);
+}
+
+extern "C" int decoder_ipc_control_consume (int const * generation, int * state,
+ int const * version,
+ int request_generation)
+{
+ if (request_generation <= 0 || !hasCurrentVersion (version)
+ || request_generation != atomicLoad (generation))
+ {
+ return 0;
+ }
+
+ return compareExchange (state, DECODER_IPC_COMPLETE, DECODER_IPC_IDLE);
+}
+
+extern "C" void decoder_ipc_progress_bind (int const * generation,
+ int const * state,
+ int const * version,
+ int * progress)
+{
+ progressGeneration.store (generation, std::memory_order_relaxed);
+ progressState.store (state, std::memory_order_relaxed);
+ progressVersion.store (version, std::memory_order_relaxed);
+ progressCounter.store (progress, std::memory_order_release);
+}
+
+extern "C" void decoder_ipc_progress_unbind ()
+{
+ progressCounter.store (nullptr, std::memory_order_release);
+ progressVersion.store (nullptr, std::memory_order_relaxed);
+ progressState.store (nullptr, std::memory_order_relaxed);
+ progressGeneration.store (nullptr, std::memory_order_relaxed);
+}
+
+extern "C" void decoder_ipc_progress_report (int request_generation)
+{
+ auto * progress = progressCounter.load (std::memory_order_acquire);
+ auto const * version = progressVersion.load (std::memory_order_relaxed);
+ auto const * state = progressState.load (std::memory_order_relaxed);
+ auto const * generation = progressGeneration.load (std::memory_order_relaxed);
+ if (!progress || !version || !state || !generation
+ || request_generation <= 0
+ || DECODER_IPC_VERSION != atomicLoad (version)
+ || DECODER_IPC_DECODING != atomicLoad (state)
+ || request_generation != atomicLoad (generation))
+ {
+ return;
+ }
+
+ for (;;)
+ {
+ auto const current = atomicLoad (progress);
+ auto const next = std::numeric_limits<int>::max () == current
+ ? 1 : current + 1;
+ if (compareExchange (progress, current, next)) return;
+ }
+}