Commit 0ce7158
committed
cheevos: drop stale rc_client load completions via generation
counter
Even with the atomic queued_command from the previous patch, a
race remains: a background load callback for game A can complete
*after* the user has closed content and started loading game B.
The bg thread atomically writes CMD_CHEEVOS_FINALIZE_LOAD to
queued_command; on the next frame, rcheevos_test dispatches
rcheevos_finalize_game_load_on_ui_thread - applying game A's
finalization (achievement runtime initialization, memory map
hookup, hardcore enforcement) to game B's rcheevos_locals state.
Add a load_generation atomic counter to rcheevos_locals_t. Bump
it (atomic_inc) at the top of rcheevos_unload and rcheevos_load,
before any other state mutation. Capture the current value as
the userdata argument to rc_client_begin_identify_and_load_game
(passed through as void* via intptr_t cast - the rc_client
library treats userdata opaquely).
The bg-thread callback checks the captured generation against
the live counter at two points:
- At the top of rcheevos_client_load_game_callback, before
any work that mutates rcheevos_locals (rcheevos_init_memory,
rc_client_set_read_memory_function, rcheevos_finalize_game_load).
Stale callbacks return immediately without side effects.
- Just before publishing CMD_CHEEVOS_FINALIZE_LOAD to
queued_command. Between the entry check and here the bg
thread runs a long sequence (network/file I/O, can take
seconds); the user may unload mid-callback. The re-check
is cheap and prevents a stale FINALIZE_LOAD from queuing.
Both checks are HAVE_THREADS-gated and skipped when the callback
runs on the main thread (which only happens when the rc_client
synchronously dispatches; in that case the userdata sentinel is
NULL but task_is_on_main_thread() returns true so the check is
skipped). The kickoff site is also gated: under HAVE_THREADS
the userdata is the captured generation, otherwise NULL.
Closes the second half of the cheevos queued_command hazard. The
first half (atomicity) was addressed in fd50fe1.1 parent fd50fe1 commit 0ce7158
2 files changed
Lines changed: 92 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
96 | 99 | | |
97 | 100 | | |
98 | 101 | | |
| |||
806 | 809 | | |
807 | 810 | | |
808 | 811 | | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
809 | 823 | | |
810 | 824 | | |
811 | 825 | | |
| |||
1663 | 1677 | | |
1664 | 1678 | | |
1665 | 1679 | | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
1666 | 1703 | | |
1667 | 1704 | | |
1668 | 1705 | | |
| |||
1748 | 1785 | | |
1749 | 1786 | | |
1750 | 1787 | | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
1751 | 1801 | | |
1752 | 1802 | | |
| 1803 | + | |
1753 | 1804 | | |
1754 | 1805 | | |
1755 | 1806 | | |
| |||
1768 | 1819 | | |
1769 | 1820 | | |
1770 | 1821 | | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
1771 | 1830 | | |
1772 | 1831 | | |
1773 | 1832 | | |
| |||
1882 | 1941 | | |
1883 | 1942 | | |
1884 | 1943 | | |
1885 | | - | |
1886 | | - | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
1887 | 1964 | | |
1888 | 1965 | | |
1889 | 1966 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
96 | 109 | | |
97 | 110 | | |
98 | 111 | | |
| |||
0 commit comments