• 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 "functionalext.h"
18 
19 /**
20  * @tc.name      : pthread_rwlock_trywrlock_0100
21  * @tc.desc      : Try adding a write lock
22  * @tc.level     : Level 0
23  */
pthread_rwlock_trywrlock_0100(void)24 void pthread_rwlock_trywrlock_0100(void)
25 {
26     pthread_rwlock_t rwlock;
27     pthread_rwlockattr_t rwlockattr;
28     pthread_rwlockattr_init(&rwlockattr);
29     pthread_rwlock_init(&rwlock, &rwlockattr);
30 
31     int ret = pthread_rwlock_trywrlock(&rwlock);
32     EXPECT_EQ("pthread_rwlock_trywrlock_0100", ret, CMPFLAG);
33     ret = pthread_rwlock_unlock(&rwlock);
34     EXPECT_EQ("pthread_rwlock_trywrlock_0100", ret, CMPFLAG);
35 
36     pthread_rwlock_destroy(&rwlock);
37     pthread_rwlockattr_destroy(&rwlockattr);
38 }
39 
40 /**
41  * @tc.name      : pthread_rwlock_trywrlock_0200
42  * @tc.desc      : If it is already locked, try adding a write lock
43  * @tc.level     : Level 2
44  */
pthread_rwlock_trywrlock_0200(void)45 void pthread_rwlock_trywrlock_0200(void)
46 {
47     pthread_rwlock_t rwlock;
48     pthread_rwlockattr_t rwlockattr;
49     pthread_rwlockattr_init(&rwlockattr);
50     pthread_rwlock_init(&rwlock, &rwlockattr);
51 
52     int ret = pthread_rwlock_wrlock(&rwlock);
53     EXPECT_EQ("pthread_rwlock_trywrlock_0100", ret, CMPFLAG);
54 
55     ret = pthread_rwlock_trywrlock(&rwlock);
56     EXPECT_EQ("pthread_rwlock_trywrlock_0100", ret, EBUSY);
57 
58     ret = pthread_rwlock_unlock(&rwlock);
59     EXPECT_EQ("pthread_rwlock_trywrlock_0100", ret, CMPFLAG);
60 
61     pthread_rwlock_destroy(&rwlock);
62     pthread_rwlockattr_destroy(&rwlockattr);
63 }
64 
main(void)65 int main(void)
66 {
67     pthread_rwlock_trywrlock_0100();
68     pthread_rwlock_trywrlock_0200();
69     return t_status;
70 }