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