Home | History | Annotate | Download | only in wtf
      1 // Copyright (c) 2005, 2006, Google Inc.
      2 // Copyright (c) 2010, Patrick Gansterer <paroga (at) paroga.com>
      3 // All rights reserved.
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      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
     14 // distribution.
     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.
     18 //
     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.
     30 
     31 // ---
     32 // Author: Sanjay Ghemawat <opensource (at) google.com>
     33 
     34 #ifndef TCMALLOC_INTERNAL_SPINLOCK_H__
     35 #define TCMALLOC_INTERNAL_SPINLOCK_H__
     36 
     37 #if OS(UNIX)
     38 #include <sched.h>
     39 #endif
     40 
     41 #include <pthread.h>
     42 
     43 // Portable version
     44 struct TCMalloc_SpinLock {
     45   pthread_mutex_t private_lock_;
     46 
     47   inline void Init() {
     48     if (pthread_mutex_init(&private_lock_, NULL) != 0) CRASH();
     49   }
     50   inline void Finalize() {
     51     if (pthread_mutex_destroy(&private_lock_) != 0) CRASH();
     52   }
     53   inline void Lock() {
     54     if (pthread_mutex_lock(&private_lock_) != 0) CRASH();
     55   }
     56   inline void Unlock() {
     57     if (pthread_mutex_unlock(&private_lock_) != 0) CRASH();
     58   }
     59   bool IsHeld() {
     60     if (pthread_mutex_trylock(&private_lock_))
     61       return true;
     62 
     63     Unlock();
     64     return false;
     65   }
     66 };
     67 
     68 #define SPINLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
     69 
     70 // Corresponding locker object that arranges to acquire a spinlock for
     71 // the duration of a C++ scope.
     72 class TCMalloc_SpinLockHolder {
     73  private:
     74   TCMalloc_SpinLock* lock_;
     75  public:
     76   inline explicit TCMalloc_SpinLockHolder(TCMalloc_SpinLock* l)
     77     : lock_(l) { l->Lock(); }
     78   inline ~TCMalloc_SpinLockHolder() { lock_->Unlock(); }
     79 };
     80 
     81 // Short-hands for convenient use by tcmalloc.cc
     82 typedef TCMalloc_SpinLock SpinLock;
     83 typedef TCMalloc_SpinLockHolder SpinLockHolder;
     84 
     85 #endif  // TCMALLOC_INTERNAL_SPINLOCK_H__
     86