mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 SkOnce_DEFINED |
| 9 | #define SkOnce_DEFINED |
| 10 | |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 11 | #include <atomic> |
| 12 | #include <utility> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkTypes.h" |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 14 | |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 15 | // SkOnce provides call-once guarantees for Skia, much like std::once_flag/std::call_once(). |
| 16 | // |
| 17 | // There should be no particularly error-prone gotcha use cases when using SkOnce. |
| 18 | // It works correctly as a class member, a local, a global, a function-scoped static, whatever. |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 19 | |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 20 | class SkOnce { |
mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 21 | public: |
mtklein | e86e51f | 2016-04-29 13:58:18 -0700 | [diff] [blame] | 22 | constexpr SkOnce() = default; |
| 23 | |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 24 | template <typename Fn, typename... Args> |
| 25 | void operator()(Fn&& fn, Args&&... args) { |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame] | 26 | auto state = fState.load(std::memory_order_acquire); |
| 27 | |
| 28 | if (state == Done) { |
| 29 | return; |
mtklein | df02d33 | 2016-04-20 10:54:54 -0700 | [diff] [blame] | 30 | } |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame] | 31 | |
mtklein | b37c68a | 2016-05-04 13:57:30 -0700 | [diff] [blame] | 32 | // If it looks like no one has started calling fn(), try to claim that job. |
| 33 | if (state == NotStarted && fState.compare_exchange_strong(state, Claimed, |
Lee Salzman | ee1c73f | 2016-12-16 13:56:32 -0500 | [diff] [blame] | 34 | std::memory_order_relaxed, |
mtklein | b37c68a | 2016-05-04 13:57:30 -0700 | [diff] [blame] | 35 | std::memory_order_relaxed)) { |
| 36 | // Great! We'll run fn() then notify the other threads by releasing Done into fState. |
| 37 | fn(std::forward<Args>(args)...); |
| 38 | return fState.store(Done, std::memory_order_release); |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame] | 39 | } |
| 40 | |
mtklein | b37c68a | 2016-05-04 13:57:30 -0700 | [diff] [blame] | 41 | // Some other thread is calling fn(). |
| 42 | // We'll just spin here acquiring until it releases Done into fState. |
| 43 | while (fState.load(std::memory_order_acquire) != Done) { /*spin*/ } |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 44 | } |
commit-bot@chromium.org | ba9354b | 2014-02-10 19:58:49 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | ba9354b | 2014-02-10 19:58:49 +0000 | [diff] [blame] | 46 | private: |
mtklein | b37c68a | 2016-05-04 13:57:30 -0700 | [diff] [blame] | 47 | enum State : uint8_t { NotStarted, Claimed, Done}; |
mtklein | 650f9e9 | 2016-04-20 13:49:15 -0700 | [diff] [blame] | 48 | std::atomic<uint8_t> fState{NotStarted}; |
commit-bot@chromium.org | ba9354b | 2014-02-10 19:58:49 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
mtklein@google.com | 3a19fb5 | 2013-10-09 16:12:23 +0000 | [diff] [blame] | 51 | #endif // SkOnce_DEFINED |