• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <pthread.h>
18 #include <time.h>
19 #include "test.h"
20 
21 #define TEST(c, ...) ((c) || (t_error(#c " failed: " __VA_ARGS__), 0))
22 #define SLEEP_20_MS (20000)
23 
24 pthread_rwlock_t g_rwlock1;
25 static int number = 0;
26 
pthreadRdlockR1(void * arg)27 static void *pthreadRdlockR1(void *arg)
28 {
29     TEST(pthread_rwlock_rdlock(&g_rwlock1) == 0);
30     number++;
31     TEST(pthread_rwlock_unlock(&g_rwlock1) == 0);
32     return arg;
33 }
34 
pthreadRdlockR2(void * arg)35 static void *pthreadRdlockR2(void *arg)
36 {
37     usleep(SLEEP_20_MS);
38     TEST(pthread_rwlock_rdlock(&g_rwlock1) == 0);
39     number--;
40     TEST(pthread_rwlock_unlock(&g_rwlock1) == 0);
41     return arg;
42 }
43 
44 /**
45  * @tc.name      : pthread_rwlock_rdlock_0100
46  * @tc.desc      : Verify read lock read-write lock success
47  * @tc.level     : Level 0
48  */
pthread_rwlock_rdlock_0100(void)49 void pthread_rwlock_rdlock_0100(void)
50 {
51     pthread_t thread;
52     TEST(pthread_rwlock_init(&g_rwlock1, NULL) == 0);
53     TEST(pthread_rwlock_rdlock(&g_rwlock1) == 0);
54     TEST(pthread_rwlock_unlock(&g_rwlock1) == 0);
55     TEST(pthread_rwlock_destroy(&g_rwlock1) == 0);
56 }
57 
58 /**
59  * @tc.name      : pthread_rwlock_rdlock_0200
60  * @tc.desc      : Verify that the read lock is successful for multiple read-write locks
61  * @tc.level     : Level 1
62  */
pthread_rwlock_rdlock_0200(void)63 void pthread_rwlock_rdlock_0200(void)
64 {
65     pthread_t thread;
66     TEST(pthread_rwlock_init(&g_rwlock1, NULL) == 0);
67     TEST(pthread_rwlock_rdlock(&g_rwlock1) == 0);
68     TEST(pthread_rwlock_rdlock(&g_rwlock1) == 0);
69     TEST(pthread_rwlock_unlock(&g_rwlock1) == 0);
70     TEST(pthread_rwlock_unlock(&g_rwlock1) == 0);
71     TEST(pthread_rwlock_destroy(&g_rwlock1) == 0);
72 }
73 
74 /**
75  * @tc.name      : pthread_rwlock_rdlock_0300
76  * @tc.desc      : Verify that the read lock read-write lock is successfully set multiple times
77  * @tc.level     : Level 1
78  */
pthread_rwlock_rdlock_0300(void)79 void pthread_rwlock_rdlock_0300(void)
80 {
81     pthread_t tid[2];
82     TEST(pthread_rwlock_init(&g_rwlock1, NULL) == 0);
83 
84     TEST(pthread_create(&tid[0], NULL, pthreadRdlockR1, NULL) == 0);
85     TEST(pthread_create(&tid[1], NULL, pthreadRdlockR2, NULL) == 0);
86     TEST(pthread_join(tid[0], NULL) == 0);
87     TEST(pthread_join(tid[1], NULL) == 0);
88     TEST(number == 0);
89     TEST(pthread_rwlock_destroy(&g_rwlock1) == 0);
90 }
91 
main(int argc,char * argv[])92 int main(int argc, char *argv[])
93 {
94     pthread_rwlock_rdlock_0100();
95     pthread_rwlock_rdlock_0200();
96     pthread_rwlock_rdlock_0300();
97     return t_status;
98 }