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