1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_rpc/internal/config.h" 17 #include "pw_sync/lock_annotations.h" 18 19 #if PW_RPC_USE_GLOBAL_MUTEX 20 21 #include <mutex> 22 23 #include "pw_sync/mutex.h" // nogncheck 24 25 #endif // PW_RPC_USE_GLOBAL_MUTEX 26 27 namespace pw::rpc::internal { 28 29 #if PW_RPC_USE_GLOBAL_MUTEX 30 31 using RpcLock = sync::Mutex; 32 using LockGuard = std::lock_guard<RpcLock>; 33 34 #else 35 36 class PW_LOCKABLE("pw::rpc::internal::RpcLock") RpcLock { 37 public: 38 constexpr void lock() PW_EXCLUSIVE_LOCK_FUNCTION() {} 39 constexpr void unlock() PW_UNLOCK_FUNCTION() {} 40 }; 41 42 class PW_SCOPED_LOCKABLE LockGuard { 43 public: 44 // [[maybe_unused]] needs to be after the parameter to workaround a gcc bug 45 // context: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81429 46 constexpr LockGuard(RpcLock& mutex [[maybe_unused]]) 47 PW_EXCLUSIVE_LOCK_FUNCTION(mutex) {} 48 49 ~LockGuard() PW_UNLOCK_FUNCTION() = default; 50 }; 51 52 #endif // PW_RPC_USE_GLOBAL_MUTEX 53 54 RpcLock& rpc_lock(); 55 56 } // namespace pw::rpc::internal 57