okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
5.7 KiB128 linesraw
1// sfnx's stdin (tools/sfnx.cpp): the control lines that act at once and the queue of the rest, with the request numbers
2// that tie each flag to the request it is for. Plain C++ (tools/sfnx_input_test.cpp tests it without a GPU)
3#pragma once
4
5#include <atomic>
6#include <chrono>
7#include <condition_variable>
8#include <cstdio>
9#include <deque>
10#include <iostream>
11#include <mutex>
12#include <string>
13#include <utility>
14
15namespace sfnx {
16
17constexpr int MAX_SLOTS = 8;
18
19// stdin: STOP, BSTOP <slot> and BYIELD <slot> act the moment they arrive; every other line is queued with its number
20// (request lines are counted in arrival order). A flag holds the number of the latest request it is for, so nothing ever
21// clears it and one sent before a request's line never acts on that request, while one sent after does however long
22// the request waits in the queue: STOP is for the latest request line in and no other (the front end sends a request's
23// line and its STOP under one control lock, so the latest line is the request it means; an older one still queued is
24// another client's), BSTOP <slot> and BYIELD <slot> for that slot's latest BGEN / BGENI, and BYIELD also for the latest
25// GEN / GENI (a solo read gives way, its part read waiting in the slot named)
26struct Input {
27    std::mutex mu;
28    std::condition_variable cv;
29    std::deque<std::pair<std::string, long>> lines;
30    bool eof = false;
31    long seq = 0;       // request lines queued (under mu)
32    long running = 0;   // the request the engine is on (begin / end; under mu), 0: none
33    // the reader's own: the numbers of the latest GEN / GENI and of each slot's latest BGEN / BGENI
34    long solo_seq = 0, slot_seq[MAX_SLOTS] = {};
35    std::atomic<long> stop_upto{0}, solo_yield_upto{0}, bstop_upto[MAX_SLOTS], yield_upto[MAX_SLOTS];
36    std::atomic<int> solo_yield_slot{-1};
37    // STOP for the running request, as Session::generate takes it: set by begin() and by a STOP while it runs
38    std::atomic<bool> stop_now{false};
39    Input() {
40        for (int i = 0; i < MAX_SLOTS; ++i) {
41            bstop_upto[i] = 0;
42            yield_upto[i] = 0;
43        }
44    }
45    void run(std::istream& is = std::cin) {
46        std::string line;
47        while (std::getline(is, line)) {
48            if (!line.empty() && line.back() == '\r') line.pop_back();
49            if (line == "STOP") {
50                std::lock_guard<std::mutex> lk(mu);
51                stop_upto = seq;
52                if (running > 0 && running == seq) stop_now = true;
53                continue;
54            }
55            int slot = -1;
56            if (std::sscanf(line.c_str(), "BSTOP %d", &slot) == 1) {
57                if (slot >= 0 && slot < MAX_SLOTS) bstop_upto[slot] = slot_seq[slot];
58                else std::fprintf(stderr, "sfnx: '%s': no slot %d (0..%d): nothing stopped\n", line.c_str(), slot, MAX_SLOTS - 1);
59                continue;
60            }
61            if (std::sscanf(line.c_str(), "BYIELD %d", &slot) == 1) {
62                if (slot >= 0 && slot < MAX_SLOTS) {
63                    yield_upto[slot] = slot_seq[slot];
64                    solo_yield_slot = slot;   // before the number: whoever sees the number sees this slot or a later one
65                    solo_yield_upto = solo_seq;
66                } else {
67                    std::fprintf(stderr, "sfnx: '%s': no slot %d (0..%d): nothing yields\n", line.c_str(), slot, MAX_SLOTS - 1);
68                }
69                continue;
70            }
71            const std::string cmd = line.substr(0, line.find(' '));
72            const bool request = cmd == "GEN" || cmd == "GENI" || cmd == "BGEN" || cmd == "BGENI";
73            std::lock_guard<std::mutex> lk(mu);
74            long n = 0;
75            if (request) {
76                n = ++seq;
77                if (cmd == "GEN" || cmd == "GENI") solo_seq = n;
78                else if (std::sscanf(line.c_str() + cmd.size(), " %d", &slot) == 1 && slot >= 0 && slot < MAX_SLOTS)
79                    slot_seq[slot] = n;
80            }
81            lines.emplace_back(line, n);
82            cv.notify_one();
83        }
84        std::lock_guard<std::mutex> lk(mu);
85        eof = true;
86        cv.notify_one();
87    }
88    // the next queued line and its request number (0: not a request): waiting for one (at most `most`) when wait, else
89    // only if one is there
90    bool next(std::string& line, long& n, bool wait, std::chrono::milliseconds most = std::chrono::milliseconds::max()) {
91        std::unique_lock<std::mutex> lk(mu);
92        if (wait && most == std::chrono::milliseconds::max()) cv.wait(lk, [&] { return !lines.empty() || eof; });
93        else if (wait) cv.wait_for(lk, most, [&] { return !lines.empty() || eof; });
94        if (lines.empty()) return false;
95        line = std::move(lines.front().first);
96        n = lines.front().second;
97        lines.pop_front();
98        return true;
99    }
100    bool closed() {
101        std::lock_guard<std::mutex> lk(mu);
102        return eof && lines.empty();
103    }
104    // the engine starts on request n / is done with it (stop_now follows STOP for it in between)
105    void begin(long n) {
106        std::lock_guard<std::mutex> lk(mu);
107        running = n;
108        stop_now = stop_upto == n;
109    }
110    void end() {
111        std::lock_guard<std::mutex> lk(mu);
112        running = 0;
113        stop_now = false;
114    }
115    bool bstopped(int slot, long n) const { return bstop_upto[slot] >= n; }
116    bool yielded(int slot, long n) const { return yield_upto[slot] >= n; }
117    // the slot a BYIELD named for solo request n, -1: none came for it
118    int solo_yield(long n) const { return solo_yield_upto >= n ? solo_yield_slot.load() : -1; }
119};
120
121// begin() for a request, end() however its handling returns
122struct Running {
123    Input& in;
124    Running(Input& i, long n) : in(i) { in.begin(n); }
125    ~Running() { in.end(); }
126};
127
128}  // namespace sfnx