2015-06-17 17:50:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSemaphore_DEFINED
|
|
|
|
#define SkSemaphore_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkOnce.h"
|
2019-10-28 15:18:41 +00:00
|
|
|
#include "include/private/SkThreadAnnotations.h"
|
2020-02-07 15:36:46 +00:00
|
|
|
#include <algorithm>
|
2016-05-05 17:57:37 +00:00
|
|
|
#include <atomic>
|
2015-09-24 14:34:49 +00:00
|
|
|
|
2019-06-17 18:40:42 +00:00
|
|
|
class SkSemaphore {
|
2016-05-05 17:57:37 +00:00
|
|
|
public:
|
2019-06-17 18:40:42 +00:00
|
|
|
constexpr SkSemaphore(int count = 0) : fCount(count), fOSSemaphore(nullptr) {}
|
|
|
|
|
|
|
|
// Cleanup the underlying OS semaphore.
|
2020-05-13 14:44:49 +00:00
|
|
|
SK_SPI ~SkSemaphore();
|
2015-09-24 14:34:49 +00:00
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
// Increment the counter n times.
|
|
|
|
// Generally it's better to call signal(n) instead of signal() n times.
|
|
|
|
void signal(int n = 1);
|
2015-09-24 14:34:49 +00:00
|
|
|
|
|
|
|
// Decrement the counter by 1,
|
2017-02-22 19:33:12 +00:00
|
|
|
// then if the counter is < 0, sleep this thread until the counter is >= 0.
|
2016-05-05 17:57:37 +00:00
|
|
|
void wait();
|
2016-05-05 01:23:30 +00:00
|
|
|
|
2017-02-22 03:53:16 +00:00
|
|
|
// If the counter is positive, decrement it by 1 and return true, otherwise return false.
|
2020-05-13 14:44:49 +00:00
|
|
|
SK_SPI bool try_wait();
|
2017-02-22 03:53:16 +00:00
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
private:
|
2015-09-24 14:34:49 +00:00
|
|
|
// This implementation follows the general strategy of
|
|
|
|
// 'A Lightweight Semaphore with Partial Spinning'
|
|
|
|
// found here
|
|
|
|
// http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
|
|
|
|
// That article (and entire blog) are very much worth reading.
|
|
|
|
//
|
|
|
|
// We wrap an OS-provided semaphore with a user-space atomic counter that
|
|
|
|
// lets us avoid interacting with the OS semaphore unless strictly required:
|
2017-02-22 19:33:12 +00:00
|
|
|
// moving the count from >=0 to <0 or vice-versa, i.e. sleeping or waking threads.
|
2016-05-05 17:57:37 +00:00
|
|
|
struct OSSemaphore;
|
|
|
|
|
2020-05-13 14:44:49 +00:00
|
|
|
SK_SPI void osSignal(int n);
|
|
|
|
SK_SPI void osWait();
|
2016-05-05 17:57:37 +00:00
|
|
|
|
|
|
|
std::atomic<int> fCount;
|
|
|
|
SkOnce fOSSemaphoreOnce;
|
|
|
|
OSSemaphore* fOSSemaphore;
|
2016-05-04 18:31:29 +00:00
|
|
|
};
|
2015-06-17 17:50:25 +00:00
|
|
|
|
2019-06-17 18:40:42 +00:00
|
|
|
inline void SkSemaphore::signal(int n) {
|
2016-05-05 17:57:37 +00:00
|
|
|
int prev = fCount.fetch_add(n, std::memory_order_release);
|
2016-05-04 18:31:29 +00:00
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
// We only want to call the OS semaphore when our logical count crosses
|
2017-02-22 19:33:12 +00:00
|
|
|
// from <0 to >=0 (when we need to wake sleeping threads).
|
2016-05-05 17:57:37 +00:00
|
|
|
//
|
|
|
|
// This is easiest to think about with specific examples of prev and n.
|
|
|
|
// If n == 5 and prev == -3, there are 3 threads sleeping and we signal
|
2020-02-07 15:36:46 +00:00
|
|
|
// std::min(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
|
2016-05-05 17:57:37 +00:00
|
|
|
//
|
2020-02-07 15:36:46 +00:00
|
|
|
// If prev >= 0, no threads are waiting, std::min(-prev, n) is always <= 0,
|
2016-05-05 17:57:37 +00:00
|
|
|
// so we don't call the OS semaphore, leaving the count at (prev + n).
|
2020-02-07 15:36:46 +00:00
|
|
|
int toSignal = std::min(-prev, n);
|
2016-05-05 17:57:37 +00:00
|
|
|
if (toSignal > 0) {
|
|
|
|
this->osSignal(toSignal);
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 01:23:30 +00:00
|
|
|
|
2019-06-17 18:40:42 +00:00
|
|
|
inline void SkSemaphore::wait() {
|
2016-05-05 17:57:37 +00:00
|
|
|
// Since this fetches the value before the subtract, zero and below means that there are no
|
|
|
|
// resources left, so the thread needs to wait.
|
|
|
|
if (fCount.fetch_sub(1, std::memory_order_acquire) <= 0) {
|
2019-10-28 15:18:41 +00:00
|
|
|
SK_POTENTIALLY_BLOCKING_REGION_BEGIN;
|
2016-05-05 17:57:37 +00:00
|
|
|
this->osWait();
|
2019-10-28 15:18:41 +00:00
|
|
|
SK_POTENTIALLY_BLOCKING_REGION_END;
|
2016-05-05 17:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 17:50:25 +00:00
|
|
|
|
|
|
|
#endif//SkSemaphore_DEFINED
|