• 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_lock(&lock);
26     EXPECT_EQ("pthread_spin_unlock_0100", ret, CMPFLAG);
27     ++count;
28     ret = pthread_spin_unlock(&lock);
29     EXPECT_EQ("pthread_spin_unlock_0100", ret, CMPFLAG);
30     EXPECT_EQ("pthread_spin_unlock_0100", count, 1);
31     pthread_exit("success");
32 }
33 
34 /**
35  * @tc.name      : pthread_spin_unlock_0100
36  * @tc.desc      : Verify that the spin lock releases the lock successfully under multi-threading
37  * @tc.level     : Level 0
38  */
pthread_spin_unlock_0100(void)39 void pthread_spin_unlock_0100(void)
40 {
41     pthread_t thread;
42     void *rev;
43 
44     int ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
45 
46     EXPECT_EQ("pthread_spin_unlock_0100", ret, CMPFLAG);
47     ret = pthread_create(&thread, NULL, (void *)thread1, NULL);
48     if (ret != 0) {
49         EXPECT_EQ("pthread_spin_unlock_0100", ret, CMPFLAG);
50         pthread_spin_destroy(&lock);
51         return;
52     }
53 
54     pthread_join(thread, &rev);
55     EXPECT_STREQ("pthread_spin_unlock_0100", rev, "success");
56     EXPECT_EQ("pthread_spin_unlock_0100", count, 1);
57 
58     pthread_spin_destroy(&lock);
59 }
60 
main(void)61 int main(void)
62 {
63     pthread_spin_unlock_0100();
64     return t_status;
65 }