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