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 <atomic> | 11 #include <atomic> |
12 #include <utility> | 12 #include <utility> |
13 #include "SkTypes.h" | 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 constexpr SkOnce() = default; |
| 23 |
22 template <typename Fn, typename... Args> | 24 template <typename Fn, typename... Args> |
23 void operator()(Fn&& fn, Args&&... args) { | 25 void operator()(Fn&& fn, Args&&... args) { |
24 auto state = fState.load(std::memory_order_acquire); | 26 auto state = fState.load(std::memory_order_acquire); |
25 | 27 |
26 if (state == Done) { | 28 if (state == Done) { |
27 return; | 29 return; |
28 } | 30 } |
29 | 31 |
30 if (state == NotStarted) { | 32 if (state == NotStarted) { |
31 // Try to claim the job of calling fn() by swapping from NotStarted
to Calling. | 33 // Try to claim the job of calling fn() by swapping from NotStarted
to Calling. |
(...skipping 29 matching lines...) Expand all Loading... |
61 * | 63 * |
62 * But if that compare_exchange_strong() fails and we find ourselves in the Done
state, | 64 * 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. | 65 * we've never done an acquire load to sync up to the store of that Done state. |
64 * | 66 * |
65 * So on failure we need an acquire load. Generally the failure memory order ca
nnot be | 67 * 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 | 68 * 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. | 69 * memory order version of compare_exchange_strong() uses the same acquire order
for both. |
68 */ | 70 */ |
69 | 71 |
70 #endif // SkOnce_DEFINED | 72 #endif // SkOnce_DEFINED |
OLD | NEW |