TestLibPthreadRWLocks.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2021, Rodrigo Tobar <rtobarc@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPthread/pthread.h>
  7. #include <LibTest/TestCase.h>
  8. TEST_CASE(rwlock_init)
  9. {
  10. pthread_rwlock_t lock;
  11. auto result = pthread_rwlock_init(&lock, nullptr);
  12. EXPECT_EQ(0, result);
  13. }
  14. TEST_CASE(rwlock_rdlock)
  15. {
  16. pthread_rwlock_t lock;
  17. auto result = pthread_rwlock_init(&lock, nullptr);
  18. EXPECT_EQ(0, result);
  19. result = pthread_rwlock_rdlock(&lock);
  20. EXPECT_EQ(0, result);
  21. result = pthread_rwlock_unlock(&lock);
  22. EXPECT_EQ(0, result);
  23. result = pthread_rwlock_rdlock(&lock);
  24. EXPECT_EQ(0, result);
  25. result = pthread_rwlock_rdlock(&lock);
  26. EXPECT_EQ(0, result);
  27. result = pthread_rwlock_unlock(&lock);
  28. EXPECT_EQ(0, result);
  29. result = pthread_rwlock_unlock(&lock);
  30. EXPECT_EQ(0, result);
  31. }
  32. TEST_CASE(rwlock_wrlock)
  33. {
  34. pthread_rwlock_t lock;
  35. auto result = pthread_rwlock_init(&lock, nullptr);
  36. EXPECT_EQ(0, result);
  37. result = pthread_rwlock_wrlock(&lock);
  38. EXPECT_EQ(0, result);
  39. result = pthread_rwlock_unlock(&lock);
  40. EXPECT_EQ(0, result);
  41. }
  42. TEST_CASE(rwlock_rwr_sequence)
  43. {
  44. pthread_rwlock_t lock;
  45. auto result = pthread_rwlock_init(&lock, nullptr);
  46. EXPECT_EQ(0, result);
  47. result = pthread_rwlock_rdlock(&lock);
  48. EXPECT_EQ(0, result);
  49. result = pthread_rwlock_unlock(&lock);
  50. EXPECT_EQ(0, result);
  51. result = pthread_rwlock_wrlock(&lock);
  52. EXPECT_EQ(0, result);
  53. result = pthread_rwlock_unlock(&lock);
  54. EXPECT_EQ(0, result);
  55. result = pthread_rwlock_rdlock(&lock);
  56. EXPECT_EQ(0, result);
  57. result = pthread_rwlock_unlock(&lock);
  58. EXPECT_EQ(0, result);
  59. }
  60. TEST_CASE(rwlock_wrlock_init_in_once)
  61. {
  62. static pthread_rwlock_t lock;
  63. static pthread_once_t once1 = PTHREAD_ONCE_INIT;
  64. static pthread_once_t once2 = PTHREAD_ONCE_INIT;
  65. static pthread_once_t once3 = PTHREAD_ONCE_INIT;
  66. pthread_once(&once1, []() {
  67. pthread_once(&once2, []() {
  68. pthread_once(&once3, []() {
  69. auto result = pthread_rwlock_init(&lock, nullptr);
  70. EXPECT_EQ(0, result);
  71. });
  72. });
  73. });
  74. auto result = pthread_rwlock_wrlock(&lock);
  75. EXPECT_EQ(0, result);
  76. result = pthread_rwlock_unlock(&lock);
  77. EXPECT_EQ(0, result);
  78. }