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 mutex.h
307777dab0Sopenharmony_ci * @kit FunctionFlowRuntimeKit
317777dab0Sopenharmony_ci *
327777dab0Sopenharmony_ci * @brief Declares the mutex 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_MUTEX_H
397777dab0Sopenharmony_ci#define FFRT_API_C_MUTEX_H
407777dab0Sopenharmony_ci#include "type_def.h"
417777dab0Sopenharmony_ci
427777dab0Sopenharmony_ci/**
437777dab0Sopenharmony_ci * @brief Initializes mutex attr.
447777dab0Sopenharmony_ci *
457777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
467777dab0Sopenharmony_ci * @param attr Indicates a pointer to the mutex attribute.
477777dab0Sopenharmony_ci * @return {@link ffrt_success} 0 - success
487777dab0Sopenharmony_ci *         {@link ffrt_error_inval} 22 - if attr is null.
497777dab0Sopenharmony_ci * @since 12
507777dab0Sopenharmony_ci * @version 1.0
517777dab0Sopenharmony_ci */
527777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutexattr_init(ffrt_mutexattr_t* attr);
537777dab0Sopenharmony_ci
547777dab0Sopenharmony_ci/**
557777dab0Sopenharmony_ci * @brief set mutex type.
567777dab0Sopenharmony_ci *
577777dab0Sopenharmony_ci * @param attr Indicates a pointer to the mutex attribute.
587777dab0Sopenharmony_ci * @param type Indicates a int to the mutex type.
597777dab0Sopenharmony_ci * @return {@link ffrt_success} 0 - success.
607777dab0Sopenharmony_ci *         {@link ffrt_error_inval} 22 - if attr is null or type is not 0 or 2.
617777dab0Sopenharmony_ci * @since 12
627777dab0Sopenharmony_ci * @version 1.0
637777dab0Sopenharmony_ci */
647777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutexattr_settype(ffrt_mutexattr_t* attr, int type);
657777dab0Sopenharmony_ci
667777dab0Sopenharmony_ci/**
677777dab0Sopenharmony_ci * @brief get mutex type.
687777dab0Sopenharmony_ci *
697777dab0Sopenharmony_ci * @param attr Indicates a pointer to the mutex attribute.
707777dab0Sopenharmony_ci * @param type Indicates a pointer to the mutex type.
717777dab0Sopenharmony_ci * @return {@link ffrt_success} 0 - success.
727777dab0Sopenharmony_ci *         {@link ffrt_error_inval} 22 - if attr is null or type is null.
737777dab0Sopenharmony_ci * @since 12
747777dab0Sopenharmony_ci * @version 1.0
757777dab0Sopenharmony_ci */
767777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutexattr_gettype(ffrt_mutexattr_t* attr, int* type);
777777dab0Sopenharmony_ci
787777dab0Sopenharmony_ci/**
797777dab0Sopenharmony_ci * @brief destroy mutex attr, the user needs to invoke this interface.
807777dab0Sopenharmony_ci *
817777dab0Sopenharmony_ci * @param attr Indicates a pointer to the mutex attribute.
827777dab0Sopenharmony_ci * @return {@link ffrt_success} 0 - success.
837777dab0Sopenharmony_ci *         {@link ffrt_error_inval} 22 - if attr is null.
847777dab0Sopenharmony_ci * @since 12
857777dab0Sopenharmony_ci * @version 1.0
867777dab0Sopenharmony_ci */
877777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutexattr_destroy(ffrt_mutexattr_t* attr);
887777dab0Sopenharmony_ci
897777dab0Sopenharmony_ci/**
907777dab0Sopenharmony_ci * @brief Initializes a mutex.
917777dab0Sopenharmony_ci *
927777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
937777dab0Sopenharmony_ci * @param attr Indicates a pointer to the mutex attribute.
947777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the mutex is initialized;
957777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
967777dab0Sopenharmony_ci * @since 10
977777dab0Sopenharmony_ci * @version 1.0
987777dab0Sopenharmony_ci */
997777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr);
1007777dab0Sopenharmony_ci
1017777dab0Sopenharmony_ci/**
1027777dab0Sopenharmony_ci * @brief Locks a mutex.
1037777dab0Sopenharmony_ci *
1047777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
1057777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked;
1067777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> or blocks the calling thread otherwise.
1077777dab0Sopenharmony_ci * @since 10
1087777dab0Sopenharmony_ci * @version 1.0
1097777dab0Sopenharmony_ci */
1107777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex);
1117777dab0Sopenharmony_ci
1127777dab0Sopenharmony_ci/**
1137777dab0Sopenharmony_ci * @brief Unlocks a mutex.
1147777dab0Sopenharmony_ci *
1157777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
1167777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the mutex is unlocked;
1177777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
1187777dab0Sopenharmony_ci * @since 10
1197777dab0Sopenharmony_ci * @version 1.0
1207777dab0Sopenharmony_ci */
1217777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex);
1227777dab0Sopenharmony_ci
1237777dab0Sopenharmony_ci/**
1247777dab0Sopenharmony_ci * @brief Attempts to lock a mutex.
1257777dab0Sopenharmony_ci *
1267777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
1277777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the mutex is locked;
1287777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> or <b>ffrt_thrd_busy</b> otherwise.
1297777dab0Sopenharmony_ci * @since 10
1307777dab0Sopenharmony_ci * @version 1.0
1317777dab0Sopenharmony_ci */
1327777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex);
1337777dab0Sopenharmony_ci
1347777dab0Sopenharmony_ci/**
1357777dab0Sopenharmony_ci * @brief Destroys a mutex.
1367777dab0Sopenharmony_ci *
1377777dab0Sopenharmony_ci * @param mutex Indicates a pointer to the mutex.
1387777dab0Sopenharmony_ci * @return Returns <b>ffrt_thrd_success</b> if the mutex is destroyed;
1397777dab0Sopenharmony_ci           returns <b>ffrt_thrd_error</b> otherwise.
1407777dab0Sopenharmony_ci * @since 10
1417777dab0Sopenharmony_ci * @version 1.0
1427777dab0Sopenharmony_ci */
1437777dab0Sopenharmony_ciFFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex);
1447777dab0Sopenharmony_ci#endif
145