Skip to content

Commit 0bf7868

Browse files
Change the NamedMutex (#1358)
Change the IsCanceled call to the atomic_bool variable. Upon cancellation the variable is set to true, means we don't lock the mutex inside CancellationContext. Which eliminates the lock ordering problem. Move the token reset code out of lock_mutex_. Switch CI from ubuntu-18.04 to ubuntu-20.04 due to deprecation. Fix a warning. Relates-To: OAM-1807 Signed-off-by: Mykhailo Kuchma <ext-mykhailo.kuchma@here.com>
1 parent a0dc81e commit 0bf7868

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

.github/workflows/psv_pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616
jobs:
1717
psv-linux-gcc-build-test-codecov:
1818
name: PSV / Linux gcc 7.5 / Tests / Code coverage
19-
runs-on: ubuntu-18.04
19+
runs-on: ubuntu-20.04
2020
steps:
2121
- name: Check out repository
2222
uses: actions/checkout@v2
@@ -40,7 +40,7 @@ jobs:
4040

4141
psv-linux-gcc-build-no-cache:
4242
name: PSV / Linux gcc 7.5 / OLP_SDK_ENABLE_DEFAULT_CACHE=OFF
43-
runs-on: ubuntu-18.04
43+
runs-on: ubuntu-20.04
4444
env:
4545
BUILD_TYPE: RelWithDebInfo
4646
steps:

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ variables:
1515
jobs:
1616
- job: Commit_checker
1717
pool:
18-
vmImage: 'ubuntu-18.04'
18+
vmImage: 'ubuntu-20.04'
1919
condition: eq(variables['Build.Reason'], 'PullRequest')
2020
steps:
2121
- bash: scripts/misc/commit_checker.sh

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Response<rapidjson::Document> Parse(client::HttpResponse& http_response) {
8686
"Failed to parse response"});
8787
}
8888

89-
return std::move(document);
89+
return Response<rapidjson::Document>(std::move(document));
9090
}
9191
} // namespace
9292

olp-cpp-sdk-dataservice-read/src/repositories/NamedMutex.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,29 @@ NamedMutex::NamedMutex(NamedMutexStorage& storage, const std::string& name,
140140
name_{name},
141141
mutex_{storage_.AcquireLock(name_)},
142142
lock_condition_{storage_.GetLockCondition(name_)},
143-
lock_mutex_{storage_.GetLockMutex(name_)} {}
143+
lock_mutex_{storage_.GetLockMutex(name_)},
144+
is_canceled_{false} {}
144145

145146
NamedMutex::~NamedMutex() { storage_.ReleaseLock(name_); }
146147

147148
void NamedMutex::lock() {
148-
const bool valid = context_.ExecuteOrCancelled(
149-
[&]() { return client::CancellationToken([&]() { Notify(); }); });
149+
const bool executed = context_.ExecuteOrCancelled([&]() {
150+
return client::CancellationToken([&]() {
151+
is_canceled_.store(true);
152+
Notify();
153+
});
154+
});
150155

151-
if (valid) {
156+
is_canceled_.store(!executed);
157+
158+
if (executed) {
152159
std::unique_lock<std::mutex> unique_lock{lock_mutex_};
153160
lock_condition_.wait(unique_lock,
154-
[&] { return context_.IsCancelled() || try_lock(); });
155-
// Reset the cancel token
156-
context_.ExecuteOrCancelled([&]() { return client::CancellationToken(); });
161+
[&] { return is_canceled_ || try_lock(); });
157162
}
163+
164+
// Reset the cancel token
165+
context_.ExecuteOrCancelled([]() { return client::CancellationToken(); });
158166
}
159167

160168
bool NamedMutex::try_lock() { return is_locked_ = mutex_.try_lock(); }
@@ -176,6 +184,8 @@ boost::optional<client::ApiError> NamedMutex::GetError() {
176184
}
177185

178186
void NamedMutex::Notify() {
187+
// A mutex is required since there is a gap between predicate call and wait
188+
// call when calling wait in NamedMutex::lock.
179189
std::unique_lock<std::mutex> unique_lock{lock_mutex_};
180190
lock_condition_.notify_all();
181191
}

olp-cpp-sdk-dataservice-read/src/repositories/NamedMutex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#pragma once
2121

22+
#include <atomic>
2223
#include <condition_variable>
2324
#include <memory>
2425
#include <mutex>
@@ -121,6 +122,9 @@ class NamedMutex final {
121122
std::mutex& mutex_;
122123
std::condition_variable& lock_condition_;
123124
std::mutex& lock_mutex_;
125+
// We can't check CancellationContext::IsCanceled, since it might result in a
126+
// deadlock.
127+
std::atomic<bool> is_canceled_;
124128
};
125129

126130
} // namespace repository

0 commit comments

Comments
 (0)