@@ -235,8 +235,9 @@ static T AtomicLoad(ThreadState *thr, uptr pc, const volatile T *a, morder mo) {
235
235
T v = NoTsanAtomicLoad (a, mo);
236
236
SyncVar *s = ctx->metamap .GetSyncIfExists ((uptr)a);
237
237
if (s) {
238
- ReadLock l (&s->mtx );
239
- AcquireImpl (thr, pc, &s->clock );
238
+ SlotLocker locker (thr);
239
+ ReadLock lock (&s->mtx );
240
+ thr->clock .Acquire (s->clock );
240
241
// Re-read under sync mutex because we need a consistent snapshot
241
242
// of the value and the clock we acquire.
242
243
v = NoTsanAtomicLoad (a, mo);
@@ -270,33 +271,36 @@ static void AtomicStore(ThreadState *thr, uptr pc, volatile T *a, T v,
270
271
NoTsanAtomicStore (a, v, mo);
271
272
return ;
272
273
}
273
- __sync_synchronize ( );
274
- SyncVar *s = ctx-> metamap . GetSyncOrCreate (thr, pc, (uptr)a, false );
275
- Lock l (&s-> mtx );
276
- thr-> fast_state . IncrementEpoch ( );
277
- // Can't increment epoch w/o writing to the trace as well.
278
- TraceAddEvent (thr, thr-> fast_state , EventTypeMop, 0 );
279
- ReleaseStoreImpl (thr, pc, &s-> clock );
280
- NoTsanAtomicStore (a, v, mo );
274
+ SlotLocker locker (thr );
275
+ {
276
+ auto s = ctx-> metamap . GetSyncOrCreate (thr, pc, (uptr)a, false );
277
+ Lock lock (&s-> mtx );
278
+ thr-> clock . ReleaseStore (&s-> clock );
279
+ NoTsanAtomicStore (a, v, mo );
280
+ }
281
+ IncrementEpoch (thr );
281
282
}
282
283
283
284
template <typename T, T (*F)(volatile T *v, T op)>
284
285
static T AtomicRMW (ThreadState *thr, uptr pc, volatile T *a, T v, morder mo) {
285
286
MemoryAccess (thr, pc, (uptr)a, AccessSize<T>(), kAccessWrite | kAccessAtomic );
286
287
if (LIKELY (mo == mo_relaxed))
287
288
return F (a, v);
288
- SyncVar *s = ctx->metamap .GetSyncOrCreate (thr, pc, (uptr)a, false );
289
- Lock l (&s->mtx );
290
- thr->fast_state .IncrementEpoch ();
291
- // Can't increment epoch w/o writing to the trace as well.
292
- TraceAddEvent (thr, thr->fast_state , EventTypeMop, 0 );
293
- if (IsAcqRelOrder (mo))
294
- AcquireReleaseImpl (thr, pc, &s->clock );
295
- else if (IsReleaseOrder (mo))
296
- ReleaseImpl (thr, pc, &s->clock );
297
- else if (IsAcquireOrder (mo))
298
- AcquireImpl (thr, pc, &s->clock );
299
- return F (a, v);
289
+ SlotLocker locker (thr);
290
+ {
291
+ auto s = ctx->metamap .GetSyncOrCreate (thr, pc, (uptr)a, false );
292
+ RWLock lock (&s->mtx , IsReleaseOrder (mo));
293
+ if (IsAcqRelOrder (mo))
294
+ thr->clock .ReleaseAcquire (&s->clock );
295
+ else if (IsReleaseOrder (mo))
296
+ thr->clock .Release (&s->clock );
297
+ else if (IsAcquireOrder (mo))
298
+ thr->clock .Acquire (s->clock );
299
+ v = F (a, v);
300
+ }
301
+ if (IsReleaseOrder (mo))
302
+ IncrementEpoch (thr);
303
+ return v;
300
304
}
301
305
302
306
template <typename T>
@@ -416,27 +420,28 @@ static bool AtomicCAS(ThreadState *thr, uptr pc, volatile T *a, T *c, T v,
416
420
*c = pr;
417
421
return false ;
418
422
}
419
-
423
+ SlotLocker locker (thr);
420
424
bool release = IsReleaseOrder (mo);
421
- SyncVar *s = ctx->metamap .GetSyncOrCreate (thr, pc, (uptr)a, false );
422
- RWLock l (&s->mtx , release);
423
- T cc = *c;
424
- T pr = func_cas (a, cc, v);
425
- bool success = pr == cc;
426
- if (!success) {
427
- *c = pr;
428
- mo = fmo;
425
+ bool success;
426
+ {
427
+ auto s = ctx->metamap .GetSyncOrCreate (thr, pc, (uptr)a, false );
428
+ RWLock lock (&s->mtx , release);
429
+ T cc = *c;
430
+ T pr = func_cas (a, cc, v);
431
+ success = pr == cc;
432
+ if (!success) {
433
+ *c = pr;
434
+ mo = fmo;
435
+ }
436
+ if (success && IsAcqRelOrder (mo))
437
+ thr->clock .ReleaseAcquire (&s->clock );
438
+ else if (success && IsReleaseOrder (mo))
439
+ thr->clock .Release (&s->clock );
440
+ else if (IsAcquireOrder (mo))
441
+ thr->clock .Acquire (s->clock );
429
442
}
430
- thr->fast_state .IncrementEpoch ();
431
- // Can't increment epoch w/o writing to the trace as well.
432
- TraceAddEvent (thr, thr->fast_state , EventTypeMop, 0 );
433
-
434
- if (success && IsAcqRelOrder (mo))
435
- AcquireReleaseImpl (thr, pc, &s->clock );
436
- else if (success && IsReleaseOrder (mo))
437
- ReleaseImpl (thr, pc, &s->clock );
438
- else if (IsAcquireOrder (mo))
439
- AcquireImpl (thr, pc, &s->clock );
443
+ if (success && release)
444
+ IncrementEpoch (thr);
440
445
return success;
441
446
}
442
447
0 commit comments