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 |
11 #include "../private/SkSpinlock.h" | |
12 #include <atomic> | 11 #include <atomic> |
13 #include <utility> | 12 #include <utility> |
| 13 #include "SkTypes.h" |
14 | 14 |
15 // SkOnce provides call-once guarantees for Skia, much like std::once_flag/std::
call_once(). | 15 // SkOnce provides call-once guarantees for Skia, much like std::once_flag/std::
call_once(). |
16 // | 16 // |
17 // There should be no particularly error-prone gotcha use cases when using SkOnc
e. | 17 // There should be no particularly error-prone gotcha use cases when using SkOnc
e. |
18 // It works correctly as a class member, a local, a global, a function-scoped st
atic, whatever. | 18 // It works correctly as a class member, a local, a global, a function-scoped st
atic, whatever. |
19 | 19 |
20 class SkOnce { | 20 class SkOnce { |
21 public: | 21 public: |
22 template <typename Fn, typename... Args> | 22 template <typename Fn, typename... Args> |
23 void operator()(Fn&& fn, Args&&... args) { | 23 void operator()(Fn&& fn, Args&&... args) { |
24 // Vanilla double-checked locking. | 24 auto state = fState.load(std::memory_order_acquire); |
25 if (!fDone.load(std::memory_order_acquire)) { | 25 |
26 fLock.acquire(); | 26 if (state == Done) { |
27 if (!fDone.load(std::memory_order_relaxed)) { | 27 return; |
| 28 } |
| 29 |
| 30 if (state == NotStarted) { |
| 31 // Try to claim the job of calling fn() by swapping from NotStarted
to Calling. |
| 32 // See [1] below for why we use std::memory_order_acquire instead of
relaxed. |
| 33 if (fState.compare_exchange_strong(state, Calling, std::memory_order
_acquire)) { |
| 34 // Claimed! Call fn(), then mark this SkOnce as Done. |
28 fn(std::forward<Args>(args)...); | 35 fn(std::forward<Args>(args)...); |
29 fDone.store(true, std::memory_order_release); | 36 return fState.store(Done, std::memory_order_release); |
30 } | 37 } |
31 fLock.release(); | |
32 } | 38 } |
| 39 |
| 40 while (state == Calling) { |
| 41 // Some other thread is calling fn(). Wait for them to finish. |
| 42 state = fState.load(std::memory_order_acquire); |
| 43 } |
| 44 SkASSERT(state == Done); |
33 } | 45 } |
34 | 46 |
35 private: | 47 private: |
36 std::atomic<bool> fDone{false}; | 48 enum State : uint8_t { NotStarted, Calling, Done}; |
37 SkSpinlock fLock; | 49 std::atomic<uint8_t> fState{NotStarted}; |
38 }; | 50 }; |
39 | 51 |
| 52 /* [1] Why do we compare_exchange_strong() with std::memory_order_acquire inste
ad of relaxed? |
| 53 * |
| 54 * If we succeed, we really only need a relaxed compare_exchange_strong()... we'
re the ones |
| 55 * who are about to do a release store, so there's certainly nothing yet for an
acquire to |
| 56 * synchronize with. |
| 57 * |
| 58 * If that compare_exchange_strong() fails, we're either in Calling or Done stat
e. |
| 59 * Again, if we're in Calling state, relaxed would have been fine: the spin loop
will |
| 60 * acquire up to the Calling thread's release store. |
| 61 * |
| 62 * But if that compare_exchange_strong() fails and we find ourselves in the Done
state, |
| 63 * we've never done an acquire load to sync up to the store of that Done state. |
| 64 * |
| 65 * So on failure we need an acquire load. Generally the failure memory order ca
nnot be |
| 66 * stronger than the success memory order, so we need acquire on success too. T
he single |
| 67 * memory order version of compare_exchange_strong() uses the same acquire order
for both. |
| 68 */ |
| 69 |
40 #endif // SkOnce_DEFINED | 70 #endif // SkOnce_DEFINED |
OLD | NEW |