blob: 29bbca6fa9b4aa2b64a527e2a45335dd4d312375 [file] [log] [blame]
mtklein61fa22b2015-06-17 10:50:25 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkSemaphore_DEFINED
9#define SkSemaphore_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/private/SkOnce.h"
mtklein42846ed2016-05-05 10:57:37 -070013#include <atomic>
herb7f0a3d72015-09-24 07:34:49 -070014
Herb Derby9c71e7b2019-06-17 14:40:42 -040015class SkSemaphore {
mtklein42846ed2016-05-05 10:57:37 -070016public:
Herb Derby9c71e7b2019-06-17 14:40:42 -040017 constexpr SkSemaphore(int count = 0) : fCount(count), fOSSemaphore(nullptr) {}
18
19 // Cleanup the underlying OS semaphore.
20 ~SkSemaphore();
herb7f0a3d72015-09-24 07:34:49 -070021
mtklein42846ed2016-05-05 10:57:37 -070022 // Increment the counter n times.
23 // Generally it's better to call signal(n) instead of signal() n times.
24 void signal(int n = 1);
herb7f0a3d72015-09-24 07:34:49 -070025
26 // Decrement the counter by 1,
Mike Klein19eec392017-02-22 14:33:12 -050027 // then if the counter is < 0, sleep this thread until the counter is >= 0.
mtklein42846ed2016-05-05 10:57:37 -070028 void wait();
herb7f0a3d72015-09-24 07:34:49 -070029
Mike Klein384b90a2017-02-21 22:53:16 -050030 // If the counter is positive, decrement it by 1 and return true, otherwise return false.
31 bool try_wait();
32
mtklein42846ed2016-05-05 10:57:37 -070033private:
herb7f0a3d72015-09-24 07:34:49 -070034 // This implementation follows the general strategy of
35 // 'A Lightweight Semaphore with Partial Spinning'
36 // found here
37 // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
38 // That article (and entire blog) are very much worth reading.
39 //
40 // We wrap an OS-provided semaphore with a user-space atomic counter that
41 // lets us avoid interacting with the OS semaphore unless strictly required:
Mike Klein19eec392017-02-22 14:33:12 -050042 // moving the count from >=0 to <0 or vice-versa, i.e. sleeping or waking threads.
mtklein42846ed2016-05-05 10:57:37 -070043 struct OSSemaphore;
44
45 void osSignal(int n);
46 void osWait();
47
48 std::atomic<int> fCount;
49 SkOnce fOSSemaphoreOnce;
50 OSSemaphore* fOSSemaphore;
herb7f0a3d72015-09-24 07:34:49 -070051};
mtklein61fa22b2015-06-17 10:50:25 -070052
Herb Derby9c71e7b2019-06-17 14:40:42 -040053inline void SkSemaphore::signal(int n) {
mtklein42846ed2016-05-05 10:57:37 -070054 int prev = fCount.fetch_add(n, std::memory_order_release);
55
56 // We only want to call the OS semaphore when our logical count crosses
Mike Klein19eec392017-02-22 14:33:12 -050057 // from <0 to >=0 (when we need to wake sleeping threads).
mtklein42846ed2016-05-05 10:57:37 -070058 //
59 // This is easiest to think about with specific examples of prev and n.
60 // If n == 5 and prev == -3, there are 3 threads sleeping and we signal
61 // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
62 //
63 // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0,
64 // so we don't call the OS semaphore, leaving the count at (prev + n).
65 int toSignal = SkTMin(-prev, n);
66 if (toSignal > 0) {
67 this->osSignal(toSignal);
68 }
69}
70
Herb Derby9c71e7b2019-06-17 14:40:42 -040071inline void SkSemaphore::wait() {
mtklein42846ed2016-05-05 10:57:37 -070072 // Since this fetches the value before the subtract, zero and below means that there are no
73 // resources left, so the thread needs to wait.
74 if (fCount.fetch_sub(1, std::memory_order_acquire) <= 0) {
75 this->osWait();
76 }
77}
78
mtklein61fa22b2015-06-17 10:50:25 -070079#endif//SkSemaphore_DEFINED