aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/public/pw_rpc/internal/lock.h
diff options
context:
space:
mode:
Diffstat (limited to 'pw_rpc/public/pw_rpc/internal/lock.h')
-rw-r--r--pw_rpc/public/pw_rpc/internal/lock.h25
1 files changed, 13 insertions, 12 deletions
diff --git a/pw_rpc/public/pw_rpc/internal/lock.h b/pw_rpc/public/pw_rpc/internal/lock.h
index 50c53d5c3..3961eeee6 100644
--- a/pw_rpc/public/pw_rpc/internal/lock.h
+++ b/pw_rpc/public/pw_rpc/internal/lock.h
@@ -15,11 +15,10 @@
#include "pw_rpc/internal/config.h"
#include "pw_sync/lock_annotations.h"
+#include "pw_toolchain/no_destructor.h"
#if PW_RPC_USE_GLOBAL_MUTEX
-#include <mutex>
-
#include "pw_sync/mutex.h" // nogncheck
#endif // PW_RPC_USE_GLOBAL_MUTEX
@@ -29,7 +28,6 @@ namespace pw::rpc::internal {
#if PW_RPC_USE_GLOBAL_MUTEX
using RpcLock = sync::Mutex;
-using LockGuard = std::lock_guard<RpcLock>;
#else
@@ -39,18 +37,21 @@ class PW_LOCKABLE("pw::rpc::internal::RpcLock") RpcLock {
constexpr void unlock() PW_UNLOCK_FUNCTION() {}
};
-class PW_SCOPED_LOCKABLE LockGuard {
+#endif // PW_RPC_USE_GLOBAL_MUTEX
+
+inline RpcLock& rpc_lock() {
+ static NoDestructor<RpcLock> lock;
+ return *lock;
+}
+
+class PW_SCOPED_LOCKABLE RpcLockGuard {
public:
- // [[maybe_unused]] needs to be after the parameter to workaround a gcc bug
- // context: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81429
- constexpr LockGuard(RpcLock& mutex [[maybe_unused]])
- PW_EXCLUSIVE_LOCK_FUNCTION(mutex) {}
+ RpcLockGuard() PW_EXCLUSIVE_LOCK_FUNCTION(rpc_lock()) { rpc_lock().lock(); }
- ~LockGuard() PW_UNLOCK_FUNCTION() = default;
+ ~RpcLockGuard() PW_UNLOCK_FUNCTION(rpc_lock()) { rpc_lock().unlock(); }
};
-#endif // PW_RPC_USE_GLOBAL_MUTEX
-
-RpcLock& rpc_lock();
+// Releases the RPC lock, yields, and reacquires it.
+void YieldRpcLock() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
} // namespace pw::rpc::internal