1 /* 2 * Copyright (c) 2025 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 18 23 */ 24 25 /** 26 * @file shared_mutex.h 27 * 28 * @brief Declares the shared mutex interfaces in C. 29 * 30 * @library libffrt.z.so 31 * @kit FunctionFlowRuntimeKit 32 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 33 * @since 18 34 */ 35 36 #ifndef FFRT_API_C_SHARED_MUTEX_H 37 #define FFRT_API_C_SHARED_MUTEX_H 38 39 #include "type_def.h" 40 41 /** 42 * @brief Initializes a rwlock. 43 * 44 * @param rwlock Indicates a pointer to the rwlock. 45 * @param attr Indicates a pointer to the rwlock attribute, only supports default mode, set to null pointer. 46 * @return Returns <b>ffrt_success</b> if the rwlock is initialized and the attr is nullptr; 47 returns <b>ffrt_error_inval</b> otherwise. 48 * @since 18 49 */ 50 FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr); 51 52 /** 53 * @brief Locks a write lock. 54 * 55 * @param rwlock Indicates a pointer to the rwlock. 56 * @return Returns <b>ffrt_success</b> if the rwlock is locked; 57 returns <b>ffrt_error_inval</b> or blocks the calling thread otherwise. 58 * @since 18 59 */ 60 FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock); 61 62 /** 63 * @brief Attempts to lock a write lock. 64 * 65 * @param rwlock Indicates a pointer to the rwlock. 66 * @return Returns <b>ffrt_success</b> if the rwlock is locked; 67 returns <b>ffrt_error_inval</b> or <b>ffrt_error_busy</b> otherwise. 68 * @since 18 69 */ 70 FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock); 71 72 /** 73 * @brief Locks a read lock. 74 * 75 * @param rwlock Indicates a pointer to the rwlock. 76 * @return Returns <b>ffrt_success</b> if the rwlock is locked; 77 returns <b>ffrt_error_inval</b> or blocks the calling thread otherwise. 78 * @since 18 79 */ 80 FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock); 81 82 /** 83 * @brief Attempts to lock a read lock. 84 * 85 * @param rwlock Indicates a pointer to the rwlock. 86 * @return Returns <b>ffrt_success</b> if the rwlock is locked; 87 returns <b>ffrt_error_inval</b> or <b>ffrt_error_busy</b> otherwise. 88 * @since 18 89 */ 90 FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock); 91 92 /** 93 * @brief Unlocks a rwlock. 94 * 95 * @param rwlock Indicates a pointer to the rwlock. 96 * @return Returns <b>ffrt_success</b> if the rwlock is unlocked; 97 returns <b>ffrt_error_inval</b> otherwise. 98 * @since 18 99 */ 100 FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock); 101 102 /** 103 * @brief Destroys a rwlock. 104 * 105 * @param rwlock Indicates a pointer to the rwlock. 106 * @return Returns <b>ffrt_success</b> if the rwlock is destroyed; 107 returns <b>ffrt_error_inval</b> otherwise. 108 * @since 18 109 */ 110 FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock); 111 112 #endif // FFRT_API_C_SHARED_MUTEX_H 113 /** @} */