1 /* 2 * Copyright (C) 2014 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 #ifndef DMTHREADHELPER_INCLUDE 18 #define DMTHREADHELPER_INCLUDE 19 20 /*================================================================================================== 21 22 Header Name: dmThreadHelper.h 23 24 General Description: Declarations for DMCritcalSection, DMThread and DMSingleLock classes. 25 26 ==================================================================================================*/ 27 28 #include <pthread.h> 29 #include <unistd.h> 30 #include <assert.h> 31 #include <memory.h> 32 #include <errno.h> 33 34 #ifdef DEBUG 35 #define IMASSERT(expression) assert(expression) 36 //#define NRES int nRes= 37 #define NRES(foo) int nRes=(foo) 38 #else 39 #define IMASSERT(expression) 40 //#define NRES 41 #define NRES(foo) foo 42 #endif 43 44 45 class DMCriticalSection 46 { 47 public: 48 /** 49 * Default constructor 50 */ 51 DMCriticalSection(); 52 53 /** 54 * Destructor 55 */ 56 ~DMCriticalSection(); 57 58 /** 59 * Locks mutex object 60 */ 61 void Enter(); 62 63 /** 64 * Unlocks mutex object 65 */ 66 void Leave(); 67 68 /** 69 * Tries to lock mutex object 70 * \return TRUE if mutex object is locked 71 */ 72 bool TryEnter(); 73 74 /** 75 * Retrieves handler on mutex object 76 */ 77 pthread_mutex_t& GetHandle() { return m_section;} 78 79 private: 80 /* handler */ 81 pthread_mutex_t m_section; 82 }; 83 84 85 class DMThread 86 { 87 public: 88 89 /** 90 * Default constructor 91 */ 92 DMThread() 93 { 94 m_bRunning = false; 95 memset(&m_hThread, 0, sizeof(m_hThread)); 96 } 97 98 /** 99 * Destructor 100 */ 101 virtual ~DMThread(){} 102 103 /** 104 * Starts thread 105 * \return TRUE if success 106 */ 107 bool StartThread(); 108 109 /** 110 * Stops thread 111 * \return TRUE if success 112 */ 113 bool StopThread(); 114 115 protected: 116 /** 117 * Run method to be implemented in an inherited class 118 */ 119 virtual void* Run() = 0; 120 private: 121 122 /** 123 * Thread callback function 124 */ 125 static void* ThreadProc(void *pArg); 126 127 protected: 128 /** Flag to specify if thread is running */ 129 bool m_bRunning; 130 /** Thread handler */ 131 pthread_t m_hThread; 132 }; 133 134 135 inline DMCriticalSection::DMCriticalSection( ) 136 { 137 memset( &m_section, 0, sizeof( m_section ) ); 138 139 NRES(pthread_mutex_init( &m_section, 0 )); 140 IMASSERT( nRes == 0 ); 141 } 142 143 inline DMCriticalSection::~DMCriticalSection() 144 { 145 NRES(pthread_mutex_destroy( &m_section )); 146 IMASSERT( nRes == 0 ); 147 } 148 149 inline void DMCriticalSection::Enter() 150 { 151 NRES(pthread_mutex_lock( &m_section )); 152 IMASSERT( nRes == 0 ); 153 } 154 155 inline void DMCriticalSection::Leave() 156 { 157 NRES(pthread_mutex_unlock( &m_section )); 158 159 IMASSERT( nRes == 0 ); 160 } 161 162 inline bool DMCriticalSection::TryEnter() 163 { 164 int nRes = pthread_mutex_trylock( &m_section ); 165 166 IMASSERT( nRes == 0 || nRes == EBUSY ); 167 168 return nRes == 0; 169 } 170 171 172 class DMSingleLock 173 { 174 DMCriticalSection& m_oSection; 175 public: 176 /** 177 * Constructor 178 * \param oSection [in] - critical section(mutex) 179 */ 180 DMSingleLock( DMCriticalSection& oSection ): m_oSection( oSection ) 181 { 182 m_oSection.Enter(); 183 } 184 185 /** 186 * Destructor 187 */ 188 ~DMSingleLock() 189 { 190 m_oSection.Leave(); 191 } 192 }; 193 194 #define DmThSleep(a) usleep(a) 195 196 #endif 197