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