Pigweed
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
43class TimedMutex : public Mutex {
44 public:
45 TimedMutex() = default;
46 ~TimedMutex() = default;
47 TimedMutex(const TimedMutex&) = delete;
48 TimedMutex(TimedMutex&&) = delete;
49 TimedMutex& operator=(const TimedMutex&) = delete;
50 TimedMutex& operator=(TimedMutex&&) = delete;
51
60 PW_EXCLUSIVE_TRYLOCK_FUNCTION(true);
61
69 bool try_lock_until(chrono::SystemClock::time_point deadline)
70 PW_EXCLUSIVE_TRYLOCK_FUNCTION(true);
71};
72
73class PW_LOCKABLE("pw::sync::VirtualTimedMutex") VirtualTimedMutex final
74 : public GenericBasicLockable<TimedMutex> {
75 public:
76 TimedMutex& timed_mutex() { return impl(); }
77};
78
79} // namespace pw::sync
80
81#include "pw_sync_backend/timed_mutex_inline.h"
82
84
85#else // !defined(__cplusplus)
86
88
89#endif // __cplusplus
90
91PW_EXTERN_C_START
92
94void pw_sync_TimedMutex_Lock(pw_sync_TimedMutex* mutex)
95 PW_NO_LOCK_SAFETY_ANALYSIS;
96
98bool pw_sync_TimedMutex_TryLock(pw_sync_TimedMutex* mutex)
99 PW_NO_LOCK_SAFETY_ANALYSIS;
100
102bool pw_sync_TimedMutex_TryLockFor(pw_sync_TimedMutex* mutex,
104 PW_NO_LOCK_SAFETY_ANALYSIS;
105
108bool pw_sync_TimedMutex_TryLockUntil(pw_sync_TimedMutex* mutex,
110 PW_NO_LOCK_SAFETY_ANALYSIS;
111
113void pw_sync_TimedMutex_Unlock(pw_sync_TimedMutex* mutex)
114 PW_NO_LOCK_SAFETY_ANALYSIS;
115
116PW_EXTERN_C_END
Definition: virtual_basic_lockable.h:80
Definition: mutex.h:42
Definition: timed_mutex.h:43
bool try_lock_for(chrono::SystemClock::duration timeout)
bool try_lock_until(chrono::SystemClock::time_point deadline)
Definition: timed_mutex.h:74
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:84
Definition: system_clock.h:212
Definition: system_clock.h:216