• 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 <pthread.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include "functionalext.h"
20 
21 static pthread_mutex_t g_mutex;
22 
threadfuncA(void * arg)23 void *threadfuncA(void *arg)
24 {
25     int32_t ret = pthread_mutex_lock(&g_mutex);
26     ret = pthread_mutex_unlock(&g_mutex);
27     EXPECT_EQ("pthread_mutex_un_lock_0100", ret, 0);
28     return arg;
29 }
30 
threadfuncB(void * arg)31 void *threadfuncB(void *arg)
32 {
33     pthread_mutex_lock(&g_mutex);
34     return arg;
35 }
36 
threadfuncC(void * arg)37 void *threadfuncC(void *arg)
38 {
39     int32_t ret = pthread_mutex_unlock(&g_mutex);
40     EXPECT_EQ("pthread_mutex_un_lock_0200", ret, EPERM);
41     return arg;
42 }
43 
44 /**
45  * @tc.name:      pthread_mutex_un_lock_0100
46  * @tc.desc:      Verify process pthread_mutex_un_lock once success.
47  * @tc.level:     level 0.
48  */
pthread_mutex_un_lock_0100(void)49 void pthread_mutex_un_lock_0100(void)
50 {
51     int32_t type = PTHREAD_MUTEX_RECURSIVE;
52     pthread_mutex_init(&g_mutex, (pthread_mutexattr_t *)(&type));
53     pthread_t tid1;
54     pthread_create(&tid1, NULL, threadfuncA, NULL);
55     pthread_join(tid1, NULL);
56 }
57 
58 /**
59  * @tc.name:      pthread_mutex_un_lock_0200
60  * @tc.desc:      Verify process pthread_mutex_un_lock fail. Because can not release a mutex pointed to a mutex
61  *                that is not this thread.
62  * @tc.level:     level 2.
63  */
pthread_mutex_un_lock_0200(void)64 void pthread_mutex_un_lock_0200(void)
65 {
66     int32_t type = PTHREAD_MUTEX_RECURSIVE;
67     pthread_mutex_init(&g_mutex, (pthread_mutexattr_t *)(&type));
68     pthread_t tid2, tid3;
69     pthread_create(&tid2, NULL, threadfuncB, NULL);
70     pthread_join(tid2, NULL);
71     pthread_create(&tid3, NULL, threadfuncC, NULL);
72     pthread_join(tid3, NULL);
73     pthread_mutex_destroy(&g_mutex);
74 }
75 
main(void)76 int main(void)
77 {
78     pthread_mutex_un_lock_0100();
79     pthread_mutex_un_lock_0200();
80     return t_status;
81 }