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

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

Issue 1734383002: Modernize SkSpinlock. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: std::atomic<bool> Created 4 years, 10 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 | « include/private/SkOnce.h ('k') | src/core/SkSpinlock.cpp » ('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 2015 Google Inc. 2 * Copyright 2015 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 // This file is not part of the public Skia API.
9
10 #ifndef SkSpinlock_DEFINED 8 #ifndef SkSpinlock_DEFINED
11 #define SkSpinlock_DEFINED 9 #define SkSpinlock_DEFINED
12 10
13 #include "../private/SkAtomics.h" 11 #include <atomic>
14 12
15 #define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name 13 class SkSpinlock {
16
17 // This class has no constructor and must be zero-initialized (the macro above d oes this).
18 class SK_API SkPODSpinlock {
19 public: 14 public:
20 void acquire() { 15 void acquire() {
21 // To act as a mutex, we need an acquire barrier if we take the lock. 16 // To act as a mutex, we need an acquire barrier when we acquire the loc k.
22 if (sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { 17 if (fLocked.exchange(true, std::memory_order_acquire)) {
23 // Lock was contended. Fall back to an out-of-line spin loop. 18 // Lock was contended. Fall back to an out-of-line spin loop.
24 this->contendedAcquire(); 19 this->contendedAcquire();
25 } 20 }
26 } 21 }
27 22
28 void release() { 23 void release() {
29 // To act as a mutex, we need a release barrier. 24 // To act as a mutex, we need a release barrier when we release the lock .
30 sk_atomic_store(&fLocked, false, sk_memory_order_release); 25 fLocked.store(false, std::memory_order_release);
31 } 26 }
32 27
33 private: 28 private:
34 void contendedAcquire(); 29 void contendedAcquire();
35 bool fLocked;
36 };
37 30
38 // For non-global-static use cases, this is normally what you want. 31 std::atomic<bool> fLocked{false};
39 class SkSpinlock : public SkPODSpinlock {
40 public:
41 SkSpinlock() { this->release(); }
42 }; 32 };
43 33
44 #endif//SkSpinlock_DEFINED 34 #endif//SkSpinlock_DEFINED
OLDNEW
« no previous file with comments | « include/private/SkOnce.h ('k') | src/core/SkSpinlock.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698