1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * Copyright 2021 NXP. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include "SyncEvent.h" 19 20 #include <android-base/stringprintf.h> 21 #include <android-base/logging.h> 22 23 using android::base::StringPrintf; 24 25 std::list<SyncEvent *> syncEventList; 26 std::mutex syncEventListMutex; 27 ~SyncEvent()28SyncEvent::~SyncEvent() { mWait = false; } 29 start()30void SyncEvent::start() { 31 mWait = false; 32 mMutex.lock(); 33 } 34 wait()35void SyncEvent::wait() { 36 mWait = true; 37 addEvent(); 38 while (mWait) { 39 mCondVar.wait(mMutex); 40 } 41 } 42 wait(long millisec)43bool SyncEvent::wait(long millisec) { 44 bool retVal; 45 mWait = true; 46 addEvent(); 47 while (mWait) { 48 retVal = mCondVar.wait(mMutex, millisec); 49 if (!retVal) 50 mWait = false; 51 } 52 return retVal; 53 } 54 notifyOne()55void SyncEvent::notifyOne() { 56 mWait = false; 57 removeEvent(); 58 mCondVar.notifyOne(); 59 } 60 notify()61void SyncEvent::notify() { 62 mWait = false; 63 mCondVar.notifyOne(); 64 } 65 end()66void SyncEvent::end() { 67 mWait = false; 68 mMutex.unlock(); 69 } 70 addEvent()71void SyncEvent::addEvent() { 72 std::lock_guard<std::mutex> guard( 73 syncEventListMutex); // with lock access list 74 bool contains = (std::find(syncEventList.begin(), syncEventList.end(), 75 this) != syncEventList.end()); 76 if (!contains) 77 syncEventList.push_back(this); 78 } 79 removeEvent()80void SyncEvent::removeEvent() { 81 std::lock_guard<std::mutex> guard( 82 syncEventListMutex); // with lock access list 83 syncEventList.remove(this); 84 } 85 notifyAll()86void SyncEvent::notifyAll() { 87 std::lock_guard<std::mutex> guard( 88 syncEventListMutex); // with lock access list 89 for (auto &i : syncEventList) { 90 if (i != NULL) 91 i->notify(); 92 } 93 syncEventList.clear(); 94 } 95