1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. 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 __WIFI_HAL_SYNC_H__ 17 #define __WIFI_HAL_SYNC_H__ 18 19 #include <pthread.h> 20 21 class Mutex { 22 public: Mutex()23 Mutex() 24 { 25 pthread_mutex_init(&mMutex, nullptr); 26 } ~Mutex()27 ~Mutex() 28 { 29 pthread_mutex_destroy(&mMutex); 30 } TryLock()31 int TryLock() 32 { 33 return pthread_mutex_trylock(&mMutex); 34 } Lock()35 int Lock() 36 { 37 return pthread_mutex_lock(&mMutex); 38 } Unlock()39 void Unlock() 40 { 41 pthread_mutex_unlock(&mMutex); 42 } 43 44 private: 45 pthread_mutex_t mMutex; 46 }; 47 48 class Condition { 49 public: Condition()50 Condition() 51 { 52 pthread_mutex_init(&mMutex, nullptr); 53 pthread_cond_init(&mCondition, nullptr); 54 } ~Condition()55 ~Condition() 56 { 57 pthread_cond_destroy(&mCondition); 58 pthread_mutex_destroy(&mMutex); 59 } 60 Wait()61 int Wait() 62 { 63 return pthread_cond_wait(&mCondition, &mMutex); 64 } 65 Signal()66 void Signal() 67 { 68 pthread_cond_signal(&mCondition); 69 } 70 71 private: 72 pthread_cond_t mCondition; 73 pthread_mutex_t mMutex; 74 }; 75 76 #endif 77