1 | // Copyright (c) 2005, 2006, 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 | #if (PLATFORM(X86) || PLATFORM(PPC)) && (COMPILER(GCC) || COMPILER(MSVC))
|
---|
37 |
|
---|
38 | #include <time.h> /* For nanosleep() */
|
---|
39 |
|
---|
40 | #include <sched.h> /* For sched_yield() */
|
---|
41 |
|
---|
42 | #if HAVE(STDINT_H)
|
---|
43 | #include <stdint.h>
|
---|
44 | #elif HAVE(INTTYPES_H)
|
---|
45 | #include <inttypes.h>
|
---|
46 | #else
|
---|
47 | #include <sys/types.h>
|
---|
48 | #endif
|
---|
49 | #include <stdlib.h> /* for abort() */
|
---|
50 |
|
---|
51 | #if PLATFORM(WIN_OS)
|
---|
52 | #ifndef WIN32_LEAN_AND_MEAN
|
---|
53 | #define WIN32_LEAN_AND_MEAN
|
---|
54 | #endif
|
---|
55 | #include <windows.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | static void TCMalloc_SlowLock(volatile unsigned int* lockword);
|
---|
59 |
|
---|
60 | // The following is a struct so that it can be initialized at compile time
|
---|
61 | struct TCMalloc_SpinLock {
|
---|
62 |
|
---|
63 | inline void Lock() {
|
---|
64 | int r;
|
---|
65 | #if COMPILER(GCC)
|
---|
66 | #if PLATFORM(X86)
|
---|
67 | __asm__ __volatile__
|
---|
68 | ("xchgl %0, %1"
|
---|
69 | : "=r"(r), "=m"(lockword_)
|
---|
70 | : "0"(1), "m"(lockword_)
|
---|
71 | : "memory");
|
---|
72 | #else
|
---|
73 | volatile unsigned int *lockword_ptr = &lockword_;
|
---|
74 | __asm__ __volatile__
|
---|
75 | ("1: lwarx %0, 0, %1\n\t"
|
---|
76 | "stwcx. %2, 0, %1\n\t"
|
---|
77 | "bne- 1b\n\t"
|
---|
78 | "isync"
|
---|
79 | : "=&r" (r), "=r" (lockword_ptr)
|
---|
80 | : "r" (1), "1" (lockword_ptr)
|
---|
81 | : "memory");
|
---|
82 | #endif
|
---|
83 | #elif COMPILER(MSVC)
|
---|
84 | __asm {
|
---|
85 | mov eax, this ; store &lockword_ (which is this+0) in eax
|
---|
86 | mov ebx, 1 ; store 1 in ebx
|
---|
87 | xchg [eax], ebx ; exchange lockword_ and 1
|
---|
88 | mov r, ebx ; store old value of lockword_ in r
|
---|
89 | }
|
---|
90 | #endif
|
---|
91 | if (r) TCMalloc_SlowLock(&lockword_);
|
---|
92 | }
|
---|
93 |
|
---|
94 | inline void Unlock() {
|
---|
95 | #if COMPILER(GCC)
|
---|
96 | #if PLATFORM(X86)
|
---|
97 | __asm__ __volatile__
|
---|
98 | ("movl $0, %0"
|
---|
99 | : "=m"(lockword_)
|
---|
100 | : "m" (lockword_)
|
---|
101 | : "memory");
|
---|
102 | #else
|
---|
103 | __asm__ __volatile__
|
---|
104 | ("isync\n\t"
|
---|
105 | "eieio\n\t"
|
---|
106 | "stw %1, %0"
|
---|
107 | #if PLATFORM(DARWIN) || PLATFORM(PPC)
|
---|
108 | : "=o" (lockword_)
|
---|
109 | #else
|
---|
110 | : "=m" (lockword_)
|
---|
111 | #endif
|
---|
112 | : "r" (0)
|
---|
113 | : "memory");
|
---|
114 | #endif
|
---|
115 | #elif COMPILER(MSVC)
|
---|
116 | __asm {
|
---|
117 | mov eax, this ; store &lockword_ (which is this+0) in eax
|
---|
118 | mov [eax], 0 ; set lockword_ to 0
|
---|
119 | }
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 | // Report if we think the lock can be held by this thread.
|
---|
123 | // When the lock is truly held by the invoking thread
|
---|
124 | // we will always return true.
|
---|
125 | // Indended to be used as CHECK(lock.IsHeld());
|
---|
126 | inline bool IsHeld() const {
|
---|
127 | return lockword_ != 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | inline void Init() { lockword_ = 0; }
|
---|
131 |
|
---|
132 | volatile unsigned int lockword_;
|
---|
133 | };
|
---|
134 |
|
---|
135 | #define SPINLOCK_INITIALIZER { 0 }
|
---|
136 |
|
---|
137 | static void TCMalloc_SlowLock(volatile unsigned int* lockword) {
|
---|
138 | sched_yield(); // Yield immediately since fast path failed
|
---|
139 | while (true) {
|
---|
140 | int r;
|
---|
141 | #if COMPILER(GCC)
|
---|
142 | #if PLATFORM(X86)
|
---|
143 | __asm__ __volatile__
|
---|
144 | ("xchgl %0, %1"
|
---|
145 | : "=r"(r), "=m"(*lockword)
|
---|
146 | : "0"(1), "m"(*lockword)
|
---|
147 | : "memory");
|
---|
148 |
|
---|
149 | #else
|
---|
150 | int tmp = 1;
|
---|
151 | __asm__ __volatile__
|
---|
152 | ("1: lwarx %0, 0, %1\n\t"
|
---|
153 | "stwcx. %2, 0, %1\n\t"
|
---|
154 | "bne- 1b\n\t"
|
---|
155 | "isync"
|
---|
156 | : "=&r" (r), "=r" (lockword)
|
---|
157 | : "r" (tmp), "1" (lockword)
|
---|
158 | : "memory");
|
---|
159 | #endif
|
---|
160 | #elif COMPILER(MSVC)
|
---|
161 | __asm {
|
---|
162 | mov eax, lockword ; assign lockword into eax
|
---|
163 | mov ebx, 1 ; assign 1 into ebx
|
---|
164 | xchg [eax], ebx ; exchange *lockword and 1
|
---|
165 | mov r, ebx ; store old value of *lockword in r
|
---|
166 | }
|
---|
167 | #endif
|
---|
168 | if (!r) {
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // This code was adapted from the ptmalloc2 implementation of
|
---|
173 | // spinlocks which would sched_yield() upto 50 times before
|
---|
174 | // sleeping once for a few milliseconds. Mike Burrows suggested
|
---|
175 | // just doing one sched_yield() outside the loop and always
|
---|
176 | // sleeping after that. This change helped a great deal on the
|
---|
177 | // performance of spinlocks under high contention. A test program
|
---|
178 | // with 10 threads on a dual Xeon (four virtual processors) went
|
---|
179 | // from taking 30 seconds to 16 seconds.
|
---|
180 |
|
---|
181 | // Sleep for a few milliseconds
|
---|
182 | #if PLATFORM(WIN_OS)
|
---|
183 | Sleep(2);
|
---|
184 | #else
|
---|
185 | struct timespec tm;
|
---|
186 | tm.tv_sec = 0;
|
---|
187 | tm.tv_nsec = 2000001;
|
---|
188 | nanosleep(&tm, NULL);
|
---|
189 | #endif
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | #else
|
---|
194 |
|
---|
195 | #include <pthread.h>
|
---|
196 |
|
---|
197 | // Portable version
|
---|
198 | struct TCMalloc_SpinLock {
|
---|
199 | pthread_mutex_t private_lock_;
|
---|
200 |
|
---|
201 | inline void Init() {
|
---|
202 | if (pthread_mutex_init(&private_lock_, NULL) != 0) abort();
|
---|
203 | }
|
---|
204 | inline void Finalize() {
|
---|
205 | if (pthread_mutex_destroy(&private_lock_) != 0) abort();
|
---|
206 | }
|
---|
207 | inline void Lock() {
|
---|
208 | if (pthread_mutex_lock(&private_lock_) != 0) abort();
|
---|
209 | }
|
---|
210 | inline void Unlock() {
|
---|
211 | if (pthread_mutex_unlock(&private_lock_) != 0) abort();
|
---|
212 | }
|
---|
213 | };
|
---|
214 |
|
---|
215 | #define SPINLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
|
---|
216 |
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | // Corresponding locker object that arranges to acquire a spinlock for
|
---|
220 | // the duration of a C++ scope.
|
---|
221 | class TCMalloc_SpinLockHolder {
|
---|
222 | private:
|
---|
223 | TCMalloc_SpinLock* lock_;
|
---|
224 | public:
|
---|
225 | inline explicit TCMalloc_SpinLockHolder(TCMalloc_SpinLock* l)
|
---|
226 | : lock_(l) { l->Lock(); }
|
---|
227 | inline ~TCMalloc_SpinLockHolder() { lock_->Unlock(); }
|
---|
228 | };
|
---|
229 |
|
---|
230 | // Short-hands for convenient use by tcmalloc.cc
|
---|
231 | typedef TCMalloc_SpinLock SpinLock;
|
---|
232 | typedef TCMalloc_SpinLockHolder SpinLockHolder;
|
---|
233 |
|
---|
234 | #endif // TCMALLOC_INTERNAL_SPINLOCK_H__
|
---|