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 condition_variable.h 30 * @kit FunctionFlowRuntimeKit 31 * 32 * @brief Declares the condition variable interfaces in C. 33 * @library libffrt.z.so 34 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 35 * @since 10 36 * @version 1.0 37 */ 38 #ifndef FFRT_API_C_CONDITION_VARIABLE_H 39 #define FFRT_API_C_CONDITION_VARIABLE_H 40 #include <time.h> 41 #include "type_def.h" 42 43 /** 44 * @brief Initializes a condition variable. 45 * 46 * @param cond Indicates a pointer to the condition variable. 47 * @param attr Indicates a pointer to the condition variable attribute. 48 * @return Returns <b>ffrt_thrd_success</b> if the condition variable is initialized; 49 returns <b>ffrt_thrd_error</b> otherwise. 50 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 51 * @since 10 52 * @version 1.0 53 */ 54 FFRT_C_API int ffrt_cond_init(ffrt_cond_t* cond, const ffrt_condattr_t* attr); 55 56 /** 57 * @brief Unblocks at least one of the threads that are blocked on a condition variable. 58 * 59 * @param cond Indicates a pointer to the condition variable. 60 * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked; 61 returns <b>ffrt_thrd_error</b> otherwise. 62 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 63 * @since 10 64 * @version 1.0 65 */ 66 FFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond); 67 68 /** 69 * @brief Unblocks all threads currently blocked on a condition variable. 70 * 71 * @param cond Indicates a pointer to the condition variable. 72 * @return Returns <b>ffrt_thrd_success</b> if the threads are unblocked; 73 returns <b>ffrt_thrd_error</b> otherwise. 74 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 75 * @since 10 76 * @version 1.0 77 */ 78 FFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond); 79 80 /** 81 * @brief Blocks the calling thread. 82 * 83 * @param cond Indicates a pointer to the condition variable. 84 * @param mutex Indicates a pointer to the mutex. 85 * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked; 86 returns <b>ffrt_thrd_error</b> otherwise. 87 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 88 * @since 10 89 * @version 1.0 90 */ 91 FFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex); 92 93 /** 94 * @brief Blocks the calling thread for a given duration. 95 * 96 * @param cond Indicates a pointer to the condition variable. 97 * @param mutex Indicates a pointer to the mutex. 98 * @param time_point Indicates the maximum duration that the thread is blocked. 99 * If <b>ffrt_cond_signal</b> or <b>ffrt_cond_broadcast</b> is not called to unblock the thread 100 * when the maximum duration reaches, the thread is automatically unblocked. 101 * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked; 102 returns <b>ffrt_thrd_timedout</b> if the maximum duration reaches; 103 returns <b>ffrt_thrd_error</b> if the blocking fails. 104 * @since 10 105 * @version 1.0 106 */ 107 FFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point); 108 109 /** 110 * @brief Destroys a condition variable. 111 * 112 * @param cond Indicates a pointer to the condition variable. 113 * @return Returns <b>ffrt_thrd_success</b> if the condition variable is destroyed; 114 returns <b>ffrt_thrd_error</b> otherwise. 115 * @since 10 116 * @version 1.0 117 */ 118 FFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond); 119 #endif 120 /** @} */