OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_MUTEX_H_ |
| 29 #define V8_MUTEX_H_ |
| 30 |
| 31 #include "globals.h" |
| 32 |
| 33 #if V8_OS_UNIX |
| 34 #include <pthread.h> // NOLINT |
| 35 #elif V8_OS_WIN32 |
| 36 #include "win32-headers.h" |
| 37 #endif |
| 38 |
| 39 #include "checks.h" |
| 40 #include "lazy-instance.h" |
| 41 |
| 42 namespace v8 { |
| 43 namespace internal { |
| 44 |
| 45 // ---------------------------------------------------------------------------- |
| 46 // Mutex |
| 47 // |
| 48 // Mutexes are used for serializing access to non-reentrant sections of code. |
| 49 // The implementations of mutex should allow for nested/recursive locking. |
| 50 // |
| 51 class Mutex { |
| 52 public: |
| 53 Mutex(); |
| 54 ~Mutex(); |
| 55 |
| 56 // Locks the given mutex. If the mutex is currently unlocked, it becomes |
| 57 // locked and owned by the calling thread. If the mutex is already locked |
| 58 // by another thread, suspends the calling thread until the mutex is |
| 59 // unlocked. |
| 60 void Lock(); |
| 61 |
| 62 // Unlocks the given mutex. The mutex is assumed to be locked and owned |
| 63 // by the calling thread on entrance. |
| 64 void Unlock(); |
| 65 |
| 66 // Tries to lock the given mutex. Returns true if the mutex was locked |
| 67 // successfully. |
| 68 bool TryLock(); |
| 69 |
| 70 private: |
| 71 #if V8_OS_UNIX |
| 72 pthread_mutex_t mutex_; |
| 73 #elif V8_OS_WIN32 |
| 74 CRITICAL_SECTION cs_; |
| 75 #endif |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 78 }; |
| 79 |
| 80 |
| 81 // ---------------------------------------------------------------------------- |
| 82 // LazyMutex |
| 83 // |
| 84 // POD Mutex initialized lazily (i.e. the first time Pointer() is called). |
| 85 // Usage: |
| 86 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; |
| 87 // |
| 88 // void my_function() { |
| 89 // ScopedLock my_lock(&my_mutex); |
| 90 // // Do something. |
| 91 // } |
| 92 // |
| 93 typedef LazyDynamicInstance<Mutex, |
| 94 DefaultCreateTrait<Mutex>, |
| 95 ThreadSafeInitOnceTrait>::type LazyMutex; |
| 96 |
| 97 #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
| 98 |
| 99 |
| 100 // ---------------------------------------------------------------------------- |
| 101 // ScopedLock |
| 102 // |
| 103 // Stack-allocated ScopedLocks provide block-scoped locking and |
| 104 // unlocking of a mutex. |
| 105 // |
| 106 class ScopedLock { |
| 107 public: |
| 108 explicit ScopedLock(Mutex* mutex): mutex_(mutex) { |
| 109 ASSERT(mutex_ != NULL); |
| 110 mutex_->Lock(); |
| 111 } |
| 112 |
| 113 explicit ScopedLock(LazyMutex* lazy_mutex) : mutex_(lazy_mutex->Pointer()) { |
| 114 ASSERT(mutex_ != NULL); |
| 115 mutex_->Lock(); |
| 116 } |
| 117 |
| 118 ~ScopedLock() { |
| 119 mutex_->Unlock(); |
| 120 } |
| 121 |
| 122 private: |
| 123 Mutex* mutex_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(ScopedLock); |
| 126 }; |
| 127 |
| 128 } } // namespace v8::internal |
| 129 |
| 130 #endif // V8_MUTEX_H_ |
OLD | NEW |