File: partitioned_lock_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (319 lines) | stat: -rw-r--r-- 11,904 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/locks/partitioned_lock_manager.h"

#include <memory>
#include <utility>

#include "base/barrier_closure.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "chrome/browser/web_applications/locks/partitioned_lock.h"
#include "chrome/browser/web_applications/locks/partitioned_lock_id.h"

namespace web_app {
namespace {
void CallIfHolderIsAlive(base::WeakPtr<PartitionedLockHolder> lock_holder,
                         base::OnceClosure on_all_locks_acquired) {
  if (lock_holder) {
    std::move(on_all_locks_acquired).Run();
  }
}
}  // namespace

PartitionedLockHolder::PartitionedLockHolder() = default;
PartitionedLockHolder::~PartitionedLockHolder() = default;

PartitionedLockManager::PartitionedLockRequest::PartitionedLockRequest(
    PartitionedLockId lock_id,
    LockType type)
    : lock_id(std::move(lock_id)), type(type) {}

PartitionedLockManager::LockRequest::LockRequest() = default;
PartitionedLockManager::LockRequest::LockRequest(
    LockType type,
    base::WeakPtr<PartitionedLockHolder> locks_holder,
    base::OnceClosure acquire_next_lock_or_post_completion,
    const base::Location& location)
    : requested_type(type),
      locks_holder(std::move(locks_holder)),
      acquire_next_lock_or_post_completion(
          std::move(acquire_next_lock_or_post_completion)),
      location(location) {}
PartitionedLockManager::LockRequest::LockRequest(LockRequest&&) noexcept =
    default;
PartitionedLockManager::LockRequest::~LockRequest() = default;
PartitionedLockManager::Lock::Lock() = default;
PartitionedLockManager::Lock::Lock(Lock&&) noexcept = default;
PartitionedLockManager::Lock::~Lock() = default;
PartitionedLockManager::Lock& PartitionedLockManager::Lock::operator=(
    PartitionedLockManager::Lock&&) noexcept = default;

PartitionedLockManager::PartitionedLockManager()
    : task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {}

PartitionedLockManager::~PartitionedLockManager() = default;

int64_t PartitionedLockManager::LocksHeldForTesting() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  int64_t locks = 0;
  for (const auto& [lock_id, lock] : locks_) {
    locks += lock.acquired_count;
  }
  return locks;
}

int64_t PartitionedLockManager::RequestsWaitingForTesting() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  int64_t requests = 0;
  for (const auto& [lock_id, lock] : locks_) {
    requests += lock.queue.size();
  }
  return requests;
}

void PartitionedLockManager::AcquireLocks(
    base::flat_set<PartitionedLockRequest> lock_requests,
    PartitionedLockHolder& locks_holder,
    LocksAcquiredCallback callback,
    const base::Location& location) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Pre-allocate all locks in the request, so the `locks_` map is not mutated
  // while acquiring locks (which invalidates iterators and storage).
  for (const PartitionedLockRequest& request : lock_requests) {
    locks_.try_emplace(request.lock_id);
    // Ensure that none of the locks are 'before' any already-held locks by the
    // holder.
    for (const PartitionedLock& lock : locks_holder.locks) {
      CHECK(lock.lock_id() < request.lock_id)
          << lock.lock_id() << " vs " << request.lock_id;
    }
  }

  locks_holder.locks.reserve(locks_holder.locks.size() + lock_requests.size());
  auto stored_requests =
      std::make_unique<base::flat_set<PartitionedLockRequest>>(
          std::move(lock_requests));
  auto first_request = stored_requests->begin();

  AcquireNextLockOrPostCompletion(
      std::move(stored_requests), first_request, locks_holder.AsWeakPtr(),
      base::BindOnce(CallIfHolderIsAlive, locks_holder.AsWeakPtr(),
                     std::move(callback)),
      location);
}

PartitionedLockManager::TestLockResult PartitionedLockManager::TestLock(
    PartitionedLockRequest request) {
  Lock& lock = locks_[request.lock_id];
  return lock.CanBeAcquired(request.type) ? TestLockResult::kFree
                                          : TestLockResult::kLocked;
}

std::vector<base::Location>
PartitionedLockManager::GetHeldAndQueuedLockLocations(
    const base::flat_set<PartitionedLockRequest>& requests) const {
  std::vector<base::Location> result;
  for (const auto& request : requests) {
    auto lock_it = locks_.find(request.lock_id);
    if (lock_it == locks_.end()) {
      continue;
    }
    result.insert(result.end(), lock_it->second.request_locations.begin(),
                  lock_it->second.request_locations.end());
    for (const LockRequest& queued_request : lock_it->second.queue) {
      result.push_back(queued_request.location);
    }
  }
  return result;
}

std::vector<PartitionedLockId> PartitionedLockManager::GetUnacquirableLocks(
    std::vector<PartitionedLockRequest>& lock_requests) {
  std::vector<PartitionedLockId> lock_ids;
  for (PartitionedLockRequest& request : lock_requests) {
    auto it = locks_.find(request.lock_id);
    if (it != locks_.end()) {
      Lock& lock = it->second;
      if (!lock.CanBeAcquired(request.type)) {
        lock_ids.push_back(request.lock_id);
      }
    }
  }
  return lock_ids;
}

base::Value PartitionedLockManager::ToDebugValue(
    TransformLockIdToStringFn transform) const {
  base::Value::Dict result;
  for (const std::pair<PartitionedLockId, Lock>& id_lock_pair : locks_) {
    const Lock& lock = id_lock_pair.second;
    base::Value::Dict lock_state;
    base::Value::List held_locations;
    for (const base::Location& location : lock.request_locations) {
      held_locations.Append(location.ToString());
    }
    lock_state.Set("held_locations", std::move(held_locations));

    base::Value::List queued_locations;
    for (const LockRequest& request : lock.queue) {
      queued_locations.Append(request.location.ToString());
    }
    lock_state.Set("queue", std::move(queued_locations));

    std::string id_as_str = transform(id_lock_pair.first);
    DCHECK(!result.contains(id_as_str))
        << id_as_str << " already exists in " << result.DebugString()
        << ", cannot insert " << lock_state.DebugString();
    result.Set(id_as_str, std::move(lock_state));
  }
  return base::Value(std::move(result));
}

void PartitionedLockManager::AcquireNextLockOrPostCompletion(
    std::unique_ptr<base::flat_set<PartitionedLockRequest>> requests,
    base::flat_set<PartitionedLockRequest>::iterator current,
    base::WeakPtr<PartitionedLockHolder> locks_holder,
    base::OnceClosure on_all_acquired,
    const base::Location& location) {
  if (on_all_acquired.IsCancelled() || on_all_acquired.is_null()) {
    return;
  }
  if (!locks_holder || current == requests->end()) {
    VLOG(1) << "All locks acquired for " << location.ToString();
    // All locks have been acquired or we're aborting.
    task_runner_->PostTask(FROM_HERE, std::move(on_all_acquired));
    return;
  }
  // Note: This function cannot modify the `locks_` map.

  PartitionedLockRequest& request = *current;
  LocksMap::iterator lock_it = locks_.find(request.lock_id);
  CHECK(lock_it != locks_.end());
  Lock& lock = lock_it->second;

  auto acquire_next_lock_or_post_completion = base::BindOnce(
      &PartitionedLockManager::AcquireNextLockOrPostCompletion,
      weak_factory_.GetWeakPtr(), std::move(requests), std::next(current),
      locks_holder, std::move(on_all_acquired), location);

  if (lock.CanBeAcquired(request.type)) {
    VLOG(1) << "Acquiring " << request.lock_id << " for "
            << location.ToString();
    ++lock.acquired_count;
    lock.lock_mode = request.type;
    lock.request_locations.insert(location);
    auto released_callback =
        base::BindOnce(&PartitionedLockManager::LockReleased,
                       weak_factory_.GetWeakPtr(), location);
    locks_holder->locks.emplace_back(std::move(request.lock_id),
                                     std::move(released_callback));
    std::move(acquire_next_lock_or_post_completion).Run();
    return;
  }

  // The lock cannot be acquired now, so we put it on the queue which will
  // grant the given callback the lock when it is acquired in the future in
  // the |LockReleased| method.
  lock.queue.emplace_back(request.type, std::move(locks_holder),
                          std::move(acquire_next_lock_or_post_completion),
                          location);
}

void PartitionedLockManager::LockReleased(base::Location request_location,
                                          PartitionedLockId lock_id) {
  VLOG(1) << "Releasing " << lock_id << " requested by "
          << request_location.ToString();
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // This iterator is guaranteed to stay valid because
  // AcquireNextLockOrPostCompletion does not modify the `locks_` map.
  LocksMap::iterator it = locks_.find(lock_id);
  CHECK(it != locks_.end());
  Lock& lock = it->second;

  // First, decrement the lock `acquired_count`.
  DCHECK_GT(lock.acquired_count, 0);
  --(lock.acquired_count);
  lock.request_locations.erase(request_location);
  if (lock.acquired_count != 0) {
    return;
  }

  // Grant shared locks eagerly and exclusive locks only if `acquired_count` is
  // 0. Note that exclusive locks return below immediately after being granted.
  while (!lock.queue.empty() &&
         (lock.queue.front().requested_type == LockType::kShared ||
          lock.acquired_count == 0)) {
    LockRequest requester = std::move(lock.queue.front());
    lock.queue.pop_front();
    // Skip the request if the lock holder is already destroyed. This
    // avoids stack overflows for long chains of released locks. See
    // https://crbug.com/959743
    if (!requester.locks_holder) {
      continue;
    }
    // Grant the lock.
    VLOG(1) << "Acquiring " << lock_id << " for "
            << requester.location.ToString();
    ++lock.acquired_count;
    lock.lock_mode = requester.requested_type;
    lock.request_locations.insert(requester.location);
    auto released_callback =
        base::BindOnce(&PartitionedLockManager::LockReleased,
                       weak_factory_.GetWeakPtr(), requester.location);
    requester.locks_holder->locks.emplace_back(lock_id,
                                               std::move(released_callback));
    std::move(requester.acquire_next_lock_or_post_completion).Run();
    if (requester.requested_type == LockType::kExclusive) {
      return;
    }
  }
}

std::set<PartitionedLockHolder*> PartitionedLockManager::GetQueuedRequests(
    const PartitionedLockId& lock_id) const {
  std::set<PartitionedLockHolder*> blocked_requests;

  auto it = locks_.find(lock_id);
  if (it == locks_.end()) {
    return blocked_requests;
  }

  for (const LockRequest& request : it->second.queue) {
    if (request.locks_holder) {
      blocked_requests.insert(request.locks_holder.get());
    }
  }
  return blocked_requests;
}

bool operator<(const PartitionedLockManager::PartitionedLockRequest& x,
               const PartitionedLockManager::PartitionedLockRequest& y) {
  if (x.lock_id != y.lock_id) {
    return x.lock_id < y.lock_id;
  }
  return x.type < y.type;
}

bool operator==(const PartitionedLockManager::PartitionedLockRequest& x,
                const PartitionedLockManager::PartitionedLockRequest& y) {
  return x.lock_id == y.lock_id && x.type == y.type;
}

bool operator!=(const PartitionedLockManager::PartitionedLockRequest& x,
                const PartitionedLockManager::PartitionedLockRequest& y) {
  return !(x == y);
}

}  // namespace web_app