1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * Synchronize two or more threads using a condition variable and a mutex. 19 */ 20 #pragma once 21 #include <mutex> 22 23 #include "CondVar.h" 24 25 class SyncEvent { 26 public: 27 /******************************************************************************* 28 ** 29 ** Function: ~SyncEvent 30 ** 31 ** Description: Cleanup all resources. 32 ** 33 ** Returns: None. 34 ** 35 *******************************************************************************/ ~SyncEvent()36 ~SyncEvent() {} 37 38 /******************************************************************************* 39 ** 40 ** Function: start 41 ** 42 ** Description: Start a synchronization operation. 43 ** 44 ** Returns: None. 45 ** 46 *******************************************************************************/ start()47 void start() { mMutex.lock(); } 48 49 /******************************************************************************* 50 ** 51 ** Function: wait 52 ** 53 ** Description: Block the thread and wait for the event to occur. 54 ** 55 ** Returns: None. 56 ** 57 *******************************************************************************/ wait()58 void wait() { mCondVar.wait(mMutex); } 59 60 /******************************************************************************* 61 ** 62 ** Function: wait 63 ** 64 ** Description: Block the thread and wait for the event to occur. 65 ** millisec: Timeout in milliseconds. 66 ** 67 ** Returns: True if wait is successful; false if timeout occurs. 68 ** 69 *******************************************************************************/ wait(long millisec)70 bool wait(long millisec) { 71 bool retVal = mCondVar.wait(mMutex, millisec); 72 return retVal; 73 } 74 75 /******************************************************************************* 76 ** 77 ** Function: notifyOne 78 ** 79 ** Description: Notify a blocked thread that the event has occurred. 80 *Unblocks it. 81 ** 82 ** Returns: None. 83 ** 84 *******************************************************************************/ notifyOne()85 void notifyOne() { mCondVar.notifyOne(); } 86 87 /******************************************************************************* 88 ** 89 ** Function: end 90 ** 91 ** Description: End a synchronization operation. 92 ** 93 ** Returns: None. 94 ** 95 *******************************************************************************/ end()96 void end() { mMutex.unlock(); } 97 98 private: 99 CondVar mCondVar; 100 std::mutex mMutex; 101 }; 102 103 /*****************************************************************************/ 104 /*****************************************************************************/ 105 106 /***************************************************************************** 107 ** 108 ** Name: SyncEventGuard 109 ** 110 ** Description: Automatically start and end a synchronization event. 111 ** 112 *****************************************************************************/ 113 class SyncEventGuard { 114 public: 115 /******************************************************************************* 116 ** 117 ** Function: SyncEventGuard 118 ** 119 ** Description: Start a synchronization operation. 120 ** 121 ** Returns: None. 122 ** 123 *******************************************************************************/ SyncEventGuard(SyncEvent & event)124 SyncEventGuard(SyncEvent& event) : mEvent(event) { 125 event.start(); // automatically start operation 126 }; 127 128 /******************************************************************************* 129 ** 130 ** Function: ~SyncEventGuard 131 ** 132 ** Description: End a synchronization operation. 133 ** 134 ** Returns: None. 135 ** 136 *******************************************************************************/ ~SyncEventGuard()137 ~SyncEventGuard() { 138 mEvent.end(); // automatically end operation 139 }; 140 141 private: 142 SyncEvent& mEvent; 143 }; 144