--------------------------- MODULE CaptureFrameHold --------------------------- (***************************************************************************) (* The frame-hold protocol of one GVFG capture channel. *) (* *) (* Promise checked: while the application holds a frame whose data is the *) (* backend capture buffer, (1) that buffer still belongs to the running *) (* driver stream -- no recovery restart and no stop/start has replaced it *) (* -- and (2) the one gvfg_release_channel_frame() the application must *) (* make will succeed. *) (* *) (* Harm if broken: the application reads a zero-copy buffer the driver is *) (* refilling (torn or foreign video), and/or its release returns *) (* GVFG_ESTATE and the channel stays "frame held", so every later read is *) (* rejected until stop. *) (* *) (* Threads: R = application video worker (read / release), *) (* U = application lifecycle thread (stop / start, serialized), *) (* E = session event thread (signal change -> recovery restart). *) (* Each action is one indivisible step in the code: a section under one *) (* mutex, or one atomic access. Code references name the function in *) (* C = GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp (gvfg_channel_session_t) *) (* S = GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp *) (***************************************************************************) EXTENDS Naturals CONSTANTS AppQuiescesReader, \* TRUE: app joins/pauses its reader before stop/start (srcroot GvfgSource::stop does) Guards, \* code checks present; the shipped code has all of them (see MC .cfg files) MaxLifecycleOps, \* bound on stop/start calls MaxSignalEvents, \* bound on format-change / plug-in events MaxReads \* bound on read attempts AllGuards == {"RestartWaitsForRelease", "CaptureRejectsDuringRecovery", "DeliverRechecksRunning", "DeliverChecksRunGeneration"} ASSUME Guards \subseteq AllGuards VARIABLES \* --- channel, C: gvfg_channel_session_t ------------------------------- running, \* C running (std::atomic) readInProgress, \* C readInProgress (frameMutex) frameHeld, \* C frameHeld (frameMutex) heldNeedsBackend, \* C heldFrameNeedsBackendRelease (frameMutex) heldEpoch, \* ghost: driver stream epoch of the buffer in heldSessionFrame runGen, \* C runGeneration (frameMutex): +1 on every successful start \* --- session, S: GigabyteCaptureSession ------------------------------- sRunning, \* S running_ sFrameHeld, \* S frame_held_ (state_mutex_) recoveryPending, \* S recovery_pending_ generation, \* S signal_generation_ epoch, \* ghost: +1 on every GvfgStartCapture (driver buffers handed out anew) monitoring, \* S monitoring_ (event thread alive) \* --- threads --------------------------------------------------------- rpc, rGen, rRunGen, rEpoch, rNeeds, reads, upc, uNeeds, ops, readerPaused, epc, signals, releaseFailed \* ghost: app's release of a held frame returned GVFG_ESTATE chanVars == <> sessVars == <> rVars == <> uVars == <> eVars == <> vars == <> RStates == {"idle", "begin_lock", "wait_check", "waiting", "capture", "osd", "deliver", "stop_release", "holding", "fail"} UStates == {"idle", "quiesce", "choose", "stop_frames", "stop_backend", "stop_stream", "stop_join", "start_session", "start_channel"} EStates == {"idle", "decide", "wait_hold", "restart", "finish"} TypeOK == /\ running \in BOOLEAN /\ readInProgress \in BOOLEAN /\ frameHeld \in BOOLEAN /\ heldNeedsBackend \in BOOLEAN /\ heldEpoch \in Nat /\ runGen \in Nat /\ sRunning \in BOOLEAN /\ sFrameHeld \in BOOLEAN /\ recoveryPending \in BOOLEAN /\ generation \in Nat /\ epoch \in Nat /\ monitoring \in BOOLEAN /\ rpc \in RStates /\ rGen \in Nat /\ rRunGen \in Nat /\ rEpoch \in Nat /\ rNeeds \in BOOLEAN /\ reads \in Nat /\ upc \in UStates /\ uNeeds \in BOOLEAN /\ ops \in Nat /\ readerPaused \in BOOLEAN /\ epc \in EStates /\ signals \in Nat /\ releaseFailed \in BOOLEAN \* Channel open and started once: C start() -> S start_stream() succeeded. Init == /\ running = TRUE /\ readInProgress = FALSE /\ frameHeld = FALSE /\ heldNeedsBackend = FALSE /\ heldEpoch = 0 /\ runGen = 1 /\ sRunning = TRUE /\ sFrameHeld = FALSE /\ recoveryPending = FALSE /\ generation = 0 /\ epoch = 1 /\ monitoring = TRUE /\ rpc = "idle" /\ rGen = 0 /\ rRunGen = 0 /\ rEpoch = 0 /\ rNeeds = FALSE /\ reads = 0 /\ upc = "idle" /\ uNeeds = FALSE /\ ops = 0 /\ readerPaused = FALSE /\ epc = "idle" /\ signals = 0 /\ releaseFailed = FALSE (***************************************************************************) (* R: gvfg_read_channel_frame / gvfg_release_channel_frame *) (***************************************************************************) \* C readFrame: `if (!running)` before taking frameMutex. RBegin == /\ rpc = "idle" /\ reads < MaxReads /\ ~(AppQuiescesReader /\ readerPaused) /\ running /\ rpc' = "begin_lock" /\ reads' = reads + 1 /\ UNCHANGED <> \* C readFrame, first frameMutex section: reject if held or another read; record the run. RBeginLock == /\ rpc = "begin_lock" /\ IF frameHeld \/ readInProgress THEN rpc' = "idle" /\ UNCHANGED <> ELSE readInProgress' = TRUE /\ rRunGen' = runGen /\ rpc' = "wait_check" /\ UNCHANGED <> \* S wait_frame entry: running_ check; state_mutex_: snapshot generation, reject if held. RWaitCheck == /\ rpc = "wait_check" /\ IF ~sRunning \/ sFrameHeld THEN rpc' = "fail" /\ UNCHANGED rGen ELSE rGen' = generation /\ rpc' = "waiting" /\ UNCHANGED <> \* S wait_frame: WaitForMultipleObjects(stop, recovery, frame events). \* stop_event set (stop_stream) -> ESTATE; recovery_event -> wait_for_recovery -> non-OK. RWakeFail == /\ rpc = "waiting" /\ (~sRunning \/ recoveryPending) /\ rpc' = "fail" /\ UNCHANGED <> \* A frame event fires while the driver stream runs. RWakeFrame == /\ rpc = "waiting" /\ sRunning /\ rpc' = "capture" /\ UNCHANGED <> \* S wait_frame capture section, one step under capture_mutex_: re-check, get the buffer, frame_held_ = true. RCapture == /\ rpc = "capture" /\ IF ~sRunning \/ ("CaptureRejectsDuringRecovery" \in Guards /\ (recoveryPending \/ rGen # generation)) THEN rpc' = "fail" /\ UNCHANGED <> ELSE sFrameHeld' = TRUE /\ rEpoch' = epoch /\ rpc' = "osd" /\ UNCHANGED <> \* C readFrame frame processor: no OSD, or in-place OSD, keeps the backend hold ... ROsdKeep == /\ rpc = "osd" /\ rNeeds' = TRUE /\ rpc' = "deliver" /\ UNCHANGED <> \* ... OSD into a separate SDK buffer releases the backend frame now (S release_frame). ROsdEarlyRelease == /\ rpc = "osd" /\ IF sFrameHeld THEN sFrameHeld' = FALSE /\ rNeeds' = FALSE /\ rpc' = "deliver" ELSE rpc' = "fail" /\ UNCHANGED <> /\ UNCHANGED <> \* C readFrame delivery, frameMutex: deliver unless capture stopped, or stopped and \* restarted, since the read began. Stale == \/ ("DeliverRechecksRunning" \in Guards /\ ~running) \/ ("DeliverChecksRunGeneration" \in Guards /\ rRunGen # runGen) RDeliver == /\ rpc = "deliver" /\ IF Stale THEN /\ readInProgress' = FALSE /\ rpc' = IF rNeeds THEN "stop_release" ELSE "idle" /\ UNCHANGED <> ELSE /\ frameHeld' = TRUE /\ heldNeedsBackend' = rNeeds /\ heldEpoch' = rEpoch /\ readInProgress' = FALSE /\ rpc' = "holding" /\ UNCHANGED <> \* C readFrame stoppedWhileWaiting: session->release_frame() (clears the hold if still held). RStopRelease == /\ rpc = "stop_release" /\ sFrameHeld' = FALSE /\ rpc' = "idle" /\ UNCHANGED <> \* C releaseFrame, one step under frameMutex (calls S release_frame inside). RRelease == /\ rpc = "holding" /\ rpc' = "idle" /\ CASE ~frameHeld -> \* invalidated by stop: ESTATE, documented UNCHANGED <> [] frameHeld /\ heldNeedsBackend /\ ~sFrameHeld -> \* S rejects; C returns before clearing /\ releaseFailed' = TRUE /\ UNCHANGED <> [] OTHER -> /\ frameHeld' = FALSE /\ heldNeedsBackend' = FALSE /\ sFrameHeld' = IF heldNeedsBackend THEN FALSE ELSE sFrameHeld /\ UNCHANGED releaseFailed /\ UNCHANGED <> RFail == /\ rpc = "fail" /\ readInProgress' = FALSE /\ rpc' = "idle" /\ UNCHANGED <> (***************************************************************************) (* U: gvfg_stop_channel / gvfg_start_channel, serialized by the app *) (***************************************************************************) UBegin == /\ upc = "idle" /\ ops < MaxLifecycleOps /\ IF AppQuiescesReader THEN readerPaused' = TRUE /\ upc' = "quiesce" ELSE upc' = "choose" /\ UNCHANGED readerPaused /\ UNCHANGED <> \* App protocol (docs/GVFG_FRAME_FLOW_AND_STATE.md section 5): reader released its frame and is joined. UQuiesced == /\ upc = "quiesce" /\ rpc = "idle" /\ upc' = "choose" /\ UNCHANGED <> \* C stop: running = false. UStop == /\ upc = "choose" /\ running /\ running' = FALSE /\ ops' = ops + 1 /\ upc' = "stop_frames" /\ UNCHANGED <> \* C releaseHeldFrameForStop, frameMutex: invalidate the app's frame. UStopFrames == /\ upc = "stop_frames" /\ uNeeds' = (frameHeld /\ heldNeedsBackend) /\ frameHeld' = FALSE /\ heldNeedsBackend' = FALSE /\ upc' = "stop_backend" /\ UNCHANGED <> \* C releaseHeldFrameForStop: session->release_frame(). UStopBackend == /\ upc = "stop_backend" /\ sFrameHeld' = IF uNeeds THEN FALSE ELSE sFrameHeld /\ upc' = "stop_stream" /\ UNCHANGED <> \* S stop_stream: running_ = false, events set, frame_held_ = false. UStopStream == /\ upc = "stop_stream" /\ sRunning' = FALSE /\ recoveryPending' = FALSE /\ sFrameHeld' = FALSE /\ monitoring' = FALSE /\ upc' = "stop_join" /\ UNCHANGED <> \* S stop_stream: join the event thread, GvfgStopCapture. UStopJoin == /\ upc = "stop_join" /\ epc = "idle" /\ upc' = "idle" /\ readerPaused' = FALSE /\ UNCHANGED <> \* C start: no-op while running. S start_stream: GvfgStartCapture, \* frame_held_ = false, running_ = true, event thread started. UStartSession == /\ upc = "choose" /\ ~running /\ ops' = ops + 1 /\ IF sRunning THEN UNCHANGED <> ELSE /\ sRunning' = TRUE /\ sFrameHeld' = FALSE /\ recoveryPending' = FALSE /\ epoch' = epoch + 1 /\ monitoring' = TRUE /\ upc' = "start_channel" /\ UNCHANGED <> \* C start, frameMutex: ++runGeneration; running = true. UStartChannel == /\ upc = "start_channel" /\ runGen' = runGen + 1 /\ running' = TRUE /\ upc' = "idle" /\ readerPaused' = FALSE /\ UNCHANGED <> (***************************************************************************) (* E: S event_thread_proc -> handle_signal_event (format change / plug-in) *) (***************************************************************************) \* S handle_signal_event first block, under capture_mutex_ + state_mutex_. ESignal == /\ epc = "idle" /\ monitoring /\ sRunning /\ signals < MaxSignalEvents /\ generation' = generation + 1 /\ recoveryPending' = TRUE /\ signals' = signals + 1 /\ epc' = "decide" /\ UNCHANGED <> \* S handle_signal_event: restart only if the input format really changed. EDecide == /\ epc = "decide" /\ epc' \in {"wait_hold", "finish"} /\ UNCHANGED <> \* S restart_stream_after_signal_change: wait for !frame_held_ || !running_. EWaitHold == /\ epc = "wait_hold" /\ ("RestartWaitsForRelease" \notin Guards \/ ~sFrameHeld \/ ~sRunning) /\ epc' = "restart" /\ UNCHANGED <> \* S restart_stream_after_signal_change under capture_mutex_: GvfgStopCapture + GvfgStartCapture. ERestart == /\ epc = "restart" /\ epoch' = IF sRunning THEN epoch + 1 ELSE epoch /\ epc' = "finish" /\ UNCHANGED <> \* S handle_signal_event end: recovery done. EFinish == /\ epc = "finish" /\ recoveryPending' = FALSE /\ epc' = "idle" /\ UNCHANGED <> (***************************************************************************) Terminated == /\ rpc = "idle" /\ epc = "idle" /\ upc = "idle" /\ ops = MaxLifecycleOps Next == \/ RBegin \/ RBeginLock \/ RWaitCheck \/ RWakeFail \/ RWakeFrame \/ RCapture \/ ROsdKeep \/ ROsdEarlyRelease \/ RDeliver \/ RStopRelease \/ RRelease \/ RFail \/ UBegin \/ UQuiesced \/ UStop \/ UStopFrames \/ UStopBackend \/ UStopStream \/ UStopJoin \/ UStartSession \/ UStartChannel \/ ESignal \/ EDecide \/ EWaitHold \/ ERestart \/ EFinish \/ (Terminated /\ UNCHANGED vars) Spec == Init /\ [][Next]_vars (***************************************************************************) (* Properties *) (***************************************************************************) \* (1) The buffer the app holds belongs to the driver stream that is running now. HeldFrameIsCurrent == (frameHeld /\ heldNeedsBackend) => heldEpoch = epoch \* (2) The app's single release of that frame will succeed (S still records the hold). ReleaseWillSucceed == (frameHeld /\ heldNeedsBackend) => sFrameHeld \* (2') Consequence observed by the app. NoFailedRelease == ~releaseFailed =============================================================================