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