okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
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 ; 30bool eof = false; 31long seq = 0 ;// request lines queued (under mu) 32long 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 34long 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}; 39Input () { 40for (int i = 0 ;i < MAX_SLOTS ;++ i ) { 41bstop_upto [i ]= 0 ; 42yield_upto [i ]= 0 ; 43 } 44 } 45void run (std::istream & is = std::cin ) { 46 std::string line ; 47while (std::getline (is ,line )) { 48if (!line .empty ()&& line .back ()== '\r' )line .pop_back (); 49if (line == "STOP" ) { 50 std::lock_guard < std::mutex > lk (mu ); 51stop_upto = seq ; 52if (running > 0 && running == seq )stop_now = true; 53continue ; 54 } 55int slot = -1 ; 56if (std::sscanf (line .c_str (),"BSTOP %d" ,& slot )== 1 ) { 57if (slot >=0 && slot < MAX_SLOTS )bstop_upto [slot ]= slot_seq [slot ]; 58else std::fprintf (stderr ,"sfnx: '%s': no slot %d (0..%d): nothing stopped\n" ,line .c_str (),slot ,MAX_SLOTS - 1 ); 59continue ; 60 } 61if (std::sscanf (line .c_str (),"BYIELD %d" ,& slot )== 1 ) { 62if (slot >=0 && slot < MAX_SLOTS ) { 63yield_upto [slot ]= slot_seq [slot ]; 64solo_yield_slot = slot ;// before the number: whoever sees the number sees this slot or a later one 65solo_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 } 69continue ; 70 } 71const std::string cmd = line .substr (0 ,line .find (' ' )); 72const bool request = cmd == "GEN" || cmd == "GENI" || cmd == "BGEN" || cmd == "BGENI" ; 73 std::lock_guard < std::mutex > lk (mu ); 74long n = 0 ; 75if (request ) { 76n = ++ seq ; 77if (cmd == "GEN" || cmd == "GENI" )solo_seq = n ; 78else if (std::sscanf (line .c_str ()+ cmd .size ()," %d" ,& slot )== 1 && slot >=0 && slot < MAX_SLOTS ) 79slot_seq [slot ]= n ; 80 } 81lines .emplace_back (line ,n ); 82cv .notify_one (); 83 } 84 std::lock_guard < std::mutex > lk (mu ); 85eof = true; 86cv .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 90bool next (std::string & line ,long & n ,bool wait , std::chrono::milliseconds most = std::chrono::milliseconds::max ()) { 91 std::unique_lock < std::mutex > lk (mu ); 92if (wait && most == std::chrono::milliseconds::max ())cv .wait (lk , [& ] {return !lines .empty ()|| eof ; }); 93else if (wait )cv .wait_for (lk ,most , [& ] {return !lines .empty ()|| eof ; }); 94if (lines .empty ())return false; 95line = std::move (lines .front ().first ); 96n = lines .front ().second ; 97lines .pop_front (); 98return true; 99 } 100bool closed () { 101 std::lock_guard < std::mutex > lk (mu ); 102return eof && lines .empty (); 103 } 104// the engine starts on request n / is done with it (stop_now follows STOP for it in between) 105void begin (long n ) { 106 std::lock_guard < std::mutex > lk (mu ); 107running = n ; 108stop_now = stop_upto == n ; 109 } 110void end () { 111 std::lock_guard < std::mutex > lk (mu ); 112running = 0 ; 113stop_now = false; 114 } 115bool bstopped (int slot ,long n )const {return bstop_upto [slot ] >=n ; } 116bool 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 118int 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 { 123Input & in ; 124Running (Input & i ,long n ) :in (i ) {in .begin (n ); } 125 ~Running () {in .end (); } 126}; 127 128}// namespace sfnx