Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
fml::Semaphore Class Reference

A traditional counting semaphore. Waits decrement the counter and Signal increments it. More...

#include <semaphore.h>

Public Member Functions

 Semaphore (uint32_t count)
 Initializes the counting semaphore to a specified start count. More...
 
 ~Semaphore ()
 Destroy the counting semaphore. More...
 
bool IsValid () const
 Check if the underlying semaphore handle could be created. Failure modes are platform specific and may occur due to issue like handle exhaustion. All Waits on invalid semaphore handles will fail and Signal calls will be ignored. More...
 
bool Wait ()
 Decrements the count and waits indefinitely if the value is less than zero for a Signal. More...
 
bool TryWait ()
 Decrement the counts if it is greater than zero. Returns false if the counter is already at zero. More...
 
void Signal ()
 Increment the count by one. Any pending Waits will be resolved at this point. More...
 

Detailed Description

A traditional counting semaphore. Waits decrement the counter and Signal increments it.

This is a cross-platform replacement for std::counting_semaphore which is only available since C++20. Once Flutter migrates past that point, this class should become obsolete and must be replaced.

Definition at line 26 of file semaphore.h.

Constructor & Destructor Documentation

◆ Semaphore()

fml::Semaphore::Semaphore ( uint32_t  count)
explicit

Initializes the counting semaphore to a specified start count.

Warning
Callers must check if the handle could be successfully created by calling the IsValid method. Waits on an invalid semaphore will always fail and signals will fail silently.
Parameters
[in]countThe starting count of the counting semaphore.

Definition at line 173 of file semaphore.cc.

173: impl_(new PlatformSemaphore(count)) {}
int count
Definition: FontMgrTest.cpp:50

◆ ~Semaphore()

fml::Semaphore::~Semaphore ( )
default

Destroy the counting semaphore.

Member Function Documentation

◆ IsValid()

bool fml::Semaphore::IsValid ( ) const

Check if the underlying semaphore handle could be created. Failure modes are platform specific and may occur due to issue like handle exhaustion. All Waits on invalid semaphore handles will fail and Signal calls will be ignored.

Returns
True if valid, False otherwise.

Definition at line 177 of file semaphore.cc.

177 {
178 return impl_->IsValid();
179}

◆ Signal()

void fml::Semaphore::Signal ( )

Increment the count by one. Any pending Waits will be resolved at this point.

Definition at line 189 of file semaphore.cc.

189 {
190 return impl_->Signal();
191}

◆ TryWait()

bool fml::Semaphore::TryWait ( )

Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.

Warning
False is also returned if the semaphore handle is invalid. Which makes doing the validity check before this call doubly important.
Returns
If the count could be decremented.

Definition at line 185 of file semaphore.cc.

185 {
186 return impl_->TryWait();
187}

◆ Wait()

bool fml::Semaphore::Wait ( )

Decrements the count and waits indefinitely if the value is less than zero for a Signal.

Returns
If the Wait call was successful. See IsValid for failure.

Definition at line 181 of file semaphore.cc.

181 {
182 return impl_->Wait();
183}

The documentation for this class was generated from the following files: