| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkOnce_DEFINED | 8 #ifndef SkOnce_DEFINED |
| 9 #define SkOnce_DEFINED | 9 #define SkOnce_DEFINED |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 constexpr SkOnce() = default; | 22 constexpr SkOnce() = default; |
| 23 | 23 |
| 24 template <typename Fn, typename... Args> | 24 template <typename Fn, typename... Args> |
| 25 void operator()(Fn&& fn, Args&&... args) { | 25 void operator()(Fn&& fn, Args&&... args) { |
| 26 auto state = fState.load(std::memory_order_acquire); | 26 auto state = fState.load(std::memory_order_acquire); |
| 27 | 27 |
| 28 if (state == Done) { | 28 if (state == Done) { |
| 29 return; | 29 return; |
| 30 } | 30 } |
| 31 | 31 |
| 32 if (state == NotStarted) { | 32 // If it looks like no one has started calling fn(), try to claim that j
ob. |
| 33 // Try to claim the job of calling fn() by swapping from NotStarted
to Calling. | 33 if (state == NotStarted && fState.compare_exchange_strong(state, Claimed
, |
| 34 // See [1] below for why we use std::memory_order_acquire instead of
relaxed. | 34 std::memory_or
der_relaxed)) { |
| 35 if (fState.compare_exchange_strong(state, Calling, std::memory_order
_acquire)) { | 35 // Great! We'll run fn() then notify the other threads by releasing
Done into fState. |
| 36 // Claimed! Call fn(), then mark this SkOnce as Done. | 36 fn(std::forward<Args>(args)...); |
| 37 fn(std::forward<Args>(args)...); | 37 return fState.store(Done, std::memory_order_release); |
| 38 return fState.store(Done, std::memory_order_release); | |
| 39 } | |
| 40 } | 38 } |
| 41 | 39 |
| 42 while (state == Calling) { | 40 // Some other thread is calling fn(). |
| 43 // Some other thread is calling fn(). Wait for them to finish. | 41 // We'll just spin here acquiring until it releases Done into fState. |
| 44 state = fState.load(std::memory_order_acquire); | 42 while (fState.load(std::memory_order_acquire) != Done) { /*spin*/ } |
| 45 } | |
| 46 SkASSERT(state == Done); | |
| 47 } | 43 } |
| 48 | 44 |
| 49 private: | 45 private: |
| 50 enum State : uint8_t { NotStarted, Calling, Done}; | 46 enum State : uint8_t { NotStarted, Claimed, Done}; |
| 51 std::atomic<uint8_t> fState{NotStarted}; | 47 std::atomic<uint8_t> fState{NotStarted}; |
| 52 }; | 48 }; |
| 53 | 49 |
| 54 /* [1] Why do we compare_exchange_strong() with std::memory_order_acquire inste
ad of relaxed? | |
| 55 * | |
| 56 * If we succeed, we really only need a relaxed compare_exchange_strong()... we'
re the ones | |
| 57 * who are about to do a release store, so there's certainly nothing yet for an
acquire to | |
| 58 * synchronize with. | |
| 59 * | |
| 60 * If that compare_exchange_strong() fails, we're either in Calling or Done stat
e. | |
| 61 * Again, if we're in Calling state, relaxed would have been fine: the spin loop
will | |
| 62 * acquire up to the Calling thread's release store. | |
| 63 * | |
| 64 * But if that compare_exchange_strong() fails and we find ourselves in the Done
state, | |
| 65 * we've never done an acquire load to sync up to the store of that Done state. | |
| 66 * | |
| 67 * So on failure we need an acquire load. Generally the failure memory order ca
nnot be | |
| 68 * stronger than the success memory order, so we need acquire on success too. T
he single | |
| 69 * memory order version of compare_exchange_strong() uses the same acquire order
for both. | |
| 70 */ | |
| 71 | |
| 72 #endif // SkOnce_DEFINED | 50 #endif // SkOnce_DEFINED |
| OLD | NEW |