Commit fd50fe1
committed
cheevos: make queued_command atomic to fix bg-thread /
main-thread race
rcheevos_locals.queued_command was a plain enum event_command,
written from background threads (the rcheevos client's HTTP
completion callback at the bottom of
rcheevos_client_load_game_callback, and the rewind init/deinit
toggles inside rcheevos_apply_hardcore_state) and read on the
main thread by rcheevos_test once per frame.
Concurrent plain access to a non-atomic field is undefined: there
is no memory barrier publishing the writer's prior writes, no
guarantee that the reader observes the value at all on weak-memory
targets, and on architectures where enum is wider than the
naturally-atomic store size, a torn read.
Replace the field type with retro_atomic_int_t. Writers go through
retro_atomic_store_release_int; the reader in rcheevos_test takes
a single acquire-load snapshot at the top of the dispatch block,
preventing the three previous reads (the !=NONE check, the
==FINALIZE check, and the dispatch argument) from observing
different values if a writer fires between them.
Mirrors the atomic discipline already established for
gfx_thumbnail_t.status (gfx/gfx_thumbnail.c:50-65). The static
initializer for atomic_int with literal 0 is well-defined for all
retro_atomic.h backends (C11 stdatomic, C++11 std::atomic, and the
volatile-int fallback) - and CMD_EVENT_NONE happens to be 0.
This is the first of two patches addressing the queued_command
hazard. The second adds a load-generation counter to filter stale
bg-thread completions that race with rcheevos_unload, preventing
a finalize for game A from being applied to the memory map of
game B.1 parent 3741954 commit fd50fe1
2 files changed
Lines changed: 36 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
| |||
812 | 814 | | |
813 | 815 | | |
814 | 816 | | |
815 | | - | |
| 817 | + | |
| 818 | + | |
816 | 819 | | |
817 | 820 | | |
818 | 821 | | |
| |||
836 | 839 | | |
837 | 840 | | |
838 | 841 | | |
839 | | - | |
| 842 | + | |
| 843 | + | |
840 | 844 | | |
841 | 845 | | |
842 | 846 | | |
| |||
919 | 923 | | |
920 | 924 | | |
921 | 925 | | |
922 | | - | |
| 926 | + | |
| 927 | + | |
923 | 928 | | |
924 | 929 | | |
925 | 930 | | |
| |||
943 | 948 | | |
944 | 949 | | |
945 | 950 | | |
946 | | - | |
| 951 | + | |
| 952 | + | |
947 | 953 | | |
948 | 954 | | |
949 | 955 | | |
| |||
1114 | 1120 | | |
1115 | 1121 | | |
1116 | 1122 | | |
1117 | | - | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
1118 | 1132 | | |
1119 | | - | |
| 1133 | + | |
1120 | 1134 | | |
1121 | 1135 | | |
1122 | | - | |
| 1136 | + | |
1123 | 1137 | | |
1124 | | - | |
| 1138 | + | |
| 1139 | + | |
1125 | 1140 | | |
1126 | 1141 | | |
1127 | 1142 | | |
| |||
1733 | 1748 | | |
1734 | 1749 | | |
1735 | 1750 | | |
1736 | | - | |
| 1751 | + | |
| 1752 | + | |
1737 | 1753 | | |
1738 | 1754 | | |
1739 | 1755 | | |
| |||
1752 | 1768 | | |
1753 | 1769 | | |
1754 | 1770 | | |
1755 | | - | |
| 1771 | + | |
| 1772 | + | |
1756 | 1773 | | |
1757 | 1774 | | |
1758 | 1775 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
88 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
89 | 96 | | |
90 | 97 | | |
91 | 98 | | |
| |||
0 commit comments