Commit c9a9e2f
fix: [ee/libcglue] correct __retarget_lock_acquire_recursive race
The previous implementation incremented lock->count and stored the
caller's thread_id BEFORE WaitSema, with this race-prone shape:
starting = lock->count == 0;
if (starting) lock->thread_id = thread_id; // <-- racy
if (lock->thread_id == thread_id) {
lock->count++;
if (starting) WaitSema(lock->sem_id);
} else {
WaitSema(lock->sem_id);
lock->thread_id = thread_id;
lock->count++;
}
If a higher-priority thread preempted between `count++` and `WaitSema`,
the preempting thread saw `lock->count > 0`, took the else branch
(another WaitSema), then nobody signalled it after the original thread
released.
Rewritten as a proper recursive mutex:
- Fast path: if this thread already owns the gate
(count > 0 && thread_id == self), just increment count and return.
Safe to read those fields without the gate because only the owning
thread mutates them.
- Slow path: WaitSema first, *then* set thread_id + count = 1
atomically (relative to other threads, since we hold the gate).
Same logic applied to __retarget_lock_try_acquire_recursive (PollSema
instead of WaitSema, returns 1 on contention).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 586784c commit c9a9e2f
1 file changed
Lines changed: 27 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
144 | 143 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
161 | 150 | | |
| 151 | + | |
162 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
177 | | - | |
178 | 176 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 177 | + | |
| 178 | + | |
195 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
196 | 185 | | |
197 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
198 | 189 | | |
199 | 190 | | |
200 | 191 | | |
| |||
0 commit comments