Skip to content

Commit 316b325

Browse files
authored
refactor: use lock_guard instead of unique_lock when available (#398)
Follow the answer in stackoverflow https://stackoverflow.com/a/60172828/11568166 1. `lock_guard` if you need to lock exactly 1 mutex for an entire scope. 2. `scoped_lock` if you need to lock a number of mutexes that is not exactly 1. 3. `unique_lock` if you need to unlock within the scope of the block (which includes use with a condition_variable). So let clang-tidy only report `modernize-use-scoped-lock` warning when there are multiple `lock_guard`.
1 parent 3fc0445 commit 316b325

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

.clang-tidy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ CheckOptions:
3838
value: '_'
3939
- key: readability-identifier-naming.ProtectedMemberSuffix
4040
value: '_'
41+
- key: modernize-use-scoped-lock.WarnOnSingleLocks
42+
value: 'false'
4143

4244
HeaderFilterRegex: 'src/iceberg|example'

src/iceberg/catalog/memory/in_memory_catalog.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#include "iceberg/catalog/memory/in_memory_catalog.h"
2121

2222
#include <algorithm>
23-
#include <iterator> // IWYU pragma: keep
23+
#include <iterator>
24+
#include <mutex>
2425

25-
#include "iceberg/exception.h"
2626
#include "iceberg/table.h"
2727
#include "iceberg/table_metadata.h"
2828
#include "iceberg/util/macros.h"
@@ -337,42 +337,42 @@ std::string_view InMemoryCatalog::name() const { return catalog_name_; }
337337

338338
Status InMemoryCatalog::CreateNamespace(
339339
const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) {
340-
std::unique_lock lock(mutex_);
340+
std::lock_guard guard(mutex_);
341341
return root_namespace_->CreateNamespace(ns, properties);
342342
}
343343

344344
Result<std::unordered_map<std::string, std::string>>
345345
InMemoryCatalog::GetNamespaceProperties(const Namespace& ns) const {
346-
std::unique_lock lock(mutex_);
346+
std::lock_guard guard(mutex_);
347347
return root_namespace_->GetProperties(ns);
348348
}
349349

350350
Result<std::vector<Namespace>> InMemoryCatalog::ListNamespaces(
351351
const Namespace& ns) const {
352-
std::unique_lock lock(mutex_);
352+
std::lock_guard guard(mutex_);
353353
return root_namespace_->ListNamespaces(ns);
354354
}
355355

356356
Status InMemoryCatalog::DropNamespace(const Namespace& ns) {
357-
std::unique_lock lock(mutex_);
357+
std::lock_guard guard(mutex_);
358358
return root_namespace_->DropNamespace(ns);
359359
}
360360

361361
Result<bool> InMemoryCatalog::NamespaceExists(const Namespace& ns) const {
362-
std::unique_lock lock(mutex_);
362+
std::lock_guard guard(mutex_);
363363
return root_namespace_->NamespaceExists(ns);
364364
}
365365

366366
Status InMemoryCatalog::UpdateNamespaceProperties(
367367
const Namespace& ns, const std::unordered_map<std::string, std::string>& updates,
368368
const std::unordered_set<std::string>& removals) {
369-
std::unique_lock lock(mutex_);
369+
std::lock_guard guard(mutex_);
370370
return root_namespace_->UpdateNamespaceProperties(ns, updates, removals);
371371
}
372372

373373
Result<std::vector<TableIdentifier>> InMemoryCatalog::ListTables(
374374
const Namespace& ns) const {
375-
std::unique_lock lock(mutex_);
375+
std::lock_guard guard(mutex_);
376376
const auto& table_names = root_namespace_->ListTables(ns);
377377
ICEBERG_RETURN_UNEXPECTED(table_names);
378378
std::vector<TableIdentifier> table_idents;
@@ -405,12 +405,12 @@ Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable(
405405
}
406406

407407
Result<bool> InMemoryCatalog::TableExists(const TableIdentifier& identifier) const {
408-
std::unique_lock lock(mutex_);
408+
std::lock_guard guard(mutex_);
409409
return root_namespace_->TableExists(identifier);
410410
}
411411

412412
Status InMemoryCatalog::DropTable(const TableIdentifier& identifier, bool purge) {
413-
std::unique_lock lock(mutex_);
413+
std::lock_guard guard(mutex_);
414414
// TODO(Guotao): Delete all metadata files if purge is true.
415415
return root_namespace_->UnregisterTable(identifier);
416416
}
@@ -428,7 +428,7 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
428428

429429
Result<std::string> metadata_location;
430430
{
431-
std::unique_lock lock(mutex_);
431+
std::lock_guard guard(mutex_);
432432
ICEBERG_ASSIGN_OR_RAISE(metadata_location,
433433
root_namespace_->GetTableMetadataLocation(identifier));
434434
}
@@ -443,7 +443,7 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
443443

444444
Result<std::shared_ptr<Table>> InMemoryCatalog::RegisterTable(
445445
const TableIdentifier& identifier, const std::string& metadata_file_location) {
446-
std::unique_lock lock(mutex_);
446+
std::lock_guard guard(mutex_);
447447
if (!root_namespace_->NamespaceExists(identifier.ns)) {
448448
return NoSuchNamespace("table namespace does not exist.");
449449
}

src/iceberg/catalog/rest/http_client.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Result<HttpResponse> HttpClient::Get(
139139
const ErrorHandler& error_handler) {
140140
cpr::Response response;
141141
{
142-
std::scoped_lock<std::mutex> lock(session_mutex_);
142+
std::lock_guard guard(session_mutex_);
143143
PrepareSession(path, headers, params);
144144
response = session_->Get();
145145
}
@@ -156,7 +156,7 @@ Result<HttpResponse> HttpClient::Post(
156156
const ErrorHandler& error_handler) {
157157
cpr::Response response;
158158
{
159-
std::scoped_lock<std::mutex> lock(session_mutex_);
159+
std::lock_guard guard(session_mutex_);
160160
PrepareSession(path, headers);
161161
session_->SetBody(cpr::Body{body});
162162
response = session_->Post();
@@ -176,7 +176,7 @@ Result<HttpResponse> HttpClient::PostForm(
176176
cpr::Response response;
177177

178178
{
179-
std::scoped_lock<std::mutex> lock(session_mutex_);
179+
std::lock_guard guard(session_mutex_);
180180

181181
// Override default Content-Type (application/json) with form-urlencoded
182182
auto form_headers = headers;
@@ -204,7 +204,7 @@ Result<HttpResponse> HttpClient::Head(
204204
const ErrorHandler& error_handler) {
205205
cpr::Response response;
206206
{
207-
std::scoped_lock<std::mutex> lock(session_mutex_);
207+
std::lock_guard guard(session_mutex_);
208208
PrepareSession(path, headers);
209209
response = session_->Head();
210210
}
@@ -220,7 +220,7 @@ Result<HttpResponse> HttpClient::Delete(
220220
const ErrorHandler& error_handler) {
221221
cpr::Response response;
222222
{
223-
std::scoped_lock<std::mutex> lock(session_mutex_);
223+
std::lock_guard guard(session_mutex_);
224224
PrepareSession(path, headers);
225225
response = session_->Delete();
226226
}

src/iceberg/table_metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class ICEBERG_EXPORT TableMetadataBuilder : public ErrorCollector {
419419
Result<std::unique_ptr<TableMetadata>> Build();
420420

421421
/// \brief Destructor
422-
~TableMetadataBuilder();
422+
~TableMetadataBuilder() override;
423423

424424
// Delete copy operations (use BuildFrom to create a new builder)
425425
TableMetadataBuilder(const TableMetadataBuilder&) = delete;

0 commit comments

Comments
 (0)