Pigweed
 
Loading...
Searching...
No Matches
counting_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/counting_semaphore_native.h"
25
26namespace pw::sync {
27
46 public:
47 using native_handle_type = backend::NativeCountingSemaphoreHandle;
48
51 CountingSemaphore(const CountingSemaphore&) = delete;
53 CountingSemaphore& operator=(const CountingSemaphore&) = delete;
54 CountingSemaphore& operator=(CountingSemaphore&&) = delete;
55
64 void release(ptrdiff_t update = 1);
65
69 void acquire();
70
75 [[nodiscard]] bool try_acquire() noexcept;
76
83 [[nodiscard]] bool try_acquire_for(chrono::SystemClock::duration timeout);
84
92 [[nodiscard]] bool try_acquire_until(
93 chrono::SystemClock::time_point deadline);
94
96 [[nodiscard]] static constexpr ptrdiff_t max() noexcept {
97 return backend::kCountingSemaphoreMaxValue;
98 }
99
100 native_handle_type native_handle();
101
102 private:
104 backend::NativeCountingSemaphore native_type_;
105};
106
107} // namespace pw::sync
108
109#include "pw_sync_backend/counting_semaphore_inline.h"
110
112
113#else // !defined(__cplusplus)
114
116
117#endif // __cplusplus
118
119PW_EXTERN_C_START
120
121void pw_sync_CountingSemaphore_Release(pw_sync_CountingSemaphore* semaphore);
122void pw_sync_CountingSemaphore_ReleaseNum(pw_sync_CountingSemaphore* semaphore,
123 ptrdiff_t update);
124void pw_sync_CountingSemaphore_Acquire(pw_sync_CountingSemaphore* semaphore);
125bool pw_sync_CountingSemaphore_TryAcquire(pw_sync_CountingSemaphore* semaphore);
126bool pw_sync_CountingSemaphore_TryAcquireFor(
127 pw_sync_CountingSemaphore* semaphore,
129bool pw_sync_CountingSemaphore_TryAcquireUntil(
130 pw_sync_CountingSemaphore* semaphore,
132ptrdiff_t pw_sync_CountingSemaphore_Max(void);
133
134PW_EXTERN_C_END
Definition: counting_semaphore.h:45
bool try_acquire() noexcept
bool try_acquire_until(chrono::SystemClock::time_point deadline)
static constexpr ptrdiff_t max() noexcept
Returns the internal counter's maximum possible value.
Definition: counting_semaphore.h:96
bool try_acquire_for(chrono::SystemClock::duration timeout)
void release(ptrdiff_t update=1)
Definition: system_clock.h:224
Definition: system_clock.h:228