1484543d1Sopenharmony_ci/* 2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License. 5484543d1Sopenharmony_ci * You may obtain a copy of the License at 6484543d1Sopenharmony_ci * 7484543d1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8484543d1Sopenharmony_ci * 9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and 13484543d1Sopenharmony_ci * limitations under the License. 14484543d1Sopenharmony_ci */ 15484543d1Sopenharmony_ci 16484543d1Sopenharmony_ci/** 17484543d1Sopenharmony_ci * @addtogroup Ffrt 18484543d1Sopenharmony_ci * @{ 19484543d1Sopenharmony_ci * 20484543d1Sopenharmony_ci * @brief ffrt provides APIs. 21484543d1Sopenharmony_ci * 22484543d1Sopenharmony_ci * 23484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 24484543d1Sopenharmony_ci * 25484543d1Sopenharmony_ci * @since 10 26484543d1Sopenharmony_ci */ 27484543d1Sopenharmony_ci 28484543d1Sopenharmony_ci /** 29484543d1Sopenharmony_ci * @file condition_variable.h 30484543d1Sopenharmony_ci * 31484543d1Sopenharmony_ci * @brief Declares the condition variable interfaces in C. 32484543d1Sopenharmony_ci * 33484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 34484543d1Sopenharmony_ci * @since 10 35484543d1Sopenharmony_ci * @version 1.0 36484543d1Sopenharmony_ci */ 37484543d1Sopenharmony_ci#ifndef FFRT_API_C_CONDITION_VARIABLE_H 38484543d1Sopenharmony_ci#define FFRT_API_C_CONDITION_VARIABLE_H 39484543d1Sopenharmony_ci#include <time.h> 40484543d1Sopenharmony_ci#include "type_def.h" 41484543d1Sopenharmony_ci 42484543d1Sopenharmony_ci/** 43484543d1Sopenharmony_ci * @brief Initializes a condition variable. 44484543d1Sopenharmony_ci * 45484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 46484543d1Sopenharmony_ci * @param attr Indicates a pointer to the condition variable attribute. 47484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the condition variable is initialized; 48484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> otherwise. 49484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 50484543d1Sopenharmony_ci * @since 10 51484543d1Sopenharmony_ci * @version 1.0 52484543d1Sopenharmony_ci */ 53484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_init(ffrt_cond_t* cond, const ffrt_condattr_t* attr); 54484543d1Sopenharmony_ci 55484543d1Sopenharmony_ci/** 56484543d1Sopenharmony_ci * @brief Unblocks at least one of the threads that are blocked on a condition variable. 57484543d1Sopenharmony_ci * 58484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 59484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked; 60484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> otherwise. 61484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 62484543d1Sopenharmony_ci * @since 10 63484543d1Sopenharmony_ci * @version 1.0 64484543d1Sopenharmony_ci */ 65484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond); 66484543d1Sopenharmony_ci 67484543d1Sopenharmony_ci/** 68484543d1Sopenharmony_ci * @brief Unblocks all threads currently blocked on a condition variable. 69484543d1Sopenharmony_ci * 70484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 71484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the threads are unblocked; 72484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> otherwise. 73484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 74484543d1Sopenharmony_ci * @since 10 75484543d1Sopenharmony_ci * @version 1.0 76484543d1Sopenharmony_ci */ 77484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond); 78484543d1Sopenharmony_ci 79484543d1Sopenharmony_ci/** 80484543d1Sopenharmony_ci * @brief Blocks the calling thread. 81484543d1Sopenharmony_ci * 82484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 83484543d1Sopenharmony_ci * @param mutex Indicates a pointer to the mutex. 84484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked; 85484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> otherwise. 86484543d1Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core 87484543d1Sopenharmony_ci * @since 10 88484543d1Sopenharmony_ci * @version 1.0 89484543d1Sopenharmony_ci */ 90484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex); 91484543d1Sopenharmony_ci 92484543d1Sopenharmony_ci/** 93484543d1Sopenharmony_ci * @brief Blocks the calling thread for a given duration. 94484543d1Sopenharmony_ci * 95484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 96484543d1Sopenharmony_ci * @param mutex Indicates a pointer to the mutex. 97484543d1Sopenharmony_ci * @param time_point Indicates the maximum duration that the thread is blocked. 98484543d1Sopenharmony_ci * If <b>ffrt_cond_signal</b> or <b>ffrt_cond_broadcast</b> is not called to unblock the thread 99484543d1Sopenharmony_ci * when the maximum duration reaches, the thread is automatically unblocked. 100484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked; 101484543d1Sopenharmony_ci returns <b>ffrt_thrd_timedout</b> if the maximum duration reaches; 102484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> if the blocking fails. 103484543d1Sopenharmony_ci * @since 10 104484543d1Sopenharmony_ci * @version 1.0 105484543d1Sopenharmony_ci */ 106484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point); 107484543d1Sopenharmony_ci 108484543d1Sopenharmony_ci/** 109484543d1Sopenharmony_ci * @brief Destroys a condition variable. 110484543d1Sopenharmony_ci * 111484543d1Sopenharmony_ci * @param cond Indicates a pointer to the condition variable. 112484543d1Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the condition variable is destroyed; 113484543d1Sopenharmony_ci returns <b>ffrt_thrd_error</b> otherwise. 114484543d1Sopenharmony_ci * @since 10 115484543d1Sopenharmony_ci * @version 1.0 116484543d1Sopenharmony_ci */ 117484543d1Sopenharmony_ciFFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond); 118484543d1Sopenharmony_ci#endif 119