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 Provides FFRT C APIs. 21 * 22 * @since 10 23 */ 24 25 /** 26 * @file mutex.h 27 * 28 * @brief Declares the mutex interfaces in C. 29 * 30 * @library libffrt.z.so 31 * @kit FunctionFlowRuntimeKit 32 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 33 * @since 10 34 * @version 1.0 35 */ 36 37 #ifndef FFRT_API_C_MUTEX_H 38 #define FFRT_API_C_MUTEX_H 39 40 #include "type_def.h" 41 42 /** 43 * @brief Initializes a mutex attribute. 44 * 45 * @param attr Indicates a pointer to the mutex attribute. 46 * @return Returns <b>ffrt_success</b> if the mutex attribute is initialized; 47 returns <b>ffrt_error_inval</b> otherwise. 48 * @since 12 49 * @version 1.0 50 */ 51 FFRT_C_API int ffrt_mutexattr_init(ffrt_mutexattr_t* attr); 52 53 /** 54 * @brief Sets the type of a mutex attribute. 55 * 56 * @param attr Indicates a pointer to the mutex attribute. 57 * @param type Indicates a int to the mutex type. 58 * @return Returns <b>ffrt_success</b> if the mutex attribute type is set successfully; 59 returns <b>ffrt_error_inval</b> if <b>attr</b> is a null pointer or 60 the mutex attribute type is not <b>ffrt_mutex_normal</b> or <b>ffrt_mutex_recursive</b>. 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 Gets the type of a mutex attribute. 68 * 69 * @param attr Indicates a pointer to the mutex attribute. 70 * @param type Indicates a pointer to the mutex type. 71 * @return Returns <b>ffrt_success</b> if the mutex attribute type is get successfully; 72 returns <b>ffrt_error_inval</b> if <b>attr</b> or <b>type</b> is a null pointer. 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 Destroys a mutex attribute, the user needs to invoke this interface. 80 * 81 * @param attr Indicates a pointer to the mutex attribute. 82 * @return Returns <b>ffrt_success</b> if the mutex attribute is destroyed; 83 returns <b>ffrt_error_inval</b> otherwise. 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_success</b> if the mutex is initialized; 95 returns <b>ffrt_error_inval</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_success</b> if the mutex is locked; 106 returns <b>ffrt_error_inval</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_success</b> if the mutex is unlocked; 117 returns <b>ffrt_error_inval</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_success</b> if the mutex is locked; 128 returns <b>ffrt_error_inval</b> or <b>ffrt_error_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, the user needs to invoke this interface. 136 * 137 * @param mutex Indicates a pointer to the mutex. 138 * @return Returns <b>ffrt_success</b> if the mutex is destroyed; 139 returns <b>ffrt_error_inval</b> otherwise. 140 * @since 10 141 * @version 1.0 142 */ 143 FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex); 144 145 #endif // FFRT_API_C_MUTEX_H 146 /** @} */