1 /* 2 * Copyright (C) 2021 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 #ifndef SEMAPHORE_H 17 #define SEMAPHORE_H 18 19 #include <stdint.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 typedef struct Semaphore Semaphore; 26 27 /** 28 * @brief Perform instantiation of the Semaphore. 29 * 30 * @param val set Semaphore initial val. 31 * @return Semaphore* Succeed return Semaphore pointer, failed return NULL. 32 * @since 6 33 */ 34 Semaphore *SemaphoreCreate(uint32_t val); 35 36 /** 37 * @brief Delete an instantiation of the Semaphore. 38 * 39 * @param sem Semaphore pointer. 40 * @since 6 41 */ 42 void SemaphoreDelete(Semaphore *sem); 43 44 /** 45 * @brief Block reduce Semaphore val by 1. 46 * 47 * @param sem Semaphore pointer. 48 * @since 6 49 */ 50 void SemaphoreWait(const Semaphore *sem); 51 52 /** 53 * @brief NonBlock reduce Semaphore val by 1. 54 * 55 * @param sem Semaphore pointer. 56 * @return Succeed return 0, failed return -1. 57 * @since 6 58 */ 59 int32_t SemaphoreTryWait(Semaphore *sem); 60 61 /** 62 * @brief Block increase Semaphore val by 1. 63 * 64 * @param sem Semaphore pointer. 65 * @since 6 66 */ 67 void SemaphorePost(const Semaphore *sem); 68 69 /** 70 * @brief Try Increase Semaphore val by 1. 71 * 72 * @param sem Semaphore pointer. 73 * @return Succeed return 0, failed return -1. 74 * @since 6 75 */ 76 int32_t SemaphoreTryPost(Semaphore *sem); 77 78 /** 79 * @brief Get Semaphore instantiation fd. 80 * 81 * @param sem Semaphore pointer. 82 * @return Succeed return fd, failed return -1. 83 * @since 6 84 */ 85 int32_t SemaphoreGetfd(const Semaphore *sem); 86 87 #ifdef __cplusplus 88 } 89 #endif 90 91 #endif // SEMAPHORE_H