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