Flutter Engine
 
Loading...
Searching...
No Matches
semaphore.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FML_SYNCHRONIZATION_SEMAPHORE_H_
6#define FLUTTER_FML_SYNCHRONIZATION_SEMAPHORE_H_
7
8#include <memory>
9
10#include "flutter/fml/macros.h"
11
12namespace fml {
13
14class PlatformSemaphore;
15
16//------------------------------------------------------------------------------
17/// @brief A traditional counting semaphore. `Wait`s decrement the counter
18/// and `Signal` increments it.
19///
20/// This is a cross-platform replacement for std::counting_semaphore
21/// which is only available since C++20. Once Flutter migrates past
22/// that point, this class should become obsolete and must be
23/// replaced.
24///
25class Semaphore {
26 public:
27 //----------------------------------------------------------------------------
28 /// @brief Initializes the counting semaphore to a specified start count.
29 ///
30 /// @warning Callers must check if the handle could be successfully created
31 /// by calling the `IsValid` method. `Wait`s on an invalid
32 /// semaphore will always fail and signals will fail silently.
33 ///
34 /// @param[in] count The starting count of the counting semaphore.
35 ///
36 explicit Semaphore(uint32_t count);
37
38 //----------------------------------------------------------------------------
39 /// @brief Destroy the counting semaphore.
40 ///
42
43 //----------------------------------------------------------------------------
44 /// @brief Check if the underlying semaphore handle could be created.
45 /// Failure modes are platform specific and may occur due to issue
46 /// like handle exhaustion. All `Wait`s on invalid semaphore
47 /// handles will fail and `Signal` calls will be ignored.
48 ///
49 /// @return True if valid, False otherwise.
50 ///
51 bool IsValid() const;
52
53 //----------------------------------------------------------------------------
54 /// @brief Decrements the count and waits indefinitely if the value is
55 /// less than zero for a `Signal`.
56 ///
57 /// @return If the `Wait` call was successful. See `IsValid` for failure.
58 ///
59 [[nodiscard]] bool Wait();
60
61 //----------------------------------------------------------------------------
62 /// @brief Decrement the counts if it is greater than zero. Returns false
63 /// if the counter is already at zero.
64 ///
65 /// @warning False is also returned if the semaphore handle is invalid.
66 /// Which makes doing the validity check before this call doubly
67 /// important.
68 ///
69 /// @return If the count could be decremented.
70 ///
71 [[nodiscard]] bool TryWait();
72
73 //----------------------------------------------------------------------------
74 /// @brief Increment the count by one. Any pending `Wait`s will be
75 /// resolved at this point.
76 ///
77 void Signal();
78
79 private:
80 std::unique_ptr<PlatformSemaphore> impl_;
81
83};
84
85} // namespace fml
86
87#endif // FLUTTER_FML_SYNCHRONIZATION_SEMAPHORE_H_
A traditional counting semaphore. Waits decrement the counter and Signal increments it.
Definition semaphore.h:25
bool IsValid() const
Check if the underlying semaphore handle could be created. Failure modes are platform specific and ma...
Definition semaphore.cc:177
~Semaphore()
Destroy the counting semaphore.
void Signal()
Increment the count by one. Any pending Waits will be resolved at this point.
Definition semaphore.cc:189
bool TryWait()
Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.
Definition semaphore.cc:185
bool Wait()
Decrements the count and waits indefinitely if the value is less than zero for a Signal.
Definition semaphore.cc:181
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27