Flutter Engine
The Flutter Engine
Classes | Public Member Functions | List of all members
SkSemaphore Class Reference

#include <SkSemaphore.h>

Classes

struct  OSSemaphore
 

Public Member Functions

constexpr SkSemaphore (int count=0)
 
SK_SPI ~SkSemaphore ()
 
void signal (int n=1)
 
void wait ()
 
SK_SPI bool try_wait ()
 

Detailed Description

Definition at line 18 of file SkSemaphore.h.

Constructor & Destructor Documentation

◆ SkSemaphore()

constexpr SkSemaphore::SkSemaphore ( int  count = 0)
inlineconstexpr

Definition at line 20 of file SkSemaphore.h.

20: fCount(count), fOSSemaphore(nullptr) {}
int count
Definition: FontMgrTest.cpp:50

◆ ~SkSemaphore()

SkSemaphore::~SkSemaphore ( )

Definition at line 63 of file SkSemaphore.cpp.

63 {
64 delete fOSSemaphore;
65}

Member Function Documentation

◆ signal()

void SkSemaphore::signal ( int  n = 1)
inline

Definition at line 56 of file SkSemaphore.h.

56 {
57 int prev = fCount.fetch_add(n, std::memory_order_release);
58
59 // We only want to call the OS semaphore when our logical count crosses
60 // from <0 to >=0 (when we need to wake sleeping threads).
61 //
62 // This is easiest to think about with specific examples of prev and n.
63 // If n == 5 and prev == -3, there are 3 threads sleeping and we signal
64 // std::min(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
65 //
66 // If prev >= 0, no threads are waiting, std::min(-prev, n) is always <= 0,
67 // so we don't call the OS semaphore, leaving the count at (prev + n).
68 int toSignal = std::min(-prev, n);
69 if (toSignal > 0) {
70 this->osSignal(toSignal);
71 }
72}
static float prev(float f)
static float min(float r, float g, float b)
Definition: hsl.cpp:48

◆ try_wait()

bool SkSemaphore::try_wait ( )

Definition at line 77 of file SkSemaphore.cpp.

77 {
78 int count = fCount.load(std::memory_order_relaxed);
79 if (count > 0) {
80 return fCount.compare_exchange_weak(count, count-1, std::memory_order_acquire);
81 }
82 return false;
83}

◆ wait()

void SkSemaphore::wait ( )
inline

Definition at line 74 of file SkSemaphore.h.

74 {
75 // Since this fetches the value before the subtract, zero and below means that there are no
76 // resources left, so the thread needs to wait.
77 if (fCount.fetch_sub(1, std::memory_order_acquire) <= 0) {
79 this->osWait();
81 }
82}
#define SK_POTENTIALLY_BLOCKING_REGION_END
#define SK_POTENTIALLY_BLOCKING_REGION_BEGIN

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