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 <unistd.h>
18 #include "functionalext.h"
19
20 pthread_spinlock_t lock = 0;
21 volatile int count = 0;
22
thread1(void)23 static void thread1(void)
24 {
25 int ret = pthread_spin_lock(&lock);
26 EXPECT_EQ("pthread_spin_lock_0100", ret, CMPFLAG);
27 ++count;
28 ret = pthread_spin_unlock(&lock);
29 pthread_exit("success");
30 }
31
32 /**
33 * @tc.name : pthread_spin_lock_0100
34 * @tc.desc : Verify that the return value of the calling function is correct
35 * @tc.level : Level 0
36 */
pthread_spin_lock_0100(void)37 void pthread_spin_lock_0100(void)
38 {
39 pthread_spinlock_t spin;
40
41 int ret = pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE);
42 EXPECT_EQ("pthread_spin_lock_0100", ret, CMPFLAG);
43
44 ret = pthread_spin_lock(&spin);
45 EXPECT_EQ("pthread_spin_lock_0100", ret, CMPFLAG);
46
47 ret = pthread_spin_unlock(&spin);
48 EXPECT_EQ("pthread_spin_lock_0100", ret, CMPFLAG);
49
50 pthread_spin_destroy(&spin);
51 }
52
53 /**
54 * @tc.name : pthread_spin_lock_0200
55 * @tc.desc : Verify that under multi-threading, whether the modification of the global variable data is
56 * as expected, and whether the spin lock is successfully locked
57 * @tc.level : Level 0
58 */
pthread_spin_lock_0200(void)59 void pthread_spin_lock_0200(void)
60 {
61 pthread_t thread;
62 void *rev;
63
64 int ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
65
66 EXPECT_EQ("pthread_spin_lock_0200", ret, CMPFLAG);
67 ret = pthread_create(&thread, NULL, (void *)thread1, NULL);
68 if (ret != 0) {
69 EXPECT_EQ("pthread_spin_lock_0200", ret, CMPFLAG);
70 return;
71 }
72
73 ret = pthread_spin_lock(&lock);
74 EXPECT_EQ("pthread_spin_lock_0200", ret, CMPFLAG);
75
76 ++count;
77 ret = pthread_spin_unlock(&lock);
78
79 pthread_join(thread, &rev);
80 EXPECT_STREQ("pthread_spin_lock_0200", rev, "success");
81 EXPECT_EQ("pthread_spin_lock_0200", count, 2);
82
83 pthread_spin_destroy(&lock);
84 }
85
main(void)86 int main(void)
87 {
88 pthread_spin_lock_0100();
89 pthread_spin_lock_0200();
90
91 return t_status;
92 }