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  * @since 10
26  */
27 
28 /**
29  * @file mutex.h
30  *
31  * @brief Declares the mutex interfaces in C.
32  *
33  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
34  * @since 10
35  * @version 1.0
36  */
37 #ifndef FFRT_API_C_MUTEX_H
38 #define FFRT_API_C_MUTEX_H
39 #include "type_def.h"
40 
41 /**
42  * @brief Initializes mutex attr.
43  *
44  * @param mutex Indicates a pointer to the mutex.
45  * @param attr Indicates a pointer to the mutex attribute.
46  * @return {@link ffrt_success} 0 - success
47  *         {@link ffrt_error_inval} 22 - if attr is null.
48  * @since 12
49  * @version 1.0
50  */
51 FFRT_C_API int ffrt_mutexattr_init(ffrt_mutexattr_t* attr);
52 
53 /**
54  * @brief set mutex type.
55  *
56  * @param attr Indicates a pointer to the mutex attribute.
57  * @param type Indicates a int to the mutex type.
58  * @return {@link ffrt_success} 0 - success.
59  *         {@link ffrt_error_inval} 22 - if attr is null or type is not 0 or 2.
60  * @since 12
61  * @version 1.0
62  */
63 FFRT_C_API int ffrt_mutexattr_settype(ffrt_mutexattr_t* attr, int type);
64 
65 /**
66  * @brief get mutex type.
67  *
68  * @param attr Indicates a pointer to the mutex attribute.
69  * @param type Indicates a pointer to the mutex type.
70  * @return {@link ffrt_success} 0 - success.
71  *         {@link ffrt_error_inval} 22 - if attr is null or type is null.
72  * @since 12
73  * @version 1.0
74  */
75 FFRT_C_API int ffrt_mutexattr_gettype(ffrt_mutexattr_t* attr, int* type);
76 
77 /**
78  * @brief destroy mutex attr, the user needs to invoke this interface.
79  *
80  * @param attr Indicates a pointer to the mutex attribute.
81  * @return {@link ffrt_success} 0 - success.
82  *         {@link ffrt_error_inval} 22 - if attr is null.
83  * @since 12
84  * @version 1.0
85  */
86 FFRT_C_API int ffrt_mutexattr_destroy(ffrt_mutexattr_t* attr);
87 
88 /**
89  * @brief Initializes a mutex.
90  *
91  * @param mutex Indicates a pointer to the mutex.
92  * @param attr Indicates a pointer to the mutex attribute.
93  * @return Returns <b>ffrt_thrd_success</b> if the mutex is initialized;
94            returns <b>ffrt_thrd_error</b> otherwise.
95  * @since 10
96  * @version 1.0
97  */
98 FFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr);
99 
100 /**
101  * @brief Locks a mutex.
102  *
103  * @param mutex Indicates a pointer to the mutex.
104  * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked;
105            returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise.
106  * @since 10
107  * @version 1.0
108  */
109 FFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex);
110 
111 /**
112  * @brief Unlocks a mutex.
113  *
114  * @param mutex Indicates a pointer to the mutex.
115  * @return Returns <b>ffrt_thrd_success</b> if the mutex is unlocked;
116            returns <b>ffrt_thrd_error</b> otherwise.
117  * @since 10
118  * @version 1.0
119  */
120 FFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex);
121 
122 /**
123  * @brief Attempts to lock a mutex.
124  *
125  * @param mutex Indicates a pointer to the mutex.
126  * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked;
127            returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise.
128  * @since 10
129  * @version 1.0
130  */
131 FFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex);
132 
133 /**
134  * @brief Destroys a mutex.
135  *
136  * @param mutex Indicates a pointer to the mutex.
137  * @return Returns <b>ffrt_thrd_success</b> if the mutex is destroyed;
138            returns <b>ffrt_thrd_error</b> otherwise.
139  * @since 10
140  * @version 1.0
141  */
142 FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex);
143 #endif
144