1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Portions copyright (C) 2017 Broadcom Limited 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include <pthread.h> 20 21 #ifndef __WIFI_HAL_SYNC_H__ 22 #define __WIFI_HAL_SYNC_H__ 23 24 class Mutex 25 { 26 private: 27 pthread_mutex_t mMutex; 28 public: Mutex()29 Mutex() { 30 pthread_mutex_init(&mMutex, NULL); 31 } ~Mutex()32 ~Mutex() { 33 pthread_mutex_destroy(&mMutex); 34 } tryLock()35 int tryLock() { 36 return pthread_mutex_trylock(&mMutex); 37 } lock()38 int lock() { 39 return pthread_mutex_lock(&mMutex); 40 } unlock()41 void unlock() { 42 pthread_mutex_unlock(&mMutex); 43 } 44 }; 45 46 class Condition 47 { 48 private: 49 pthread_cond_t mCondition; 50 pthread_mutex_t mMutex; 51 52 public: Condition()53 Condition() { 54 pthread_mutex_init(&mMutex, NULL); 55 pthread_cond_init(&mCondition, NULL); 56 } ~Condition()57 ~Condition() { 58 pthread_cond_destroy(&mCondition); 59 pthread_mutex_destroy(&mMutex); 60 } 61 wait()62 int wait() { 63 return pthread_cond_wait(&mCondition, &mMutex); 64 } 65 signal()66 void signal() { 67 pthread_cond_signal(&mCondition); 68 } 69 }; 70 71 #endif