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  *
31  * @brief Declares the condition variable interfaces in C.
32  *
33  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
34  * @since 10
35  * @version 1.0
36  */
37 #ifndef FFRT_API_C_CONDITION_VARIABLE_H
38 #define FFRT_API_C_CONDITION_VARIABLE_H
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_thrd_success</b> if the condition variable is initialized;
48            returns <b>ffrt_thrd_error</b> otherwise.
49  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
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_thrd_success</b> if the thread is unblocked;
60            returns <b>ffrt_thrd_error</b> otherwise.
61  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
62  * @since 10
63  * @version 1.0
64  */
65 FFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond);
66 
67 /**
68  * @brief Unblocks all threads currently blocked on a condition variable.
69  *
70  * @param cond Indicates a pointer to the condition variable.
71  * @return Returns <b>ffrt_thrd_success</b> if the threads are unblocked;
72            returns <b>ffrt_thrd_error</b> otherwise.
73  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
74  * @since 10
75  * @version 1.0
76  */
77 FFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond);
78 
79 /**
80  * @brief Blocks the calling thread.
81  *
82  * @param cond Indicates a pointer to the condition variable.
83  * @param mutex Indicates a pointer to the mutex.
84  * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked;
85            returns <b>ffrt_thrd_error</b> otherwise.
86  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
87  * @since 10
88  * @version 1.0
89  */
90 FFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex);
91 
92 /**
93  * @brief Blocks the calling thread for a given duration.
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  * If <b>ffrt_cond_signal</b> or <b>ffrt_cond_broadcast</b> is not called to unblock the thread
99  * when the maximum duration reaches, the thread is automatically unblocked.
100  * @return Returns <b>ffrt_thrd_success</b> if the thread is unblocked after being blocked;
101            returns <b>ffrt_thrd_timedout</b> if the maximum duration reaches;
102            returns <b>ffrt_thrd_error</b> if the blocking fails.
103  * @since 10
104  * @version 1.0
105  */
106 FFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point);
107 
108 /**
109  * @brief Destroys a condition variable.
110  *
111  * @param cond Indicates a pointer to the condition variable.
112  * @return Returns <b>ffrt_thrd_success</b> if the condition variable is destroyed;
113            returns <b>ffrt_thrd_error</b> otherwise.
114  * @since 10
115  * @version 1.0
116  */
117 FFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond);
118 #endif
119