1
/* Copyright (c) 2006, Google Inc.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are
8
* * Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above
11
* copyright notice, this list of conditions and the following disclaimer
12
* in the documentation and/or other materials provided with the
14
* * Neither the name of Google Inc. nor the names of its
15
* contributors may be used to endorse or promote products derived from
16
* this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
* Author: Sanjay Ghemawat
35
#include <time.h> /* For nanosleep() */
36
#include <sched.h> /* For sched_yield() */
38
#include <unistd.h> /* For nanosleep() on Windows, read() */
40
#include <fcntl.h> /* for open(), O_RDONLY */
41
#include <string.h> /* for strncmp */
43
#include "base/spinlock.h"
44
#include "base/sysinfo.h" /* for NumCPUs() */
46
// We can do contention-profiling of SpinLocks, but the code is in
47
// mutex.cc, which is not always linked in with spinlock. Hence we
48
// provide this weak definition, which is used if mutex.cc isn't linked in.
49
ATTRIBUTE_WEAK extern void SubmitSpinLockProfileData(const void *, int64);
50
void SubmitSpinLockProfileData(const void *, int64) {}
52
static int adaptive_spin_count = 0;
54
const base::LinkerInitialized SpinLock::LINKER_INITIALIZED =
55
base::LINKER_INITIALIZED;
57
struct SpinLock_InitHelper {
58
SpinLock_InitHelper() {
59
// On multi-cpu machines, spin for longer before yielding
60
// the processor or sleeping. Reduces idle time significantly.
62
adaptive_spin_count = 1000;
67
// Hook into global constructor execution:
68
// We do not do adaptive spinning before that,
69
// but nothing lock-intensive should be going on at that time.
70
static SpinLock_InitHelper init_helper;
72
void SpinLock::SlowLock() {
73
int saved_errno = errno; // save and restore errno for signal safety
74
int c = adaptive_spin_count;
76
// Spin a few times in the hope that the lock holder releases the lock
77
while ((c > 0) && (lockword_ != 0)) {
82
sched_yield(); // Spinning failed. Let's try to be gentle.
85
while (Acquire_CompareAndSwap(&lockword_, 0, 1) != 0) {
86
// This code was adapted from the ptmalloc2 implementation of
87
// spinlocks which would sched_yield() upto 50 times before
88
// sleeping once for a few milliseconds. Mike Burrows suggested
89
// just doing one sched_yield() outside the loop and always
90
// sleeping after that. This change helped a great deal on the
91
// performance of spinlocks under high contention. A test program
92
// with 10 threads on a dual Xeon (four virtual processors) went
93
// from taking 30 seconds to 16 seconds.
95
// Sleep for a few milliseconds