OLD | NEW |
---|---|
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
1 #ifndef SkAtomics_sync_DEFINED | 8 #ifndef SkAtomics_sync_DEFINED |
2 #define SkAtomics_sync_DEFINED | 9 #define SkAtomics_sync_DEFINED |
3 | 10 |
4 // This file is mostly a shim. We'd like to delete it. Please don't put much | 11 // This file is mostly a shim. We'd like to delete it. Please don't put much |
5 // effort into maintaining it, and if you find bugs in it, the right fix is to | 12 // effort into maintaining it, and if you find bugs in it, the right fix is to |
6 // delete this file and upgrade your compiler to something that supports | 13 // delete this file and upgrade your compiler to something that supports |
7 // __atomic builtins or std::atomic. | 14 // __atomic builtins or std::atomic. |
8 | 15 |
9 static inline void barrier(sk_memory_order mo) { | 16 static inline void barrier(sk_memory_order mo) { |
10 asm volatile("" : : : "memory"); // Prevents the compiler from reordering c ode. | 17 asm volatile("" : : : "memory"); // Prevents the compiler from reordering c ode. |
(...skipping 30 matching lines...) Expand all Loading... | |
41 template <typename T> | 48 template <typename T> |
42 bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, sk_memory_order, sk_memory_order) { | 49 bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, sk_memory_order, sk_memory_order) { |
43 T prev = __sync_val_compare_and_swap(ptr, *expected, desired); | 50 T prev = __sync_val_compare_and_swap(ptr, *expected, desired); |
44 if (prev == *expected) { | 51 if (prev == *expected) { |
45 return true; | 52 return true; |
46 } | 53 } |
47 *expected = prev; | 54 *expected = prev; |
48 return false; | 55 return false; |
49 } | 56 } |
50 | 57 |
58 template <typename T> | |
59 T sk_atomic_exchange(T* ptr, T val, sk_memory_order) { | |
60 // There is no __sync exchange. Emulate it with a CAS loop. | |
61 T prev; | |
62 do { | |
63 prev = sk_atomic_load(ptr); | |
64 } while(!sk_atomic_compare_exchange(ptr, &prev, val)); | |
bungeman-skia
2015/03/30 14:52:17
nit: usually there is a space between the 'while'
| |
65 return prev; | |
66 } | |
67 | |
51 #endif//SkAtomics_sync_DEFINED | 68 #endif//SkAtomics_sync_DEFINED |
OLD | NEW |