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 a mutex. 43 * 44 * @param mutex Indicates a pointer to the mutex. 45 * @param attr Indicates a pointer to the mutex attribute. 46 * @return Returns <b>ffrt_thrd_success</b> if the mutex is initialized; 47 returns <b>ffrt_thrd_error</b> otherwise. 48 * @since 10 49 * @version 1.0 50 */ 51 FFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr); 52 53 /** 54 * @brief Locks a mutex. 55 * 56 * @param mutex Indicates a pointer to the mutex. 57 * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked; 58 returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise. 59 * @since 10 60 * @version 1.0 61 */ 62 FFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex); 63 64 /** 65 * @brief Unlocks a mutex. 66 * 67 * @param mutex Indicates a pointer to the mutex. 68 * @return Returns <b>ffrt_thrd_success</b> if the mutex is unlocked; 69 returns <b>ffrt_thrd_error</b> otherwise. 70 * @since 10 71 * @version 1.0 72 */ 73 FFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex); 74 75 /** 76 * @brief Attempts to lock a mutex. 77 * 78 * @param mutex Indicates a pointer to the mutex. 79 * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked; 80 returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise. 81 * @since 10 82 * @version 1.0 83 */ 84 FFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex); 85 86 /** 87 * @brief Destroys a mutex. 88 * 89 * @param mutex Indicates a pointer to the mutex. 90 * @return Returns <b>ffrt_thrd_success</b> if the mutex is destroyed; 91 returns <b>ffrt_thrd_error</b> otherwise. 92 * @since 10 93 * @version 1.0 94 */ 95 FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex); 96 97 /** 98 * @param mutex Indicates a pointer to the mutex 99 * @param attr Indicates a pointer to the mutex attribute 100 * @return Returns <b>ffrt_thrd_success</b> if the mutex is initialized 101 * return <b>ffrt_thrd_error</b>otherwise 102 * @since 10 103 * @version 1.0 104 */ 105 FFRT_C_API int ffrt_recursive_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr); 106 107 /** 108 * @brief Locks a recursive mutex 109 * @param mutex Indicates a pointer to the mutex 110 * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked 111 * returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise. 112 * @since 10 113 * @version 1.0 114 */ 115 116 FFRT_C_API int ffrt_recursive_mutex_lock(ffrt_mutex_t* mutex); 117 118 /** 119 * @brief Unlocks a recursive mutex. 120 * @param mutex Indicates a pointer to the mutex. 121 * @return Returns <b>ffrt_thrd_success</b> if the mutex is unlocked. 122 * returns <b>ffrt_thrd_error</b>otherwise. 123 * @since 10 124 * @version 1.0 125 */ 126 FFRT_C_API int ffrt_recursive_mutex_unlock(ffrt_mutex_t* mutex); 127 128 /** 129 * @brief Attempts to lock a recursive mutex. 130 * @param mutex Indicates a pointer to the mutex. 131 * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked. 132 * returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise. 133 * @since 10 134 * @version 1.0 135 */ 136 FFRT_C_API int ffrt_recursive_mutex_trylock(ffrt_mutex_t* mutex); 137 138 /** 139 * @brief Destroy a recursive mutex. 140 * @param mutex Indicates a pointer to the mutex. 141 * @return Returns <b>ffrt_thrd_success</b> if the mutex is destroyed. 142 * returns <b>ffrt_thrd_error</b>otherwise. 143 * @since 10 144 * @version 1.0 145 */ 146 FFRT_C_API int ffrt_recursive_mutex_destroy(ffrt_mutex_t* mutex); 147 #endif 148