From 1f278d9ae4980415a64efa667e8ffb0816df2ced Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 28 Jul 2023 08:04:42 -0400 Subject: [PATCH] thrift: add temporary Mutex implementation The Thrift library has its own abstraction for mutexes, which normally just a wrapper around `std::mutex` using the PIMPL idiom (pointer-to-impl). Since Zephyr does not yet support `std::mutex`, a workaround was added in Zephyr that was essentially no-op, and actually the PIMPL idiom made it quite easy to do that. However, pretending there is no synchronization requirement is not a solution for it. We can't yet just use a `struct k_mutex` yet, because we don't yet support userspace, but for now we can fake a mutex interface with a spinlock. We hope to eventually drop this workaround entirely and just support `std::mutex`. Signed-off-by: Christopher Friedt --- .../thrift/src/thrift/concurrency/Mutex.cpp | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/modules/thrift/src/thrift/concurrency/Mutex.cpp b/modules/thrift/src/thrift/concurrency/Mutex.cpp index 4229143491..20848f6ed8 100644 --- a/modules/thrift/src/thrift/concurrency/Mutex.cpp +++ b/modules/thrift/src/thrift/concurrency/Mutex.cpp @@ -4,6 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + #include namespace apache @@ -13,31 +15,52 @@ namespace thrift namespace concurrency { +class Mutex::impl +{ +public: + k_spinlock_key_t key; + struct k_spinlock lock; +}; + Mutex::Mutex() { + impl_ = std::make_shared(); } void Mutex::lock() const { + while (!trylock()) { + k_msleep(1); + } } bool Mutex::trylock() const { - return false; + return k_spin_trylock(&impl_->lock, &impl_->key) == 0; } bool Mutex::timedlock(int64_t milliseconds) const { + k_timepoint_t end = sys_timepoint_calc(K_MSEC(milliseconds)); + + do { + if (trylock()) { + return true; + } + k_msleep(5); + } while(!sys_timepoint_expired(end)); + return false; } void Mutex::unlock() const { + k_spin_unlock(&impl_->lock, impl_->key); } void *Mutex::getUnderlyingImpl() const { - return nullptr; + return &impl_->lock; } } // namespace concurrency } // namespace thrift