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