• 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 <unistd.h>
18 #include "functionalext.h"
19 
20 pthread_spinlock_t lock = 0;
21 int count = 0;
22 
thread1(void)23 static void thread1(void)
24 {
25     int ret = pthread_spin_trylock(&lock);
26     EXPECT_EQ("pthread_spin_trylock_0100", ret, CMPFLAG);
27     ++count;
28     ret = pthread_spin_unlock(&lock);
29     pthread_exit("success");
30 }
31 
thread2(void)32 static void thread2(void)
33 {
34     int ret = pthread_spin_lock(&lock);
35     EXPECT_EQ("pthread_spin_trylock_0200", ret, CMPFLAG);
36 
37     ret = pthread_spin_trylock(&lock);
38     EXPECT_EQ("pthread_spin_trylock_0200", ret, EBUSY);
39     ret = pthread_spin_unlock(&lock);
40     pthread_exit("success");
41 }
42 
43 /**
44  * @tc.name      : pthread_spin_trylock_0100
45  * @tc.desc      : Verify that the attempt to lock is successful under multi-threading(success)
46  * @tc.level     : Level 0
47  */
pthread_spin_trylock_0100(void)48 void pthread_spin_trylock_0100(void)
49 {
50     pthread_t thread;
51     void *rev;
52 
53     int ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
54 
55     EXPECT_EQ("pthread_spin_trylock_0100", ret, CMPFLAG);
56     ret = pthread_create(&thread, NULL, (void *)thread1, NULL);
57     if (ret != 0) {
58         EXPECT_EQ("pthread_spin_trylock_0100", ret, CMPFLAG);
59         return;
60     }
61 
62     ret = pthread_spin_lock(&lock);
63     EXPECT_EQ("pthread_spin_trylock_0100", ret, CMPFLAG);
64 
65     ++count;
66     ret = pthread_spin_unlock(&lock);
67 
68     pthread_join(thread, &rev);
69     EXPECT_STREQ("pthread_spin_trylock_0100", rev, "success");
70     EXPECT_EQ("pthread_spin_trylock_0100", count, 2);
71 }
72 
73 /**
74  * @tc.name      : pthread_spin_trylock_0200
75  * @tc.desc      : Verify that the attempt to lock is successful under multi-threading(failed)
76  * @tc.level     : Level 2
77  */
pthread_spin_trylock_0200(void)78 void pthread_spin_trylock_0200(void)
79 {
80     pthread_t thread;
81     void *rev;
82 
83     int ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
84 
85     EXPECT_EQ("pthread_spin_trylock_0200", ret, CMPFLAG);
86     ret = pthread_create(&thread, NULL, (void *)thread2, NULL);
87     if (ret != 0) {
88         EXPECT_EQ("pthread_spin_trylock_0200", ret, CMPFLAG);
89         return;
90     }
91 
92     pthread_join(thread, &rev);
93     pthread_spin_destroy(&lock);
94 }
95 
main(void)96 int main(void)
97 {
98     pthread_spin_trylock_0100();
99     pthread_spin_trylock_0200();
100 
101     return t_status;
102 }