1 // Copyright (c) 2005, 2006, Google Inc.
2 // All rights reserved.
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 <opensource@google.com>
33 #ifndef TCMALLOC_INTERNAL_SPINLOCK_H__
34 #define TCMALLOC_INTERNAL_SPINLOCK_H__
36 #if (PLATFORM(X86) || PLATFORM(PPC)) && (COMPILER(GCC) || COMPILER(MSVC))
38 #include <time.h> /* For nanosleep() */
40 #include <sched.h> /* For sched_yield() */
44 #elif HAVE(INTTYPES_H)
47 #include <sys/types.h>
49 #include <stdlib.h> /* for abort() */
52 #define WIN32_LEAN_AND_MEAN
56 static void TCMalloc_SlowLock(volatile unsigned int* lockword);
58 // The following is a struct so that it can be initialized at compile time
59 struct TCMalloc_SpinLock {
67 : "=r"(r), "=m"(lockword_)
68 : "0"(1), "m"(lockword_)
71 volatile unsigned int *lockword_ptr = &lockword_;
73 ("1: lwarx %0, 0, %1\n\t"
74 "stwcx. %2, 0, %1\n\t"
77 : "=&r" (r), "=r" (lockword_ptr)
78 : "r" (1), "1" (lockword_ptr)
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
89 if (r) TCMalloc_SlowLock(&lockword_);
92 inline void Unlock() {
115 mov eax, this ; store &lockword_ (which is this+0) in eax
116 mov [eax], 0 ; set lockword_ to 0
120 // Report if we think the lock can be held by this thread.
121 // When the lock is truly held by the invoking thread
122 // we will always return true.
123 // Indended to be used as CHECK(lock.IsHeld());
124 inline bool IsHeld() const {
125 return lockword_ != 0;
128 inline void Init() { lockword_ = 0; }
130 volatile unsigned int lockword_;
133 #define SPINLOCK_INITIALIZER { 0 }
135 static void TCMalloc_SlowLock(volatile unsigned int* lockword) {
136 sched_yield(); // Yield immediately since fast path failed
143 : "=r"(r), "=m"(*lockword)
144 : "0"(1), "m"(*lockword)
150 ("1: lwarx %0, 0, %1\n\t"
151 "stwcx. %2, 0, %1\n\t"
154 : "=&r" (r), "=r" (lockword)
155 : "r" (tmp), "1" (lockword)
160 mov eax, lockword ; assign lockword into eax
161 mov ebx, 1 ; assign 1 into ebx
162 xchg [eax], ebx ; exchange *lockword and 1
163 mov r, ebx ; store old value of *lockword in r
170 // This code was adapted from the ptmalloc2 implementation of
171 // spinlocks which would sched_yield() upto 50 times before
172 // sleeping once for a few milliseconds. Mike Burrows suggested
173 // just doing one sched_yield() outside the loop and always
174 // sleeping after that. This change helped a great deal on the
175 // performance of spinlocks under high contention. A test program
176 // with 10 threads on a dual Xeon (four virtual processors) went
177 // from taking 30 seconds to 16 seconds.
179 // Sleep for a few milliseconds
185 tm.tv_nsec = 2000001;
186 nanosleep(&tm, NULL);
196 struct TCMalloc_SpinLock {
197 pthread_mutex_t private_lock_;
200 if (pthread_mutex_init(&private_lock_, NULL) != 0) abort();
202 inline void Finalize() {
203 if (pthread_mutex_destroy(&private_lock_) != 0) abort();
206 if (pthread_mutex_lock(&private_lock_) != 0) abort();
208 inline void Unlock() {
209 if (pthread_mutex_unlock(&private_lock_) != 0) abort();
213 #define SPINLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
217 // Corresponding locker object that arranges to acquire a spinlock for
218 // the duration of a C++ scope.
219 class TCMalloc_SpinLockHolder {
221 TCMalloc_SpinLock* lock_;
223 inline explicit TCMalloc_SpinLockHolder(TCMalloc_SpinLock* l)
224 : lock_(l) { l->Lock(); }
225 inline ~TCMalloc_SpinLockHolder() { lock_->Unlock(); }
228 // Short-hands for convenient use by tcmalloc.cc
229 typedef TCMalloc_SpinLock SpinLock;
230 typedef TCMalloc_SpinLockHolder SpinLockHolder;
232 #endif // TCMALLOC_INTERNAL_SPINLOCK_H__