C/C++ API Reference
Loading...
Searching...
No Matches
timed_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_chrono/system_clock.h"
19#include "pw_preprocessor/util.h"
20#include "pw_sync/lock_annotations.h"
21#include "pw_sync/mutex.h"
22
23#ifdef __cplusplus
24
25#include "pw_sync/virtual_basic_lockable.h"
26
27namespace pw::sync {
28
30
42class TimedMutex : public Mutex {
43 public:
44 TimedMutex() = default;
45 ~TimedMutex() = default;
46 TimedMutex(const TimedMutex&) = delete;
47 TimedMutex(TimedMutex&&) = delete;
48 TimedMutex& operator=(const TimedMutex&) = delete;
49 TimedMutex& operator=(TimedMutex&&) = delete;
50
57 [[nodiscard]] bool try_lock_for(chrono::SystemClock::duration timeout)
59
66 [[nodiscard]] bool try_lock_until(chrono::SystemClock::time_point deadline)
68};
69
70class PW_LOCKABLE("pw::sync::VirtualTimedMutex") VirtualTimedMutex final
71 : public GenericLockable<TimedMutex> {
72 public:
73 TimedMutex& timed_mutex() { return impl(); }
74
75 [[nodiscard]] bool try_lock_for(chrono::SystemClock::duration timeout)
77 return impl().try_lock_for(timeout);
78 }
79
80 [[nodiscard]] bool try_lock_until(chrono::SystemClock::time_point deadline)
82 return impl().try_lock_until(deadline);
83 }
84};
85
87
88} // namespace pw::sync
89
90#include "pw_sync_backend/timed_mutex_inline.h"
91
93
94#else // !defined(__cplusplus)
95
97
98#endif // __cplusplus
99
100PW_EXTERN_C_START
101
103
107
111
116
122
126
128
129PW_EXTERN_C_END
Definition: virtual_basic_lockable.h:109
Definition: mutex.h:40
Definition: timed_mutex.h:42
bool try_lock_for(chrono::SystemClock::duration timeout)
bool try_lock_until(chrono::SystemClock::time_point deadline)
Definition: timed_mutex.h:71
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
#define PW_LOCKABLE(name)
Definition: lock_annotations.h:208
void pw_sync_TimedMutex_Lock(pw_sync_TimedMutex *mutex)
Invokes the TimedMutex::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
bool pw_sync_TimedMutex_TryLockUntil(pw_sync_TimedMutex *mutex, pw_chrono_SystemClock_TimePoint deadline)
bool pw_sync_TimedMutex_TryLock(pw_sync_TimedMutex *mutex)
Invokes the TimedMutex::try_lock member function on the given mutex.
bool pw_sync_TimedMutex_TryLockFor(pw_sync_TimedMutex *mutex, pw_chrono_SystemClock_Duration timeout)
Invokes the TimedMutex::try_lock_for member function on the given mutex.
void pw_sync_TimedMutex_Unlock(pw_sync_TimedMutex *mutex)
Invokes the TimedMutex::unlock member function on the given mutex.
Definition: binary_semaphore.h:26
Definition: system_clock.h:228
Definition: system_clock.h:232