Debian Codegraph @

extra/gperftools/gperftools-2.15/src/base/spinlock.cc

raw
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /* Copyright (c) 2006, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * ---
32 * Author: Sanjay Ghemawat
33 */
34
35 #include <config.h>
36 #include "base/spinlock.h"
37 #include "base/spinlock_internal.h"
38 #include "base/sysinfo.h" /* for GetSystemCPUsCount() */
39
40 // NOTE on the Lock-state values:
41 //
42 // kSpinLockFree represents the unlocked state
43 // kSpinLockHeld represents the locked state with no waiters
44 // kSpinLockSleeper represents the locked state with waiters
45
46 static int adaptive_spin_count = 0;
47
48 const base::LinkerInitialized SpinLock::LINKER_INITIALIZED =
49 base::LINKER_INITIALIZED;
50
51 namespace {
52 struct SpinLock_InitHelper {
53 SpinLock_InitHelper() {
54 // On multi-cpu machines, spin for longer before yielding
55 // the processor or sleeping. Reduces idle time significantly.
56 if (GetSystemCPUsCount() > 1) {
57 adaptive_spin_count = 1000;
58 }
59 }
60 };
61
62 // Hook into global constructor execution:
63 // We do not do adaptive spinning before that,
64 // but nothing lock-intensive should be going on at that time.
65 static SpinLock_InitHelper init_helper;
66
67 inline void SpinlockPause(void) {
68 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
69 __asm__ __volatile__("rep; nop" : : );
70 #elif defined(__GNUC__) && defined(__aarch64__)
71 __asm__ __volatile__("isb" : : );
72 #endif
73 }
74
75 } // unnamed namespace
76
77 // Monitor the lock to see if its value changes within some time
78 // period (adaptive_spin_count loop iterations). The last value read
79 // from the lock is returned from the method.
80 int SpinLock::SpinLoop() {
81 int c = adaptive_spin_count;
82 while (lockword_.load(std::memory_order_relaxed) != kSpinLockFree && --c > 0) {
83 SpinlockPause();
84 }
85 int old = kSpinLockFree;
86 lockword_.compare_exchange_strong(old, kSpinLockSleeper, std::memory_order_acquire);
87 // note, that we try to set lock word to 'have sleeper' state might
88 // look unnecessary, but:
89 //
90 // *) pay attention to second call to SpinLoop at the bottom of SlowLock loop below
91 //
92 // *) note, that we get there after sleeping in SpinLockDelay and
93 // getting woken by Unlock
94 //
95 // *) also note, that we don't "count" sleepers, so when unlock
96 // awakes us, it also sets lock word to "free". So we risk
97 // forgetting other sleepers. And to prevent this, we become
98 // "designated waker", by setting lock word to "have sleeper". So
99 // then when we unlock, we also wake up someone.
100 return old;
101 }
102
103 void SpinLock::SlowLock() {
104 int lock_value = SpinLoop();
105
106 int lock_wait_call_count = 0;
107 while (lock_value != kSpinLockFree) {
108 // If the lock is currently held, but not marked as having a sleeper, mark
109 // it as having a sleeper.
110 if (lock_value == kSpinLockHeld) {
111 // Here, just "mark" that the thread is going to sleep. Don't
112 // store the lock wait time in the lock as that will cause the
113 // current lock owner to think it experienced contention. Note,
114 // compare_exchange updates lock_value with previous value of
115 // lock word.
116 lockword_.compare_exchange_strong(lock_value, kSpinLockSleeper,
117 std::memory_order_acquire);
118 if (lock_value == kSpinLockHeld) {
119 // Successfully transitioned to kSpinLockSleeper. Pass
120 // kSpinLockSleeper to the SpinLockDelay routine to properly indicate
121 // the last lock_value observed.
122 lock_value = kSpinLockSleeper;
123 } else if (lock_value == kSpinLockFree) {
124 // Lock is free again, so try and acquire it before sleeping. The
125 // new lock state will be the number of cycles this thread waited if
126 // this thread obtains the lock.
127 lockword_.compare_exchange_strong(lock_value, kSpinLockSleeper, std::memory_order_acquire);
128 continue; // skip the delay at the end of the loop
129 }
130 }
131
132 // Wait for an OS specific delay.
133 base::internal::SpinLockDelay(&lockword_, lock_value,
134 ++lock_wait_call_count);
135 // Spin again after returning from the wait routine to give this thread
136 // some chance of obtaining the lock.
137 lock_value = SpinLoop();
138 }
139 }
140
141 void SpinLock::SlowUnlock() {
142 // wake waiter if necessary
143 base::internal::SpinLockWake(&lockword_, false);
144 }