35 lines
1.1 KiB
C++
35 lines
1.1 KiB
C++
#include "contract/contract.h"
|
|
|
|
namespace nui::contract {
|
|
|
|
int contract_version() { return static_cast<int>(kContractVersion); }
|
|
|
|
void publish_frame(FrameContract& contract, const FrameData& data) {
|
|
const uint64_t next_sequence = contract.published_sequence + 1;
|
|
FrameSlot& slot = contract.slots[next_sequence % kFrameSlotCount];
|
|
// Seqlock parity: odd while writing, even once `data` is fully written.
|
|
slot.sequence = next_sequence * 2 - 1;
|
|
slot.data = data;
|
|
slot.sequence = next_sequence * 2;
|
|
contract.published_sequence = next_sequence;
|
|
}
|
|
|
|
void read_latest_frame(const FrameContract& contract, FrameData& out) {
|
|
for (;;) {
|
|
const uint64_t published = contract.published_sequence;
|
|
const FrameSlot& slot = contract.slots[published % kFrameSlotCount];
|
|
const uint64_t seq_before = slot.sequence;
|
|
if (seq_before != published * 2) {
|
|
continue; // mid-write (odd) or stale slot; retry against the current publish
|
|
}
|
|
FrameData snapshot = slot.data;
|
|
const uint64_t seq_after = slot.sequence;
|
|
if (seq_after == seq_before) {
|
|
out = snapshot;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace nui::contract
|