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 condition_variable.h 27 * 28 * @brief Declares the condition variable 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_CONDITION_VARIABLE_H 37 #define FFRT_API_C_CONDITION_VARIABLE_H 38 39 #include <time.h> 40 #include "type_def.h" 41 42 /** 43 * @brief Initializes a condition variable. 44 * 45 * @param cond Indicates a pointer to the condition variable. 46 * @param attr Indicates a pointer to the condition variable attribute. 47 * @return Returns <b>ffrt_success</b> if the condition variable is initialized; 48 returns <b>ffrt_error_inval</b> otherwise. 49 * @since 10 50 */ 51 FFRT_C_API int ffrt_cond_init(ffrt_cond_t* cond, const ffrt_condattr_t* attr); 52 53 /** 54 * @brief Unblocks at least one of the threads that are blocked on a condition variable. 55 * 56 * @param cond Indicates a pointer to the condition variable. 57 * @return Returns <b>ffrt_success</b> if the thread is unblocked; 58 returns <b>ffrt_error_inval</b> otherwise. 59 * @since 10 60 */ 61 FFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond); 62 63 /** 64 * @brief Unblocks all threads currently blocked on a condition variable. 65 * 66 * @param cond Indicates a pointer to the condition variable. 67 * @return Returns <b>ffrt_success</b> if the threads are unblocked; 68 returns <b>ffrt_error_inval</b> otherwise. 69 * @since 10 70 */ 71 FFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond); 72 73 /** 74 * @brief Blocks the calling thread. 75 * 76 * @param cond Indicates a pointer to the condition variable. 77 * @param mutex Indicates a pointer to the mutex. 78 * @return Returns <b>ffrt_success</b> if the thread is unblocked after being blocked; 79 returns <b>ffrt_error_inval</b> otherwise. 80 * @since 10 81 */ 82 FFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex); 83 84 /** 85 * @brief Blocks the calling thread for a given duration. 86 * 87 * If <b>ffrt_cond_signal</b> or <b>ffrt_cond_broadcast</b> is not called to unblock the thread 88 * when the maximum duration reaches, the thread is automatically unblocked. 89 * 90 * @param cond Indicates a pointer to the condition variable. 91 * @param mutex Indicates a pointer to the mutex. 92 * @param time_point Indicates the maximum duration that the thread is blocked. 93 * @return Returns <b>ffrt_success</b> if the thread is unblocked after being blocked; 94 returns <b>ffrt_error_timedout</b> if the maximum duration reaches; 95 returns <b>ffrt_error_inval</b> if the blocking fails. 96 * @since 10 97 */ 98 FFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point); 99 100 /** 101 * @brief Destroys a condition variable, the user needs to invoke this interface. 102 * 103 * @param cond Indicates a pointer to the condition variable. 104 * @return Returns <b>ffrt_success</b> if the condition variable is destroyed; 105 returns <b>ffrt_error_inval</b> otherwise. 106 * @since 10 107 */ 108 FFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond); 109 110 #endif // FFRT_API_C_CONDITION_VARIABLE_H 111 /** @} */