locks.c revision 7b726bdcd996f1cab3a584c04ce1afc07bc8fbe7
1/* 2 * Copyright (C) 2010 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#include "sles_allinclusive.h" 18 19 20/** \brief Exclusively lock an object */ 21 22#ifdef USE_DEBUG 23void object_lock_exclusive_(IObject *thiz, const char *file, int line) 24{ 25 int ok; 26 ok = pthread_mutex_trylock(&thiz->mMutex); 27 if (0 != ok) { 28 // pthread_mutex_timedlock_np is not available, but wait up to 100 ms 29 static const useconds_t backoffs[] = {1, 10000, 20000, 30000, 40000}; 30 unsigned i = 0; 31 for (;;) { 32 usleep(backoffs[i]); 33 ok = pthread_mutex_trylock(&thiz->mMutex); 34 if (0 == ok) 35 break; 36 if (++i >= (sizeof(backoffs) / sizeof(backoffs[0]))) { 37 SL_LOGW("%s:%d: object %p was locked by %p at %s:%d\n", 38 file, line, thiz, *(void **)&thiz->mOwner, thiz->mFile, thiz->mLine); 39 // attempt one more time; maybe this time we will be successful 40 ok = pthread_mutex_lock(&thiz->mMutex); 41 assert(0 == ok); 42 break; 43 } 44 } 45 } 46 pthread_t zero; 47 memset(&zero, 0, sizeof(pthread_t)); 48 if (0 != memcmp(&zero, &thiz->mOwner, sizeof(pthread_t))) { 49 if (pthread_equal(pthread_self(), thiz->mOwner)) { 50 SL_LOGE("%s:%d: object %p was recursively locked by %p at %s:%d\n", 51 file, line, thiz, *(void **)&thiz->mOwner, thiz->mFile, thiz->mLine); 52 } else { 53 SL_LOGE("%s:%d: object %p was left unlocked in unexpected state by %p at %s:%d\n", 54 file, line, thiz, *(void **)&thiz->mOwner, thiz->mFile, thiz->mLine); 55 } 56 assert(false); 57 } 58 thiz->mOwner = pthread_self(); 59 thiz->mFile = file; 60 thiz->mLine = line; 61} 62#else 63void object_lock_exclusive(IObject *thiz) 64{ 65 int ok; 66 ok = pthread_mutex_lock(&thiz->mMutex); 67 assert(0 == ok); 68} 69#endif 70 71 72/** \brief Exclusively unlock an object and do not report any updates */ 73 74#ifdef USE_DEBUG 75void object_unlock_exclusive_(IObject *thiz, const char *file, int line) 76{ 77 assert(pthread_equal(pthread_self(), thiz->mOwner)); 78 assert(NULL != thiz->mFile); 79 assert(0 != thiz->mLine); 80 memset(&thiz->mOwner, 0, sizeof(pthread_t)); 81 thiz->mFile = file; 82 thiz->mLine = line; 83 int ok; 84 ok = pthread_mutex_unlock(&thiz->mMutex); 85 assert(0 == ok); 86} 87#else 88void object_unlock_exclusive(IObject *thiz) 89{ 90 int ok; 91 ok = pthread_mutex_unlock(&thiz->mMutex); 92 assert(0 == ok); 93} 94#endif 95 96 97/** \brief Exclusively unlock an object and report updates to the specified bit-mask of 98 * attributes 99 */ 100 101#ifdef USE_DEBUG 102void object_unlock_exclusive_attributes_(IObject *thiz, unsigned attributes, 103 const char *file, int line) 104#else 105void object_unlock_exclusive_attributes(IObject *thiz, unsigned attributes) 106#endif 107{ 108 109#ifdef USE_DEBUG 110 assert(pthread_equal(pthread_self(), thiz->mOwner)); 111 assert(NULL != thiz->mFile); 112 assert(0 != thiz->mLine); 113#endif 114 115 int ok; 116 117 // make SL object IDs be contiguous with XA object IDs 118 SLuint32 objectID = IObjectToObjectID(thiz); 119 if ((XA_OBJECTID_ENGINE <= objectID) && (objectID <= XA_OBJECTID_CAMERADEVICE)) { 120 ; 121 } else if ((SL_OBJECTID_ENGINE <= objectID) && (objectID <= SL_OBJECTID_METADATAEXTRACTOR)) { 122 objectID -= SL_OBJECTID_ENGINE - XA_OBJECTID_CAMERADEVICE - 1; 123 } else { 124 assert(false); 125 objectID = 0; 126 } 127 128 // first synchronously handle updates to attributes here, while object is still locked. 129 // This appears to be a loop, but actually typically runs through the loop only once. 130 unsigned asynchronous = attributes; 131 while (attributes) { 132 // this sequence is carefully crafted to be O(1); tread carefully when making changes 133 unsigned bit = ctz(attributes); 134 // ATTR_INDEX_MAX == next bit position after the last attribute 135 assert(ATTR_INDEX_MAX > bit); 136 // compute the entry in the handler table using object ID and bit number 137 AttributeHandler handler = handlerTable[objectID][bit]; 138 if (NULL != handler) { 139 asynchronous &= ~(*handler)(thiz); 140 } 141 attributes &= ~(1 << bit); 142 } 143 144 // any remaining attributes are handled asynchronously in the sync thread 145 if (asynchronous) { 146 unsigned oldAttributesMask = thiz->mAttributesMask; 147 thiz->mAttributesMask = oldAttributesMask | asynchronous; 148 if (oldAttributesMask) { 149 asynchronous = ATTR_NONE; 150 } 151 } 152 153#ifdef USE_DEBUG 154 memset(&thiz->mOwner, 0, sizeof(pthread_t)); 155 thiz->mFile = file; 156 thiz->mLine = line; 157#endif 158 ok = pthread_mutex_unlock(&thiz->mMutex); 159 assert(0 == ok); 160 161 // first update to this interface since previous sync 162 if (ATTR_NONE != asynchronous) { 163 unsigned id = thiz->mInstanceID; 164 if (0 != id) { 165 --id; 166 assert(MAX_INSTANCE > id); 167 IEngine *thisEngine = &thiz->mEngine->mEngine; 168 // FIXME atomic or here 169 interface_lock_exclusive(thisEngine); 170 thisEngine->mChangedMask |= 1 << id; 171 interface_unlock_exclusive(thisEngine); 172 } 173 } 174 175} 176 177 178/** \brief Wait on the condition variable associated with the object; see pthread_cond_wait */ 179 180#ifdef USE_DEBUG 181void object_cond_wait_(IObject *thiz, const char *file, int line) 182{ 183 // note that this will unlock the mutex, so we have to clear the owner 184 assert(pthread_equal(pthread_self(), thiz->mOwner)); 185 assert(NULL != thiz->mFile); 186 assert(0 != thiz->mLine); 187 memset(&thiz->mOwner, 0, sizeof(pthread_t)); 188 thiz->mFile = file; 189 thiz->mLine = line; 190 // alas we don't know the new owner's identity 191 int ok; 192 ok = pthread_cond_wait(&thiz->mCond, &thiz->mMutex); 193 assert(0 == ok); 194 // restore my ownership 195 thiz->mOwner = pthread_self(); 196 thiz->mFile = file; 197 thiz->mLine = line; 198} 199#else 200void object_cond_wait(IObject *thiz) 201{ 202 int ok; 203 ok = pthread_cond_wait(&thiz->mCond, &thiz->mMutex); 204 assert(0 == ok); 205} 206#endif 207 208 209/** \brief Signal the condition variable associated with the object; see pthread_cond_signal */ 210 211void object_cond_signal(IObject *thiz) 212{ 213 int ok; 214 ok = pthread_cond_signal(&thiz->mCond); 215 assert(0 == ok); 216} 217 218 219/** \brief Broadcast the condition variable associated with the object; 220 * see pthread_cond_broadcast 221 */ 222 223void object_cond_broadcast(IObject *thiz) 224{ 225 int ok; 226 ok = pthread_cond_broadcast(&thiz->mCond); 227 assert(0 == ok); 228} 229