36 lines
1.1 KiB
C++
36 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;
|
|
slot.data = data;
|
|
++slot.sequence;
|
|
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 & 1) {
|
|
continue; // mid-write
|
|
}
|
|
FrameData snapshot = slot.data;
|
|
const uint64_t seq_after = slot.sequence;
|
|
if (seq_after == seq_before &&
|
|
contract.published_sequence == published) {
|
|
out = snapshot;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace nui::contract
|