Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: base/metrics/persistent_memory_allocator.cc

Issue 2886453002: Use acquire/relase for delayed persistent allocations. (Closed)
Patch Set: rebased Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/persistent_memory_allocator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/persistent_memory_allocator.h" 5 #include "base/metrics/persistent_memory_allocator.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #if defined(OS_WIN) 10 #if defined(OS_WIN)
(...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 DCHECK_NE(0U, type_); 1126 DCHECK_NE(0U, type_);
1127 DCHECK_LT(0U, size_); 1127 DCHECK_LT(0U, size_);
1128 DCHECK(reference_); 1128 DCHECK(reference_);
1129 } 1129 }
1130 1130
1131 DelayedPersistentAllocation::~DelayedPersistentAllocation() {} 1131 DelayedPersistentAllocation::~DelayedPersistentAllocation() {}
1132 1132
1133 void* DelayedPersistentAllocation::Get() const { 1133 void* DelayedPersistentAllocation::Get() const {
1134 // Relaxed operations are acceptable here because it's not protecting the 1134 // Relaxed operations are acceptable here because it's not protecting the
1135 // contents of the allocation in any way. 1135 // contents of the allocation in any way.
1136 Reference ref = reference_->load(std::memory_order_relaxed); 1136 Reference ref = reference_->load(std::memory_order_acquire);
1137 if (!ref) { 1137 if (!ref) {
1138 ref = allocator_->Allocate(size_, type_); 1138 ref = allocator_->Allocate(size_, type_);
1139 if (!ref) 1139 if (!ref)
1140 return nullptr; 1140 return nullptr;
1141 1141
1142 // Store the new reference in its proper location using compare-and-swap. 1142 // Store the new reference in its proper location using compare-and-swap.
1143 // Use a "strong" exchange to ensure no false-negatives since the operation 1143 // Use a "strong" exchange to ensure no false-negatives since the operation
1144 // cannot be retried. 1144 // cannot be retried.
1145 Reference existing = 0; // Must be mutable; receives actual value. 1145 Reference existing = 0; // Must be mutable; receives actual value.
1146 if (reference_->compare_exchange_strong(existing, ref, 1146 if (reference_->compare_exchange_strong(existing, ref,
1147 std::memory_order_relaxed, 1147 std::memory_order_release,
1148 std::memory_order_relaxed)) { 1148 std::memory_order_relaxed)) {
1149 if (make_iterable_) 1149 if (make_iterable_)
1150 allocator_->MakeIterable(ref); 1150 allocator_->MakeIterable(ref);
1151 } else { 1151 } else {
1152 // Failure indicates that something else has raced ahead, performed the 1152 // Failure indicates that something else has raced ahead, performed the
1153 // allocation, and stored its reference. Purge the allocation that was 1153 // allocation, and stored its reference. Purge the allocation that was
1154 // just done and use the other one instead. 1154 // just done and use the other one instead.
1155 DCHECK_EQ(type_, allocator_->GetType(existing)); 1155 DCHECK_EQ(type_, allocator_->GetType(existing));
1156 DCHECK_LE(size_, allocator_->GetAllocSize(existing)); 1156 DCHECK_LE(size_, allocator_->GetAllocSize(existing));
1157 allocator_->ChangeType(ref, 0, type_, /*clear=*/false); 1157 allocator_->ChangeType(ref, 0, type_, /*clear=*/false);
1158 ref = existing; 1158 ref = existing;
1159 } 1159 }
1160 } 1160 }
1161 1161
1162 char* mem = allocator_->GetAsArray<char>(ref, type_, size_); 1162 char* mem = allocator_->GetAsArray<char>(ref, type_, size_);
1163 if (!mem) { 1163 if (!mem) {
1164 // This should never happen but be tolerant if it does as corruption from 1164 // This should never happen but be tolerant if it does as corruption from
1165 // the outside is something to guard against. 1165 // the outside is something to guard against.
1166 NOTREACHED(); 1166 NOTREACHED();
1167 return nullptr; 1167 return nullptr;
1168 } 1168 }
1169 return mem + offset_; 1169 return mem + offset_;
1170 } 1170 }
1171 1171
1172 } // namespace base 1172 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/persistent_memory_allocator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698