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

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

Issue 2484803003: Move WTF::SpinLock to base::SpinLock. (Closed)
Patch Set: Rebase and fix build (NaCl can't have inline assembly). 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
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 be using a real lock.
19
20 namespace base {
21
22 class SpinLock {
23 public:
24 using Guard = std::lock_guard<SpinLock>;
25
26 ALWAYS_INLINE void lock() {
27 static_assert(sizeof(lock_) == sizeof(int),
28 "int and lock_ are different sizes");
29 if (LIKELY(!lock_.exchange(true, std::memory_order_acquire)))
30 return;
31 LockSlow();
32 }
33
34 ALWAYS_INLINE void unlock() { lock_.store(false, std::memory_order_release); }
35
36 private:
37 // This is called if the initial attempt to acquire the lock fails. It's
38 // slower, but has a much better scheduling and power consumption behavior.
39 BASE_EXPORT void LockSlow();
40
41 std::atomic_int lock_;
42 };
43
44 } // namespace base
45
46 #endif // BASE_SYNCHRONIZATION_SPIN_LOCK_H
OLDNEW
« base/BUILD.gn ('K') | « base/BUILD.gn ('k') | base/synchronization/spin_lock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698