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

Side by Side Diff: base/synchronization/spin_lock.h

Issue 2517033002: Revert of Move WTF::SpinLock to base::SpinLock. (Closed)
Patch Set: Created 4 years, 1 month 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 | « base/BUILD.gn ('k') | base/synchronization/spin_lock.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_SYNCHRONIZATION_SPIN_LOCK_H
6 #define BASE_SYNCHRONIZATION_SPIN_LOCK_H
7
8 #include <atomic>
9 #include <memory>
10 #include <mutex>
11
12 #include "base/base_export.h"
13 #include "base/compiler_specific.h"
14
15 // Spinlock is a simple spinlock class based on the standard CPU primitive of
16 // atomic increment and decrement of an int at a given memory address. These are
17 // intended only for very short duration locks and assume a system with multiple
18 // cores. For any potentially longer wait you should use a real lock, such as
19 // |base::Lock|.
20 //
21 // |SpinLock|s MUST be globals. Using them as (e.g.) struct/class members will
22 // result in an uninitialized lock, which is dangerously incorrect.
23
24 namespace base {
25 namespace subtle {
26
27 class SpinLock {
28 public:
29 using Guard = std::lock_guard<SpinLock>;
30
31 ALWAYS_INLINE void lock() {
32 static_assert(sizeof(lock_) == sizeof(int),
33 "int and lock_ are different sizes");
34 if (LIKELY(!lock_.exchange(true, std::memory_order_acquire)))
35 return;
36 LockSlow();
37 }
38
39 ALWAYS_INLINE void unlock() { lock_.store(false, std::memory_order_release); }
40
41 private:
42 // This is called if the initial attempt to acquire the lock fails. It's
43 // slower, but has a much better scheduling and power consumption behavior.
44 BASE_EXPORT void LockSlow();
45
46 std::atomic_int lock_;
47 };
48
49 } // namespace subtle
50 } // namespace base
51
52 #endif // BASE_SYNCHRONIZATION_SPIN_LOCK_H
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/synchronization/spin_lock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698