Flutter Engine
The Flutter Engine
Classes | Functions
SkSpinlock.h File Reference
#include "include/private/base/SkAPI.h"
#include "include/private/base/SkThreadAnnotations.h"
#include <atomic>

Go to the source code of this file.

Classes

class  SkAutoSpinlock
 

Functions

class SK_CAPABILITY ("mutex") SkSpinlock
 

Function Documentation

◆ SK_CAPABILITY()

class SK_CAPABILITY ( "mutex"  )

Definition at line 1 of file SkSpinlock.h.

16 {
17public:
18 constexpr SkSpinlock() = default;
19
20 void acquire() SK_ACQUIRE() {
21 // To act as a mutex, we need an acquire barrier when we acquire the lock.
22 if (fLocked.exchange(true, std::memory_order_acquire)) {
23 // Lock was contended. Fall back to an out-of-line spin loop.
24 this->contendedAcquire();
25 }
26 }
27
28 // Acquire the lock or fail (quickly). Lets the caller decide to do something other than wait.
29 bool tryAcquire() SK_TRY_ACQUIRE(true) {
30 // To act as a mutex, we need an acquire barrier when we acquire the lock.
31 if (fLocked.exchange(true, std::memory_order_acquire)) {
32 // Lock was contended. Let the caller decide what to do.
33 return false;
34 }
35 return true;
36 }
37
38 void release() SK_RELEASE_CAPABILITY() {
39 // To act as a mutex, we need a release barrier when we release the lock.
40 fLocked.store(false, std::memory_order_release);
41 }
42
43private:
44 SK_API void contendedAcquire();
45
46 std::atomic<bool> fLocked{false};
47};
#define SK_API
Definition: SkAPI.h:35
#define SK_RELEASE_CAPABILITY(...)
#define SK_TRY_ACQUIRE(...)
#define SK_ACQUIRE(...)