1
// Copyright (c) 2005, 2006, Google Inc.
2
// Copyright (c) 2010, Patrick Gansterer <paroga@paroga.com>
3
// All rights reserved.
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
// Author: Sanjay Ghemawat <opensource@google.com>
34
#ifndef TCMALLOC_INTERNAL_SPINLOCK_H__
35
#define TCMALLOC_INTERNAL_SPINLOCK_H__
37
#include <wtf/Atomics.h>
42
#if ENABLE(COMPARE_AND_SWAP)
44
static void TCMalloc_SlowLock(unsigned* lockword);
46
// The following is a struct so that it can be initialized at compile time
47
struct TCMalloc_SpinLock {
49
if (!WTF::weakCompareAndSwap(&lockword_, 0, 1))
50
TCMalloc_SlowLock(&lockword_);
51
WTF::memoryBarrierAfterLock();
55
WTF::memoryBarrierBeforeUnlock();
59
// Report if we think the lock can be held by this thread.
60
// When the lock is truly held by the invoking thread
61
// we will always return true.
62
// Indended to be used as CHECK(lock.IsHeld());
64
return lockword_ != 0;
67
void Init() { lockword_ = 0; }
73
#define SPINLOCK_INITIALIZER { 0 }
75
static void TCMalloc_SlowLock(unsigned* lockword) {
82
} while (!WTF::weakCompareAndSwap(lockword, 0, 1));
90
struct TCMalloc_SpinLock {
91
pthread_mutex_t private_lock_;
94
if (pthread_mutex_init(&private_lock_, NULL) != 0) CRASH();
96
inline void Finalize() {
97
if (pthread_mutex_destroy(&private_lock_) != 0) CRASH();
100
if (pthread_mutex_lock(&private_lock_) != 0) CRASH();
102
inline void Unlock() {
103
if (pthread_mutex_unlock(&private_lock_) != 0) CRASH();
106
if (pthread_mutex_trylock(&private_lock_))
114
#define SPINLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
118
// Corresponding locker object that arranges to acquire a spinlock for
119
// the duration of a C++ scope.
120
class TCMalloc_SpinLockHolder {
122
TCMalloc_SpinLock* lock_;
124
inline explicit TCMalloc_SpinLockHolder(TCMalloc_SpinLock* l)
125
: lock_(l) { l->Lock(); }
126
inline ~TCMalloc_SpinLockHolder() { lock_->Unlock(); }
129
// Short-hands for convenient use by tcmalloc.cc
130
typedef TCMalloc_SpinLock SpinLock;
131
typedef TCMalloc_SpinLockHolder SpinLockHolder;
133
#endif // TCMALLOC_INTERNAL_SPINLOCK_H__