"""Priority-inheritance teaching model, not a FreeRTOS emulation."""


def high_start(inherit):
    low_remaining, medium_remaining = 3, 6
    trace = []
    for now in range(30):
        high_waiting = now >= 1 and low_remaining > 0
        if now >= 1 and low_remaining == 0:
            return now, trace
        ready = []
        if low_remaining:
            ready.append((3 if inherit and high_waiting else 1, "L"))
        if now >= 1 and medium_remaining:
            ready.append((2, "M"))
        _, current = max(ready)
        trace.append(current)
        if current == "L":
            low_remaining -= 1
        else:
            medium_remaining -= 1
    raise AssertionError("did not release lock")


def main():
    without, a = high_start(False)
    with_inheritance, b = high_start(True)
    assert without == 9 and with_inheritance == 3
    assert a.count("L") == b.count("L") == 3
    assert a.count("M") == 6 and b.count("M") == 0
    print(f"without inheritance: H starts at {without}; trace={''.join(a)}")
    print(f"with inheritance: H starts at {with_inheritance}; trace={''.join(b)}")
    print("PASS")


if __name__ == "__main__":
    main()
