mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 11 | #include "../private/SkOnce.h" |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 12 | #include "SkTypes.h" |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 13 | #include <atomic> |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 14 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 15 | class SkBaseSemaphore { |
| 16 | public: |
| 17 | constexpr SkBaseSemaphore(int count = 0) |
| 18 | : fCount(count), fOSSemaphore(nullptr) {} |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 19 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 20 | // Increment the counter n times. |
| 21 | // Generally it's better to call signal(n) instead of signal() n times. |
| 22 | void signal(int n = 1); |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 23 | |
| 24 | // Decrement the counter by 1, |
Mike Klein | 19eec39 | 2017-02-22 14:33:12 -0500 | [diff] [blame] | 25 | // then if the counter is < 0, sleep this thread until the counter is >= 0. |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 26 | void wait(); |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 27 | |
Mike Klein | 384b90a | 2017-02-21 22:53:16 -0500 | [diff] [blame] | 28 | // If the counter is positive, decrement it by 1 and return true, otherwise return false. |
| 29 | bool try_wait(); |
| 30 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 31 | // SkBaseSemaphore has no destructor. Call this to clean it up. |
| 32 | void cleanup(); |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 33 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 34 | private: |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 35 | // This implementation follows the general strategy of |
| 36 | // 'A Lightweight Semaphore with Partial Spinning' |
| 37 | // found here |
| 38 | // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ |
| 39 | // That article (and entire blog) are very much worth reading. |
| 40 | // |
| 41 | // We wrap an OS-provided semaphore with a user-space atomic counter that |
| 42 | // lets us avoid interacting with the OS semaphore unless strictly required: |
Mike Klein | 19eec39 | 2017-02-22 14:33:12 -0500 | [diff] [blame] | 43 | // moving the count from >=0 to <0 or vice-versa, i.e. sleeping or waking threads. |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 44 | struct OSSemaphore; |
| 45 | |
| 46 | void osSignal(int n); |
| 47 | void osWait(); |
| 48 | |
| 49 | std::atomic<int> fCount; |
| 50 | SkOnce fOSSemaphoreOnce; |
| 51 | OSSemaphore* fOSSemaphore; |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 52 | }; |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 53 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 54 | class SkSemaphore : public SkBaseSemaphore { |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 55 | public: |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 56 | using SkBaseSemaphore::SkBaseSemaphore; |
| 57 | ~SkSemaphore() { this->cleanup(); } |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 58 | }; |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 59 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 60 | inline void SkBaseSemaphore::signal(int n) { |
| 61 | int prev = fCount.fetch_add(n, std::memory_order_release); |
| 62 | |
| 63 | // We only want to call the OS semaphore when our logical count crosses |
Mike Klein | 19eec39 | 2017-02-22 14:33:12 -0500 | [diff] [blame] | 64 | // from <0 to >=0 (when we need to wake sleeping threads). |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 65 | // |
| 66 | // This is easiest to think about with specific examples of prev and n. |
| 67 | // If n == 5 and prev == -3, there are 3 threads sleeping and we signal |
| 68 | // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2. |
| 69 | // |
| 70 | // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0, |
| 71 | // so we don't call the OS semaphore, leaving the count at (prev + n). |
| 72 | int toSignal = SkTMin(-prev, n); |
| 73 | if (toSignal > 0) { |
| 74 | this->osSignal(toSignal); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | inline void SkBaseSemaphore::wait() { |
| 79 | // Since this fetches the value before the subtract, zero and below means that there are no |
| 80 | // resources left, so the thread needs to wait. |
| 81 | if (fCount.fetch_sub(1, std::memory_order_acquire) <= 0) { |
| 82 | this->osWait(); |
| 83 | } |
| 84 | } |
| 85 | |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 86 | #endif//SkSemaphore_DEFINED |