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