| 1 | package control |
| 2 | |
| 3 | import "reasonix/internal/session" |
| 4 | |
| 5 | // turnFinishingBoundary exposes exact execution and TurnDone fan-out |
| 6 | // transitions without making observers poll scheduler-dependent state. |
| 7 | type turnFinishingBoundary struct { |
| 8 | done chan struct{} |
| 9 | idleDone chan struct{} |
| 10 | } |
| 11 | |
| 12 | func (b *turnFinishingBoundary) beginIdle() { |
| 13 | if b.idleDone == nil { |
| 14 | b.idleDone = make(chan struct{}) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | func (b *turnFinishingBoundary) endIdle() { |
| 19 | if b.idleDone == nil { |
| 20 | return |
| 21 | } |
| 22 | close(b.idleDone) |
| 23 | b.idleDone = nil |
| 24 | } |
| 25 | |
| 26 | func (b *turnFinishingBoundary) begin(finishing bool) { |
| 27 | if finishing { |
| 28 | b.done = make(chan struct{}) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func (b *turnFinishingBoundary) end() { |
| 33 | if b.done == nil { |
| 34 | return |
| 35 | } |
| 36 | close(b.done) |
| 37 | b.done = nil |
| 38 | } |
| 39 | |
| 40 | // Running reports whether a turn is currently in flight. |
| 41 | func (c *Controller) Running() bool { |
| 42 | c.mu.Lock() |
| 43 | defer c.mu.Unlock() |
| 44 | if c.closed { |
| 45 | return false |
| 46 | } |
| 47 | if c.turns.phase == session.RuntimeRecoveryRequired && c.turns.done != nil { |
| 48 | return true |
| 49 | } |
| 50 | return c.bodyActiveLocked() || c.finalizingLocked() |
| 51 | } |
| 52 | |
| 53 | // TurnIdleDone returns a boundary that closes when the currently admitted turn |
| 54 | // chain releases the running-or-finalizing admission gate. A turn parked during |
| 55 | // TurnDone fan-out remains in the same chain, so the boundary stays open until |
| 56 | // that turn also completes. Idle controllers return ok=false. |
| 57 | func (c *Controller) TurnIdleDone() (done <-chan struct{}, ok bool) { |
| 58 | c.mu.Lock() |
| 59 | defer c.mu.Unlock() |
| 60 | if c.turns.finishingBound.idleDone == nil { |
| 61 | return nil, false |
| 62 | } |
| 63 | return c.turns.finishingBound.idleDone, true |
| 64 | } |
| 65 | |
| 66 | // TurnFinishingDone returns the current TurnDone delivery boundary. |
| 67 | func (c *Controller) TurnFinishingDone() (done <-chan struct{}, ok bool) { |
| 68 | c.mu.Lock() |
| 69 | defer c.mu.Unlock() |
| 70 | if !c.finalizingLocked() || c.turns.finishingBound.done == nil { |
| 71 | return nil, false |
| 72 | } |
| 73 | return c.turns.finishingBound.done, true |
| 74 | } |
| 75 |