SkSemaphore.h 2.7 KB

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