Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: include/private/SkOnce.h

Issue 1936653002: Add reminders that these classes have constexpr default constructors. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/private/SkSpinlock.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « no previous file | include/private/SkSpinlock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698