1 /*
2  * Copyright (c) 2023 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 /**
17  * @addtogroup Ffrt
18  * @{
19  *
20  * @brief ffrt provides APIs.
21  *
22  *
23  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
24  */
25 
26 /**
27  * @file shared_mutex.h
28  *
29  * @brief Declares the rwlock interfaces in C.
30  *
31  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
32  * @version 1.0
33  */
34 
35 #ifndef FFRT_API_C_SHARED_MUTEX_H
36 #define FFRT_API_C_SHARED_MUTEX_H
37 #include "type_def_ext.h"
38 
39 /**
40  * @brief Initializes a rwlock.
41  *
42  * @param rwlock Indicates a pointer to the rwlock.
43  * @param attr Indicates a pointer to the rwlock attribute.
44  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is initialized;
45            returns <b>ffrt_thrd_error</b> otherwise.
46  * @version 1.0
47  */
48 FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr);
49 
50 /**
51  * @brief Locks a write lock.
52  *
53  * @param rwlock Indicates a pointer to the rwlock.
54  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is locked;
55            returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise.
56  * @version 1.0
57  */
58 FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock);
59 
60 /**
61  * @brief Attempts to lock a write lock.
62  *
63  * @param rwlock Indicates a pointer to the rwlock.
64  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is locked;
65            returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise.
66  * @version 1.0
67  */
68 FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock);
69 
70 /**
71  * @brief Locks a read lock.
72  *
73  * @param rwlock Indicates a pointer to the rwlock.
74  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is locked;
75            returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise.
76  * @version 1.0
77  */
78 FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock);
79 
80 /**
81  * @brief Attempts to lock a read lock.
82  *
83  * @param rwlock Indicates a pointer to the rwlock.
84  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is locked;
85            returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise.
86  * @version 1.0
87  */
88 FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock);
89 
90 /**
91  * @brief Unlocks a rwlock.
92  *
93  * @param rwlock Indicates a pointer to the rwlock.
94  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is unlocked;
95            returns <b>ffrt_thrd_error</b> otherwise.
96  * @version 1.0
97  */
98 FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock);
99 
100 /**
101  * @brief Destroys a rwlock.
102  *
103  * @param rwlock Indicates a pointer to the rwlock.
104  * @return Returns <b>ffrt_thrd_success</b> if the rwlock is destroyed;
105            returns <b>ffrt_thrd_error</b> otherwise.
106  * @version 1.0
107  */
108 FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock);
109 
110 #endif
111