17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci/**
177777dab0Sopenharmony_ci * @addtogroup Ffrt
187777dab0Sopenharmony_ci * @{
197777dab0Sopenharmony_ci *
207777dab0Sopenharmony_ci * @brief ffrt provides APIs.
217777dab0Sopenharmony_ci *
227777dab0Sopenharmony_ci *
237777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
247777dab0Sopenharmony_ci *
257777dab0Sopenharmony_ci * @since 10
267777dab0Sopenharmony_ci */
277777dab0Sopenharmony_ci
287777dab0Sopenharmony_ci /**
297777dab0Sopenharmony_ci * @file condition_variable.h
307777dab0Sopenharmony_ci * @kit FunctionFlowRuntimeKit
317777dab0Sopenharmony_ci *
327777dab0Sopenharmony_ci * @brief Declares the condition variable interfaces in C.
337777dab0Sopenharmony_ci *
347777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
357777dab0Sopenharmony_ci * @since 10
367777dab0Sopenharmony_ci * @version 1.0
377777dab0Sopenharmony_ci */
387777dab0Sopenharmony_ci#ifndef FFRT_API_C_CONDITION_VARIABLE_H
397777dab0Sopenharmony_ci#define FFRT_API_C_CONDITION_VARIABLE_H
407777dab0Sopenharmony_ci#include <time.h>
417777dab0Sopenharmony_ci#include "type_def.h"
427777dab0Sopenharmony_ci
437777dab0Sopenharmony_ci/**
447777dab0Sopenharmony_ci * @brief Initializes a condition variable.
457777dab0Sopenharmony_ci *
467777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
477777dab0Sopenharmony_ci * @param attr Indicates a pointer to the condition variable attribute.
487777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the condition variable is initialized;
497777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
507777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
517777dab0Sopenharmony_ci * @since 10
527777dab0Sopenharmony_ci * @version 1.0
537777dab0Sopenharmony_ci */
547777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_init(ffrt_cond_t* cond, const ffrt_condattr_t* attr);
557777dab0Sopenharmony_ci
567777dab0Sopenharmony_ci/**
577777dab0Sopenharmony_ci * @brief Unblocks at least one of the threads that are blocked on a condition variable.
587777dab0Sopenharmony_ci *
597777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
607777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked;
617777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
627777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
637777dab0Sopenharmony_ci * @since 10
647777dab0Sopenharmony_ci * @version 1.0
657777dab0Sopenharmony_ci */
667777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond);
677777dab0Sopenharmony_ci
687777dab0Sopenharmony_ci/**
697777dab0Sopenharmony_ci * @brief Unblocks all threads currently blocked on a condition variable.
707777dab0Sopenharmony_ci *
717777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
727777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the threads are unblocked;
737777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
747777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
757777dab0Sopenharmony_ci * @since 10
767777dab0Sopenharmony_ci * @version 1.0
777777dab0Sopenharmony_ci */
787777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond);
797777dab0Sopenharmony_ci
807777dab0Sopenharmony_ci/**
817777dab0Sopenharmony_ci * @brief Blocks the calling thread.
827777dab0Sopenharmony_ci *
837777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
847777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
857777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked;
867777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
877777dab0Sopenharmony_ci * @syscap SystemCapability.Resourceschedule.Ffrt.Core
887777dab0Sopenharmony_ci * @since 10
897777dab0Sopenharmony_ci * @version 1.0
907777dab0Sopenharmony_ci */
917777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex);
927777dab0Sopenharmony_ci
937777dab0Sopenharmony_ci/**
947777dab0Sopenharmony_ci * @brief Blocks the calling thread for a given duration.
957777dab0Sopenharmony_ci *
967777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
977777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
987777dab0Sopenharmony_ci * @param time_point Indicates the maximum duration that the thread is blocked.
997777dab0Sopenharmony_ci * If <b>ffrt_cond_signal</b> or <b>ffrt_cond_broadcast</b> is not called to unblock the thread
1007777dab0Sopenharmony_ci * when the maximum duration reaches, the thread is automatically unblocked.
1017777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked;
1027777dab0Sopenharmony_ci           returns <b>ffrt_thrd_timedout</b> if the maximum duration reaches;
1037777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> if the blocking fails.
1047777dab0Sopenharmony_ci * @since 10
1057777dab0Sopenharmony_ci * @version 1.0
1067777dab0Sopenharmony_ci */
1077777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point);
1087777dab0Sopenharmony_ci
1097777dab0Sopenharmony_ci/**
1107777dab0Sopenharmony_ci * @brief Destroys a condition variable.
1117777dab0Sopenharmony_ci *
1127777dab0Sopenharmony_ci * @param cond Indicates a pointer to the condition variable.
1137777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the condition variable is destroyed;
1147777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
1157777dab0Sopenharmony_ci * @since 10
1167777dab0Sopenharmony_ci * @version 1.0
1177777dab0Sopenharmony_ci */
1187777dab0Sopenharmony_ciFFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond);
1197777dab0Sopenharmony_ci#endif
120