Pigweed
 
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
42 public:
43 using native_handle_type = backend::NativeBinarySemaphoreHandle;
44
47 BinarySemaphore(const BinarySemaphore&) = delete;
49 BinarySemaphore& operator=(const BinarySemaphore&) = delete;
50 BinarySemaphore& operator=(BinarySemaphore&&) = delete;
51
62 void release();
63
67 void acquire();
68
74 [[nodiscard]] bool try_acquire() noexcept;
75
83 [[nodiscard]] bool try_acquire_for(chrono::SystemClock::duration timeout);
84
92 [[nodiscard]] bool try_acquire_until(
93 chrono::SystemClock::time_point deadline);
94
97 [[nodiscard]] static constexpr ptrdiff_t max() noexcept {
98 return backend::kBinarySemaphoreMaxValue;
99 }
100
101 [[nodiscard]] native_handle_type native_handle();
102
103 private:
105 backend::NativeBinarySemaphore native_type_;
106};
107
108} // namespace pw::sync
109
110#include "pw_sync_backend/binary_semaphore_inline.h"
111
113
114#else // !defined(__cplusplus)
115
117
118#endif // __cplusplus
119
120PW_EXTERN_C_START
121
122void pw_sync_BinarySemaphore_Release(pw_sync_BinarySemaphore* semaphore);
123void pw_sync_BinarySemaphore_Acquire(pw_sync_BinarySemaphore* semaphore);
124bool pw_sync_BinarySemaphore_TryAcquire(pw_sync_BinarySemaphore* semaphore);
125bool pw_sync_BinarySemaphore_TryAcquireFor(
127bool pw_sync_BinarySemaphore_TryAcquireUntil(
128 pw_sync_BinarySemaphore* semaphore,
130ptrdiff_t pw_sync_BinarySemaphore_Max(void);
131
132PW_EXTERN_C_END
Definition: binary_semaphore.h:41
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:97
Definition: system_clock.h:224
Definition: system_clock.h:228