Skip to content

Commit 7ac47c8

Browse files
committed
Simplify RwMutex usage.
1 parent 9c37075 commit 7ac47c8

5 files changed

Lines changed: 28 additions & 29 deletions

File tree

‎benchmark/bench_misc.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class RwMutexReadBench final : public td::Benchmark {
384384
for (int i = 0; i < ThreadN; i++) {
385385
threads.emplace_back([&] {
386386
for (int i = 0; i < n / ThreadN; i++) {
387-
mutex_.lock_read().ensure();
387+
auto lock = mutex_.lock_read();
388388
}
389389
});
390390
}
@@ -405,7 +405,7 @@ class RwMutexWriteBench final : public td::Benchmark {
405405
for (int i = 0; i < ThreadN; i++) {
406406
threads.emplace_back([&] {
407407
for (int i = 0; i < n / ThreadN; i++) {
408-
mutex_.lock_write().ensure();
408+
auto lock = mutex_.lock_write();
409409
}
410410
});
411411
}

‎td/telegram/Client.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,14 @@ class ClientManager::Impl final {
481481
auto client_id = MultiImpl::create_id();
482482
LOG(INFO) << "Created managed client " << client_id;
483483
{
484-
auto lock = impls_mutex_.lock_write().move_as_ok();
484+
auto lock = impls_mutex_.lock_write();
485485
impls_[client_id]; // create empty MultiImplInfo
486486
}
487487
return client_id;
488488
}
489489

490490
void send(ClientId client_id, RequestId request_id, td_api::object_ptr<td_api::Function> &&request) {
491-
auto lock = impls_mutex_.lock_read().move_as_ok();
491+
auto lock = impls_mutex_.lock_read();
492492
if (!MultiImpl::is_valid_client_id(client_id)) {
493493
receiver_.add_response(client_id, request_id,
494494
td_api::make_object<td_api::error>(400, "Invalid TDLib instance specified"));
@@ -499,15 +499,15 @@ class ClientManager::Impl final {
499499
if (it != impls_.end() && it->second.impl == nullptr) {
500500
lock.reset();
501501

502-
auto write_lock = impls_mutex_.lock_write().move_as_ok();
502+
auto write_lock = impls_mutex_.lock_write();
503503
it = impls_.find(client_id);
504504
if (it != impls_.end() && it->second.impl == nullptr) {
505505
it->second.impl = pool_.get();
506506
it->second.impl->create(client_id, receiver_.create_callback(client_id));
507507
}
508508
write_lock.reset();
509509

510-
lock = impls_mutex_.lock_read().move_as_ok();
510+
lock = impls_mutex_.lock_read();
511511
it = impls_.find(client_id);
512512
}
513513
if (it == impls_.end() || it->second.is_closed) {
@@ -524,14 +524,14 @@ class ClientManager::Impl final {
524524
static_cast<const td_api::updateAuthorizationState *>(response.object.get())->authorization_state_->get_id() ==
525525
td_api::authorizationStateClosed::ID) {
526526
LOG(INFO) << "Release closed client";
527-
auto lock = impls_mutex_.lock_write().move_as_ok();
527+
auto lock = impls_mutex_.lock_write();
528528
close_impl(response.client_id);
529529

530530
response.client_id = 0;
531531
response.object = nullptr;
532532
}
533533
if (response.object == nullptr && response.client_id != 0 && response.request_id == 0) {
534-
auto lock = impls_mutex_.lock_write().move_as_ok();
534+
auto lock = impls_mutex_.lock_write();
535535
auto it = impls_.find(response.client_id);
536536
CHECK(it != impls_.end());
537537
CHECK(it->second.is_closed);

‎tddb/td/db/BinlogKeyValue.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
129129
}
130130

131131
SeqNo set(string key, string value) final {
132-
auto lock = rw_mutex_.lock_write().move_as_ok();
132+
auto lock = rw_mutex_.lock_write();
133133
uint64 old_event_id = 0;
134134
CHECK(!key.empty());
135135
auto it_ok = map_.emplace(key, std::make_pair(value, 0));
@@ -162,7 +162,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
162162
}
163163

164164
SeqNo erase(const string &key) final {
165-
auto lock = rw_mutex_.lock_write().move_as_ok();
165+
auto lock = rw_mutex_.lock_write();
166166
auto it = map_.find(key);
167167
if (it == map_.end()) {
168168
return 0;
@@ -178,7 +178,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
178178
}
179179

180180
SeqNo erase_batch(vector<string> keys) final {
181-
auto lock = rw_mutex_.lock_write().move_as_ok();
181+
auto lock = rw_mutex_.lock_write();
182182
vector<uint64> log_event_ids;
183183
for (auto &key : keys) {
184184
auto it = map_.find(key);
@@ -199,12 +199,12 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
199199
}
200200

201201
bool isset(const string &key) final {
202-
auto lock = rw_mutex_.lock_read().move_as_ok();
202+
auto lock = rw_mutex_.lock_read();
203203
return map_.count(key) > 0;
204204
}
205205

206206
string get(const string &key) final {
207-
auto lock = rw_mutex_.lock_read().move_as_ok();
207+
auto lock = rw_mutex_.lock_read();
208208
auto it = map_.find(key);
209209
if (it == map_.end()) {
210210
return string();
@@ -222,14 +222,14 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
222222
}
223223

224224
void for_each(std::function<void(Slice, Slice)> func) final {
225-
auto lock = rw_mutex_.lock_write().move_as_ok();
225+
auto lock = rw_mutex_.lock_write();
226226
for (const auto &kv : map_) {
227227
func(kv.first, kv.second.first);
228228
}
229229
}
230230

231231
std::unordered_map<string, string, Hash<string>> prefix_get(Slice prefix) final {
232-
auto lock = rw_mutex_.lock_write().move_as_ok();
232+
auto lock = rw_mutex_.lock_write();
233233
std::unordered_map<string, string, Hash<string>> res;
234234
for (const auto &kv : map_) {
235235
if (begins_with(kv.first, prefix)) {
@@ -240,7 +240,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
240240
}
241241

242242
FlatHashMap<string, string> get_all() final {
243-
auto lock = rw_mutex_.lock_write().move_as_ok();
243+
auto lock = rw_mutex_.lock_write();
244244
FlatHashMap<string, string> res;
245245
res.reserve(map_.size());
246246
for (const auto &kv : map_) {
@@ -250,7 +250,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
250250
}
251251

252252
void erase_by_prefix(Slice prefix) final {
253-
auto lock = rw_mutex_.lock_write().move_as_ok();
253+
auto lock = rw_mutex_.lock_write();
254254
vector<uint64> event_ids;
255255
table_remove_if(map_, [&](const auto &it) {
256256
if (begins_with(it.first, prefix)) {

‎tddb/td/db/TsSeqKeyValue.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,37 @@ class TsSeqKeyValue {
3131
~TsSeqKeyValue() = default;
3232

3333
SeqNo set(Slice key, Slice value) {
34-
auto lock = rw_mutex_.lock_write().move_as_ok();
34+
auto lock = rw_mutex_.lock_write();
3535
return kv_.set(key, value);
3636
}
3737

3838
std::pair<SeqNo, RwMutex::WriteLock> set_and_lock(Slice key, Slice value) {
39-
auto lock = rw_mutex_.lock_write().move_as_ok();
39+
auto lock = rw_mutex_.lock_write();
4040
return std::make_pair(kv_.set(key, value), std::move(lock));
4141
}
4242

4343
SeqNo erase(const string &key) {
44-
auto lock = rw_mutex_.lock_write().move_as_ok();
44+
auto lock = rw_mutex_.lock_write();
4545
return kv_.erase(key);
4646
}
4747

4848
SeqNo erase_batch(vector<string> keys) {
49-
auto lock = rw_mutex_.lock_write().move_as_ok();
49+
auto lock = rw_mutex_.lock_write();
5050
return kv_.erase_batch(std::move(keys));
5151
}
5252

5353
std::pair<SeqNo, RwMutex::WriteLock> erase_and_lock(const string &key) {
54-
auto lock = rw_mutex_.lock_write().move_as_ok();
54+
auto lock = rw_mutex_.lock_write();
5555
return std::make_pair(kv_.erase(key), std::move(lock));
5656
}
5757

5858
string get(const string &key) const {
59-
auto lock = rw_mutex_.lock_read().move_as_ok();
59+
auto lock = rw_mutex_.lock_read();
6060
return kv_.get(key);
6161
}
6262

6363
bool isset(const string &key) const {
64-
auto lock = rw_mutex_.lock_read().move_as_ok();
64+
auto lock = rw_mutex_.lock_read();
6565
return kv_.isset(key);
6666
}
6767

@@ -70,7 +70,7 @@ class TsSeqKeyValue {
7070
}
7171

7272
FlatHashMap<string, string> get_all() const {
73-
auto lock = rw_mutex_.lock_write().move_as_ok();
73+
auto lock = rw_mutex_.lock_write();
7474
return kv_.get_all();
7575
}
7676

@@ -80,7 +80,7 @@ class TsSeqKeyValue {
8080
}
8181

8282
auto lock() {
83-
return rw_mutex_.lock_write().move_as_ok();
83+
return rw_mutex_.lock_write();
8484
}
8585

8686
private:

‎tdutils/td/utils/port/RwMutex.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "td/utils/port/config.h"
1010

1111
#include "td/utils/common.h"
12-
#include "td/utils/Status.h"
1312

1413
#if TD_PORT_POSIX
1514
#include <pthread.h>
@@ -60,12 +59,12 @@ class RwMutex {
6059
using ReadLock = std::unique_ptr<RwMutex, ReadUnlock>;
6160
using WriteLock = std::unique_ptr<RwMutex, WriteUnlock>;
6261

63-
Result<ReadLock> lock_read() TD_WARN_UNUSED_RESULT {
62+
ReadLock lock_read() TD_WARN_UNUSED_RESULT {
6463
lock_read_unsafe();
6564
return ReadLock(this);
6665
}
6766

68-
Result<WriteLock> lock_write() TD_WARN_UNUSED_RESULT {
67+
WriteLock lock_write() TD_WARN_UNUSED_RESULT {
6968
lock_write_unsafe();
7069
return WriteLock(this);
7170
}

0 commit comments

Comments
 (0)