C/C++ API Reference
Loading...
Searching...
No Matches
binary_semaphore.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#include <stddef.h>
18
19#include "pw_chrono/system_clock.h"
20#include "pw_preprocessor/util.h"
21
22#ifdef __cplusplus
23
24#include "pw_sync_backend/binary_semaphore_native.h"
25
26namespace pw::sync {
27
29
44 public:
45 using native_handle_type = backend::NativeBinarySemaphoreHandle;
46
49 BinarySemaphore(const BinarySemaphore&) = delete;
51 BinarySemaphore& operator=(const BinarySemaphore&) = delete;
52 BinarySemaphore& operator=(BinarySemaphore&&) = delete;
53
64 void release();
65
69 void acquire();
70
76 [[nodiscard]] bool try_acquire() noexcept;
77
85 [[nodiscard]] bool try_acquire_for(chrono::SystemClock::duration timeout);
86
94 [[nodiscard]] bool try_acquire_until(
95 chrono::SystemClock::time_point deadline);
96
99 [[nodiscard]] static constexpr ptrdiff_t max() noexcept {
100 return backend::kBinarySemaphoreMaxValue;
101 }
102
103 [[nodiscard]] native_handle_type native_handle();
104
105 private:
107 backend::NativeBinarySemaphore native_type_;
108};
109
111
112} // namespace pw::sync
113
114#include "pw_sync_backend/binary_semaphore_inline.h"
115
117
118#else // !defined(__cplusplus)
119
121
122#endif // __cplusplus
123
124PW_EXTERN_C_START
125
126void pw_sync_BinarySemaphore_Release(pw_sync_BinarySemaphore* semaphore);
127void pw_sync_BinarySemaphore_Acquire(pw_sync_BinarySemaphore* semaphore);
128bool pw_sync_BinarySemaphore_TryAcquire(pw_sync_BinarySemaphore* semaphore);
129bool pw_sync_BinarySemaphore_TryAcquireFor(
131bool pw_sync_BinarySemaphore_TryAcquireUntil(
132 pw_sync_BinarySemaphore* semaphore,
134ptrdiff_t pw_sync_BinarySemaphore_Max(void);
135
136PW_EXTERN_C_END
Definition: binary_semaphore.h:43
bool try_acquire_until(chrono::SystemClock::time_point deadline)
bool try_acquire() noexcept
bool try_acquire_for(chrono::SystemClock::duration timeout)
static constexpr ptrdiff_t max() noexcept
Definition: binary_semaphore.h:99
Definition: binary_semaphore.h:26
Definition: system_clock.h:228
Definition: system_clock.h:232