Home | History | Annotate | Download | only in platform
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/base/platform/mutex.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace v8 {
     10 namespace base {
     11 
     12 TEST(Mutex, LockGuardMutex) {
     13   Mutex mutex;
     14   { LockGuard<Mutex> lock_guard(&mutex); }
     15   { LockGuard<Mutex> lock_guard(&mutex); }
     16 }
     17 
     18 
     19 TEST(Mutex, LockGuardRecursiveMutex) {
     20   RecursiveMutex recursive_mutex;
     21   { LockGuard<RecursiveMutex> lock_guard(&recursive_mutex); }
     22   {
     23     LockGuard<RecursiveMutex> lock_guard1(&recursive_mutex);
     24     LockGuard<RecursiveMutex> lock_guard2(&recursive_mutex);
     25   }
     26 }
     27 
     28 
     29 TEST(Mutex, LockGuardLazyMutex) {
     30   LazyMutex lazy_mutex = LAZY_MUTEX_INITIALIZER;
     31   { LockGuard<Mutex> lock_guard(lazy_mutex.Pointer()); }
     32   { LockGuard<Mutex> lock_guard(lazy_mutex.Pointer()); }
     33 }
     34 
     35 
     36 TEST(Mutex, LockGuardLazyRecursiveMutex) {
     37   LazyRecursiveMutex lazy_recursive_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER;
     38   { LockGuard<RecursiveMutex> lock_guard(lazy_recursive_mutex.Pointer()); }
     39   {
     40     LockGuard<RecursiveMutex> lock_guard1(lazy_recursive_mutex.Pointer());
     41     LockGuard<RecursiveMutex> lock_guard2(lazy_recursive_mutex.Pointer());
     42   }
     43 }
     44 
     45 
     46 TEST(Mutex, MultipleMutexes) {
     47   Mutex mutex1;
     48   Mutex mutex2;
     49   Mutex mutex3;
     50   // Order 1
     51   mutex1.Lock();
     52   mutex2.Lock();
     53   mutex3.Lock();
     54   mutex1.Unlock();
     55   mutex2.Unlock();
     56   mutex3.Unlock();
     57   // Order 2
     58   mutex1.Lock();
     59   mutex2.Lock();
     60   mutex3.Lock();
     61   mutex3.Unlock();
     62   mutex2.Unlock();
     63   mutex1.Unlock();
     64 }
     65 
     66 
     67 TEST(Mutex, MultipleRecursiveMutexes) {
     68   RecursiveMutex recursive_mutex1;
     69   RecursiveMutex recursive_mutex2;
     70   // Order 1
     71   recursive_mutex1.Lock();
     72   recursive_mutex2.Lock();
     73   EXPECT_TRUE(recursive_mutex1.TryLock());
     74   EXPECT_TRUE(recursive_mutex2.TryLock());
     75   recursive_mutex1.Unlock();
     76   recursive_mutex1.Unlock();
     77   recursive_mutex2.Unlock();
     78   recursive_mutex2.Unlock();
     79   // Order 2
     80   recursive_mutex1.Lock();
     81   EXPECT_TRUE(recursive_mutex1.TryLock());
     82   recursive_mutex2.Lock();
     83   EXPECT_TRUE(recursive_mutex2.TryLock());
     84   recursive_mutex2.Unlock();
     85   recursive_mutex1.Unlock();
     86   recursive_mutex2.Unlock();
     87   recursive_mutex1.Unlock();
     88 }
     89 
     90 }  // namespace base
     91 }  // namespace v8
     92