scoped_lock_unittest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include "butil/scoped_lock.h"
  19. #include <errno.h>
  20. namespace {
  21. class ScopedLockTest : public ::testing::Test{
  22. protected:
  23. ScopedLockTest(){
  24. };
  25. virtual ~ScopedLockTest(){};
  26. virtual void SetUp() {
  27. };
  28. virtual void TearDown() {
  29. };
  30. };
  31. TEST_F(ScopedLockTest, mutex) {
  32. pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  33. {
  34. BAIDU_SCOPED_LOCK(m1);
  35. ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m1));
  36. }
  37. ASSERT_EQ(0, pthread_mutex_trylock(&m1));
  38. pthread_mutex_unlock(&m1);
  39. }
  40. TEST_F(ScopedLockTest, spinlock) {
  41. pthread_spinlock_t s1;
  42. pthread_spin_init(&s1, 0);
  43. {
  44. BAIDU_SCOPED_LOCK(s1);
  45. ASSERT_EQ(EBUSY, pthread_spin_trylock(&s1));
  46. }
  47. ASSERT_EQ(0, pthread_spin_lock(&s1));
  48. pthread_spin_unlock(&s1);
  49. pthread_spin_destroy(&s1);
  50. }
  51. TEST_F(ScopedLockTest, unique_lock_mutex) {
  52. pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  53. {
  54. std::unique_lock<pthread_mutex_t> lck(m1);
  55. ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m1));
  56. lck.unlock();
  57. {
  58. std::unique_lock<pthread_mutex_t> lck2(m1, std::try_to_lock);
  59. ASSERT_TRUE(lck2.owns_lock());
  60. }
  61. ASSERT_TRUE(lck.try_lock());
  62. ASSERT_TRUE(lck.owns_lock());
  63. std::unique_lock<pthread_mutex_t> lck2(m1, std::defer_lock);
  64. ASSERT_FALSE(lck2.owns_lock());
  65. std::unique_lock<pthread_mutex_t> lck3(m1, std::try_to_lock);
  66. ASSERT_FALSE(lck3.owns_lock());
  67. }
  68. {
  69. BAIDU_SCOPED_LOCK(m1);
  70. ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m1));
  71. }
  72. ASSERT_EQ(0, pthread_mutex_trylock(&m1));
  73. {
  74. std::unique_lock<pthread_mutex_t> lck(m1, std::adopt_lock);
  75. ASSERT_TRUE(lck.owns_lock());
  76. }
  77. std::unique_lock<pthread_mutex_t> lck(m1, std::try_to_lock);
  78. ASSERT_TRUE(lck.owns_lock());
  79. }
  80. TEST_F(ScopedLockTest, unique_lock_spin) {
  81. pthread_spinlock_t s1;
  82. pthread_spin_init(&s1, 0);
  83. {
  84. std::unique_lock<pthread_spinlock_t> lck(s1);
  85. ASSERT_EQ(EBUSY, pthread_spin_trylock(&s1));
  86. lck.unlock();
  87. ASSERT_TRUE(lck.try_lock());
  88. }
  89. {
  90. BAIDU_SCOPED_LOCK(s1);
  91. ASSERT_EQ(EBUSY, pthread_spin_trylock(&s1));
  92. }
  93. ASSERT_EQ(0, pthread_spin_lock(&s1));
  94. pthread_spin_unlock(&s1);
  95. pthread_spin_destroy(&s1);
  96. }
  97. }