Pigweed
 
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
42class PW_LOCKABLE("pw::sync::Mutex") Mutex {
43 public:
44 using native_handle_type = backend::NativeMutexHandle;
45
46 Mutex();
47 ~Mutex();
48 Mutex(const Mutex&) = delete;
49 Mutex(Mutex&&) = delete;
50 Mutex& operator=(const Mutex&) = delete;
51 Mutex& operator=(Mutex&&) = delete;
52
58 void lock() PW_EXCLUSIVE_LOCK_FUNCTION();
59
66 [[nodiscard]] bool try_lock() PW_EXCLUSIVE_TRYLOCK_FUNCTION(true);
67
72 void unlock() PW_UNLOCK_FUNCTION();
73
74 [[nodiscard]] native_handle_type native_handle();
75
76 protected:
80 backend::NativeMutex& native_type() { return native_type_; }
81 const backend::NativeMutex& native_type() const { return native_type_; }
82
83 private:
85 backend::NativeMutex native_type_;
86};
87
88class PW_LOCKABLE("pw::sync::VirtualMutex") VirtualMutex final
89 : public GenericLockable<Mutex> {
90 public:
91 Mutex& mutex() { return impl(); }
92};
93
94} // namespace pw::sync
95
96#include "pw_sync_backend/mutex_inline.h"
97
99
100#else // !defined(__cplusplus)
101
102typedef struct pw_sync_Mutex pw_sync_Mutex;
103
104#endif // __cplusplus
105
106PW_EXTERN_C_START
107
109void pw_sync_Mutex_Lock(pw_sync_Mutex* mutex) PW_NO_LOCK_SAFETY_ANALYSIS;
110
112bool pw_sync_Mutex_TryLock(pw_sync_Mutex* mutex) PW_NO_LOCK_SAFETY_ANALYSIS;
113
115void pw_sync_Mutex_Unlock(pw_sync_Mutex* mutex) PW_NO_LOCK_SAFETY_ANALYSIS;
116
117PW_EXTERN_C_END
Definition: virtual_basic_lockable.h:108
Definition: mutex.h:42
Definition: mutex.h:89