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 <limits.h>
17 #include <pthread.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21
22 static pthread_mutex_t g_mutex;
23 static int32_t g_count = 0;
24 static int32_t g_run_max_count = 10000;
25
threadfuncA(void * arg)26 void *threadfuncA(void *arg)
27 {
28 int32_t ret = pthread_mutex_lock(&g_mutex);
29 EXPECT_EQ("pthread_mutex_lock_0100", ret, 0);
30 pthread_mutex_unlock(&g_mutex);
31 return arg;
32 }
33
threadfuncB(void * arg)34 void *threadfuncB(void *arg)
35 {
36 int32_t ret = pthread_mutex_lock(&g_mutex);
37 ret = pthread_mutex_lock(&g_mutex);
38 EXPECT_EQ("pthread_mutex_lock_0200", ret, 0);
39 pthread_mutex_unlock(&g_mutex);
40 return arg;
41 }
42
threadfuncC(void * arg)43 void *threadfuncC(void *arg)
44 {
45 g_count++;
46 int32_t ret = pthread_mutex_lock(&g_mutex);
47 if (g_count == INT_MAX) {
48 EXPECT_EQ("pthread_mutex_lock_0300", ret, EAGAIN);
49 }
50 pthread_mutex_unlock(&g_mutex);
51 return arg;
52 }
53
threadfuncD(void * arg)54 void *threadfuncD(void *arg)
55 {
56 int32_t ret = pthread_mutex_lock(&g_mutex);
57 ret = pthread_mutex_lock(&g_mutex);
58 EXPECT_EQ("pthread_mutex_lock_0400", ret, EDEADLK);
59 pthread_mutex_unlock(&g_mutex);
60 return arg;
61 }
62
63 /**
64 * @tc.name: pthread_mutex_lock_0100
65 * @tc.desc: Verify process pthread_mutex_lock once success when pthread_mutex_init second args is null.
66 * @tc.level: level 0
67 */
pthread_mutex_lock_0100(void)68 void pthread_mutex_lock_0100(void)
69 {
70 pthread_mutex_init(&g_mutex, NULL);
71 pthread_t tid1;
72 int32_t ret = pthread_create(&tid1, NULL, threadfuncA, NULL);
73 pthread_join(tid1, NULL);
74 }
75
76 /**
77 * @tc.name: pthread_mutex_lock_0200
78 * @tc.desc: Verify process pthread_mutex_lock twice success when pthread_mutex_init second args is
79 * PTHREAD_MUTEX_NORMAL.
80 * @tc.level: level 1
81 */
pthread_mutex_lock_0200(void)82 void pthread_mutex_lock_0200(void)
83 {
84 int32_t type = PTHREAD_MUTEX_RECURSIVE;
85 pthread_mutex_init(&g_mutex, (pthread_mutexattr_t *)(&type));
86 pthread_t tid2;
87 pthread_create(&tid2, NULL, threadfuncB, NULL);
88 pthread_join(tid2, NULL);
89 }
90
91 /**
92 * @tc.name: pthread_mutex_lock_0300
93 * @tc.desc: Verify process pthread_mutex_lock once success when pthread_mutex_init second args is
94 * PTHREAD_MUTEX_RECURSIVE.
95 * @tc.level: level 2
96 */
pthread_mutex_lock_0300(void)97 void pthread_mutex_lock_0300(void)
98 {
99 int32_t type = PTHREAD_MUTEX_RECURSIVE;
100 pthread_mutex_init(&g_mutex, (pthread_mutexattr_t *)(&type));
101 pthread_t tid3;
102 for (int32_t i = 0; i <= g_run_max_count; i++) {
103 pthread_create(&tid3, NULL, threadfuncC, NULL);
104 pthread_join(tid3, NULL);
105 }
106 }
107
108 /**
109 * @tc.name: pthread_mutex_lock_0400
110 * @tc.desc: Verify process pthread_mutex_lock twice success when pthread_mutex_init second args is
111 * PTHREAD_MUTEX_RECURSIVE.
112 * @tc.level: level 2
113 */
pthread_mutex_lock_0400(void)114 void pthread_mutex_lock_0400(void)
115 {
116 int32_t type = PTHREAD_MUTEX_ERRORCHECK;
117 pthread_mutex_init(&g_mutex, (pthread_mutexattr_t *)(&type));
118 pthread_t tid4;
119 pthread_create(&tid4, NULL, threadfuncD, NULL);
120 pthread_join(tid4, NULL);
121 }
122
main(void)123 int main(void)
124 {
125 pthread_mutex_lock_0100();
126 pthread_mutex_lock_0200();
127 pthread_mutex_lock_0300();
128 pthread_mutex_lock_0400();
129 return t_status;
130 }