Skip to content

Commit e2b6464

Browse files
committed
[Runtime] Properly set the count in newly allocated storage in ConcurrentReadableArray. Remove a redundant load of Count. Fix memory ordering on ReaderCount.
rdar://problem/37173156
1 parent a296eda commit e2b6464

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

include/swift/Runtime/Concurrent.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ template <class ElemTy> struct ConcurrentReadableArray {
463463
auto *newStorage = allocate(newCapacity);
464464
if (storage) {
465465
std::copy(storage->data(), storage->data() + count, newStorage->data());
466+
newStorage->Count.store(count, std::memory_order_relaxed);
466467
FreeList.push_back(storage);
467468
}
468469

@@ -471,11 +472,10 @@ template <class ElemTy> struct ConcurrentReadableArray {
471472
Elements.store(storage, std::memory_order_release);
472473
}
473474

474-
auto Count = storage->Count.load(std::memory_order_relaxed);
475-
new(&storage->data()[Count]) ElemTy(elem);
476-
storage->Count.store(Count + 1, std::memory_order_release);
475+
new(&storage->data()[count]) ElemTy(elem);
476+
storage->Count.store(count + 1, std::memory_order_release);
477477

478-
if (ReaderCount.load(std::memory_order_relaxed) == 0)
478+
if (ReaderCount.load(std::memory_order_acquire) == 0)
479479
for (Storage *storage : FreeList)
480480
deallocate(storage);
481481
}
@@ -485,14 +485,14 @@ template <class ElemTy> struct ConcurrentReadableArray {
485485
/// count. This represents a snapshot of the contents at the time
486486
/// `read` was called. The pointer becomes invalid after `f` returns.
487487
template <class F> auto read(F f) -> decltype(f(nullptr, 0)) {
488-
ReaderCount.fetch_add(1, std::memory_order_relaxed);
488+
ReaderCount.fetch_add(1, std::memory_order_acquire);
489489
auto *storage = Elements.load(std::memory_order_consume);
490490
auto count = storage->Count.load(std::memory_order_acquire);
491491
auto *ptr = storage->data();
492492

493493
decltype(f(nullptr, 0)) result = f(ptr, count);
494494

495-
ReaderCount.fetch_sub(1, std::memory_order_relaxed);
495+
ReaderCount.fetch_sub(1, std::memory_order_release);
496496

497497
return result;
498498
}

0 commit comments

Comments
 (0)