C/C++ API Reference
Loading...
Searching...
No Matches
mutex.h
1// Copyright 2020 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 <stdbool.h>
17
18#include "pw_preprocessor/util.h"
19#include "pw_sync/lock_annotations.h"
20
21#ifdef __cplusplus
22
23#include "pw_sync/virtual_basic_lockable.h"
24#include "pw_sync_backend/mutex_native.h"
25
26namespace pw::sync {
27
29
40class PW_LOCKABLE("pw::sync::Mutex") Mutex {
41 public:
42 using native_handle_type = backend::NativeMutexHandle;
43
44 Mutex();
45 ~Mutex();
46 Mutex(const Mutex&) = delete;
47 Mutex(Mutex&&) = delete;
48 Mutex& operator=(const Mutex&) = delete;
49 Mutex& operator=(Mutex&&) = delete;
50
56
62 [[nodiscard]] bool try_lock() PW_EXCLUSIVE_TRYLOCK_FUNCTION(true);
63
67 void unlock() PW_UNLOCK_FUNCTION();
68
69 [[nodiscard]] native_handle_type native_handle();
70
71 protected:
75 backend::NativeMutex& native_type() { return native_type_; }
76 const backend::NativeMutex& native_type() const { return native_type_; }
77
78 private:
80 backend::NativeMutex native_type_;
81};
82
83class PW_LOCKABLE("pw::sync::VirtualMutex") VirtualMutex final
84 : public GenericLockable<Mutex> {
85 public:
86 Mutex& mutex() { return impl(); }
87};
88
90
91} // namespace pw::sync
92
93#include "pw_sync_backend/mutex_inline.h"
94
96
97#else // !defined(__cplusplus)
98
99typedef struct pw_sync_Mutex pw_sync_Mutex;
100
101#endif // __cplusplus
102
103PW_EXTERN_C_START
104
106
109
112
115
117
118PW_EXTERN_C_END
Definition: virtual_basic_lockable.h:109
Definition: mutex.h:40
Definition: mutex.h:84
#define PW_LOCKABLE(name)
Definition: lock_annotations.h:208
bool pw_sync_Mutex_TryLock(pw_sync_Mutex *mutex)
Invokes the Mutex::try_lock member function on the given mutex.
#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
void pw_sync_Mutex_Unlock(pw_sync_Mutex *mutex)
Invokes the Mutex::unlock member function on the given mutex.
void pw_sync_Mutex_Lock(pw_sync_Mutex *mutex)
Invokes the Mutex::lock member function on the given mutex.
#define PW_UNLOCK_FUNCTION(...)
Definition: lock_annotations.h:247
Definition: binary_semaphore.h:26