C/C++ API Reference
Loading...
Searching...
No Matches
virtual_basic_lockable.h
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_polyfill/language_feature_macros.h"
17#include "pw_sync/lock_annotations.h"
18
19namespace pw::sync {
20
22
31class PW_LOCKABLE("pw::sync::VirtualBasicLockable") VirtualBasicLockable {
32 public:
33 void lock() PW_EXCLUSIVE_LOCK_FUNCTION() {
34 DoLockOperation(Operation::kLock);
35 }
36
37 void unlock() PW_UNLOCK_FUNCTION() { DoLockOperation(Operation::kUnlock); }
38
39 protected:
40 ~VirtualBasicLockable() = default;
41
42 enum class Operation {
43 kLock,
44 kUnlock,
45 };
46
47 private:
50 virtual void DoLockOperation(Operation operation) = 0;
51};
52
55class PW_LOCKABLE("pw::sync::NoOpLock") NoOpLock final
56 : public VirtualBasicLockable {
57 public:
58 constexpr NoOpLock() {}
59 NoOpLock(const NoOpLock&) = delete;
60 NoOpLock(NoOpLock&&) = delete;
61 NoOpLock& operator=(const NoOpLock&) = delete;
62 NoOpLock& operator=(NoOpLock&&) = delete;
63
66 static NoOpLock& Instance() {
67 PW_CONSTINIT static NoOpLock lock;
68 return lock;
69 }
70
71 private:
72 void DoLockOperation(Operation) override {}
73};
74
78template <typename LockType>
80 public:
81 virtual ~GenericBasicLockable() = default;
82
83 protected:
84 LockType& impl() { return impl_; }
85
86 private:
87 void DoLockOperation(Operation operation) override
89 switch (operation) {
90 case Operation::kLock:
91 return impl_.lock();
92
93 case Operation::kUnlock:
94 default:
95 return impl_.unlock();
96 }
97 }
98
99 LockType impl_;
100};
101
107template <typename LockType>
108class PW_LOCKABLE("pw::sync::GenericLockable") GenericLockable
109 : public GenericBasicLockable<LockType> {
110 public:
111 [[nodiscard]] bool try_lock() PW_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
112 return GenericBasicLockable<LockType>::impl().try_lock();
113 }
114};
115
117
118} // namespace pw::sync
Definition: virtual_basic_lockable.h:79
void DoLockOperation(Operation operation) override
Definition: virtual_basic_lockable.h:87
Definition: virtual_basic_lockable.h:109
Definition: virtual_basic_lockable.h:56
void DoLockOperation(Operation) override
Definition: virtual_basic_lockable.h:72
static NoOpLock & Instance()
Definition: virtual_basic_lockable.h:66
Definition: virtual_basic_lockable.h:31
virtual void DoLockOperation(Operation operation)=0
#define PW_CONSTINIT
Definition: language_feature_macros.h:52
#define PW_LOCKABLE(name)
Definition: lock_annotations.h:208
#define PW_EXCLUSIVE_TRYLOCK_FUNCTION(...)
Definition: lock_annotations.h:260
#define PW_NO_LOCK_SAFETY_ANALYSIS
Definition: lock_annotations.h:292
#define PW_EXCLUSIVE_LOCK_FUNCTION(...)
Definition: lock_annotations.h:230
#define PW_UNLOCK_FUNCTION(...)
Definition: lock_annotations.h:247
Definition: binary_semaphore.h:26