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 "CondVar.h" 22 #include "Mutex.h" 23 24 class SyncEvent { 25 public: 26 /******************************************************************************* 27 ** 28 ** Function: ~SyncEvent 29 ** 30 ** Description: Cleanup all resources. 31 ** 32 ** Returns: None. 33 ** 34 *******************************************************************************/ ~SyncEvent()35 ~SyncEvent() {} 36 37 /******************************************************************************* 38 ** 39 ** Function: start 40 ** 41 ** Description: Start a synchronization operation. 42 ** 43 ** Returns: None. 44 ** 45 *******************************************************************************/ start()46 void start() { mMutex.lock(); } 47 48 /******************************************************************************* 49 ** 50 ** Function: wait 51 ** 52 ** Description: Block the thread and wait for the event to occur. 53 ** 54 ** Returns: None. 55 ** 56 *******************************************************************************/ wait()57 void wait() { mCondVar.wait(mMutex); } 58 59 /******************************************************************************* 60 ** 61 ** Function: wait 62 ** 63 ** Description: Block the thread and wait for the event to occur. 64 ** millisec: Timeout in milliseconds. 65 ** 66 ** Returns: True if wait is successful; false if timeout occurs. 67 ** 68 *******************************************************************************/ wait(long millisec)69 bool wait(long millisec) { 70 bool retVal = mCondVar.wait(mMutex, millisec); 71 return retVal; 72 } 73 74 /******************************************************************************* 75 ** 76 ** Function: notifyOne 77 ** 78 ** Description: Notify a blocked thread that the event has occured. 79 *Unblocks it. 80 ** 81 ** Returns: None. 82 ** 83 *******************************************************************************/ notifyOne()84 void notifyOne() { mCondVar.notifyOne(); } 85 86 /******************************************************************************* 87 ** 88 ** Function: end 89 ** 90 ** Description: End a synchronization operation. 91 ** 92 ** Returns: None. 93 ** 94 *******************************************************************************/ end()95 void end() { mMutex.unlock(); } 96 97 private: 98 CondVar mCondVar; 99 Mutex mMutex; 100 }; 101 102 /*****************************************************************************/ 103 /*****************************************************************************/ 104 105 /***************************************************************************** 106 ** 107 ** Name: SyncEventGuard 108 ** 109 ** Description: Automatically start and end a synchronization event. 110 ** 111 *****************************************************************************/ 112 class SyncEventGuard { 113 public: 114 /******************************************************************************* 115 ** 116 ** Function: SyncEventGuard 117 ** 118 ** Description: Start a synchronization operation. 119 ** 120 ** Returns: None. 121 ** 122 *******************************************************************************/ SyncEventGuard(SyncEvent & event)123 SyncEventGuard(SyncEvent& event) : mEvent(event) { 124 event.start(); // automatically start operation 125 }; 126 127 /******************************************************************************* 128 ** 129 ** Function: ~SyncEventGuard 130 ** 131 ** Description: End a synchronization operation. 132 ** 133 ** Returns: None. 134 ** 135 *******************************************************************************/ ~SyncEventGuard()136 ~SyncEventGuard() { 137 mEvent.end(); // automatically end operation 138 }; 139 140 private: 141 SyncEvent& mEvent; 142 }; 143