1 | // Copyright (c) 2005, Google Inc.
|
---|
2 | // All rights reserved.
|
---|
3 | //
|
---|
4 | // Redistribution and use in source and binary forms, with or without
|
---|
5 | // modification, are permitted provided that the following conditions are
|
---|
6 | // met:
|
---|
7 | //
|
---|
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
|
---|
13 | // distribution.
|
---|
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.
|
---|
17 | //
|
---|
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.
|
---|
29 |
|
---|
30 | // ---
|
---|
31 | // Author: Sanjay Ghemawat <[email protected]>
|
---|
32 |
|
---|
33 | #ifndef TCMALLOC_INTERNAL_SPINLOCK_H__
|
---|
34 | #define TCMALLOC_INTERNAL_SPINLOCK_H__
|
---|
35 |
|
---|
36 | #include "config.h"
|
---|
37 | #include <time.h> /* For nanosleep() */
|
---|
38 | #include <sched.h> /* For sched_yield() */
|
---|
39 | #if HAVE(STDINT_H)
|
---|
40 | #include <stdint.h>
|
---|
41 | #elif HAVE(INTTYPES_H)
|
---|
42 | #include <inttypes.h>
|
---|
43 | #else
|
---|
44 | #include <sys/types.h>
|
---|
45 | #endif
|
---|
46 | #include <stdlib.h> /* for abort() */
|
---|
47 |
|
---|
48 | #if (PLATFORM(X86) || PLATFORM(PPC)) && COMPILER(GCC)
|
---|
49 | static void TCMalloc_SlowLock(volatile unsigned int* lockword);
|
---|
50 |
|
---|
51 | // The following is a struct so that it can be initialized at compile time
|
---|
52 | struct TCMalloc_SpinLock {
|
---|
53 | volatile unsigned int private_lockword_;
|
---|
54 |
|
---|
55 | inline void Init() { private_lockword_ = 0; }
|
---|
56 | inline void Finalize() { }
|
---|
57 |
|
---|
58 | inline void Lock() {
|
---|
59 | int r;
|
---|
60 | #if PLATFORM(X86)
|
---|
61 | __asm__ __volatile__
|
---|
62 | ("xchgl %0, %1"
|
---|
63 | : "=r"(r), "=m"(private_lockword_)
|
---|
64 | : "0"(1), "m"(private_lockword_)
|
---|
65 | : "memory");
|
---|
66 | #else
|
---|
67 | volatile unsigned int *lockword_ptr = &private_lockword_;
|
---|
68 | __asm__ __volatile__
|
---|
69 | ("1: lwarx %0, 0, %1\n\t"
|
---|
70 | "stwcx. %2, 0, %1\n\t"
|
---|
71 | "bne- 1b\n\t"
|
---|
72 | "isync"
|
---|
73 | : "=&r" (r), "=r" (lockword_ptr)
|
---|
74 | : "r" (1), "1" (lockword_ptr)
|
---|
75 | : "memory");
|
---|
76 | #endif
|
---|
77 | if (r) TCMalloc_SlowLock(&private_lockword_);
|
---|
78 | }
|
---|
79 |
|
---|
80 | inline void Unlock() {
|
---|
81 | #if PLATFORM(X86)
|
---|
82 | __asm__ __volatile__
|
---|
83 | ("movl $0, %0"
|
---|
84 | : "=m"(private_lockword_)
|
---|
85 | : "m" (private_lockword_)
|
---|
86 | : "memory");
|
---|
87 | #else
|
---|
88 | __asm__ __volatile__
|
---|
89 | ("isync\n\t"
|
---|
90 | "eieio\n\t"
|
---|
91 | "stw %1, %0"
|
---|
92 | : "=o" (private_lockword_)
|
---|
93 | : "r" (0)
|
---|
94 | : "memory");
|
---|
95 | #endif
|
---|
96 | }
|
---|
97 |
|
---|
98 | #ifdef WTF_CHANGES
|
---|
99 | inline bool IsLocked() {
|
---|
100 | return private_lockword_ != 0;
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 | };
|
---|
104 |
|
---|
105 | #define SPINLOCK_INITIALIZER { 0 }
|
---|
106 |
|
---|
107 | static void TCMalloc_SlowLock(volatile unsigned int* lockword) {
|
---|
108 | sched_yield(); // Yield immediately since fast path failed
|
---|
109 | while (true) {
|
---|
110 | int r;
|
---|
111 | #if PLATFORM(X86)
|
---|
112 | __asm__ __volatile__
|
---|
113 | ("xchgl %0, %1"
|
---|
114 | : "=r"(r), "=m"(*lockword)
|
---|
115 | : "0"(1), "m"(*lockword)
|
---|
116 | : "memory");
|
---|
117 |
|
---|
118 | #else
|
---|
119 | int tmp = 1;
|
---|
120 | __asm__ __volatile__
|
---|
121 | ("1: lwarx %0, 0, %1\n\t"
|
---|
122 | "stwcx. %2, 0, %1\n\t"
|
---|
123 | "bne- 1b\n\t"
|
---|
124 | "isync"
|
---|
125 | : "=&r" (r), "=r" (lockword)
|
---|
126 | : "r" (tmp), "1" (lockword)
|
---|
127 | : "memory");
|
---|
128 | #endif
|
---|
129 | if (!r) {
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // This code was adapted from the ptmalloc2 implementation of
|
---|
134 | // spinlocks which would sched_yield() upto 50 times before
|
---|
135 | // sleeping once for a few milliseconds. Mike Burrows suggested
|
---|
136 | // just doing one sched_yield() outside the loop and always
|
---|
137 | // sleeping after that. This change helped a great deal on the
|
---|
138 | // performance of spinlocks under high contention. A test program
|
---|
139 | // with 10 threads on a dual Xeon (four virtual processors) went
|
---|
140 | // from taking 30 seconds to 16 seconds.
|
---|
141 |
|
---|
142 | // Sleep for a few milliseconds
|
---|
143 | struct timespec tm;
|
---|
144 | tm.tv_sec = 0;
|
---|
145 | tm.tv_nsec = 2000001;
|
---|
146 | nanosleep(&tm, NULL);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | #else
|
---|
151 |
|
---|
152 | #include <pthread.h>
|
---|
153 |
|
---|
154 | // Portable version
|
---|
155 | struct TCMalloc_SpinLock {
|
---|
156 | pthread_mutex_t private_lock_;
|
---|
157 |
|
---|
158 | inline void Init() {
|
---|
159 | if (pthread_mutex_init(&private_lock_, NULL) != 0) abort();
|
---|
160 | }
|
---|
161 | inline void Finalize() {
|
---|
162 | if (pthread_mutex_destroy(&private_lock_) != 0) abort();
|
---|
163 | }
|
---|
164 | inline void Lock() {
|
---|
165 | if (pthread_mutex_lock(&private_lock_) != 0) abort();
|
---|
166 | }
|
---|
167 | inline void Unlock() {
|
---|
168 | if (pthread_mutex_unlock(&private_lock_) != 0) abort();
|
---|
169 | }
|
---|
170 | };
|
---|
171 |
|
---|
172 | #define SPINLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
|
---|
173 |
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | // Corresponding locker object that arranges to acquire a spinlock for
|
---|
177 | // the duration of a C++ scope.
|
---|
178 | class TCMalloc_SpinLockHolder {
|
---|
179 | private:
|
---|
180 | TCMalloc_SpinLock* lock_;
|
---|
181 | public:
|
---|
182 | inline explicit TCMalloc_SpinLockHolder(TCMalloc_SpinLock* l)
|
---|
183 | : lock_(l) { l->Lock(); }
|
---|
184 | inline ~TCMalloc_SpinLockHolder() { lock_->Unlock(); }
|
---|
185 | };
|
---|
186 |
|
---|
187 | // Short-hands for convenient use by tcmalloc.cc
|
---|
188 | typedef TCMalloc_SpinLock SpinLock;
|
---|
189 | typedef TCMalloc_SpinLockHolder SpinLockHolder;
|
---|
190 |
|
---|
191 | #endif // TCMALLOC_INTERNAL_SPINLOCK_H__
|
---|