Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <pthread.h> #include "dyldLock.h" // until the reader/writer locks are fully tested, we just use a simple recursive mutex #define REAL_READER_WRITER_LOCK 0 // // This class implements a recursive reader/writer lock. // Recursive means a thread that has already aquired the lock can re-acquire it. // The lock allows either: // a) one "writer" thread to hold lock // b) multiple "reader" threads to hold // // A thread with the writer can acquire a reader lock. // A thread with a reader lock can acquire a writer lock iff it is the only thread with a reader lock // class RecursiveReaderWriterLock { public: RecursiveReaderWriterLock() __attribute__((visibility("hidden"))); void initIfNeeded(); void lockForSingleWritingThread() __attribute__((visibility("hidden"))); void unlockForSingleWritingThread() __attribute__((visibility("hidden"))); void lockForMultipleReadingThreads() __attribute__((visibility("hidden"))); void unlockForMultipleReadingThreads() __attribute__((visibility("hidden"))); private: #if REAL_READER_WRITER_LOCK struct ThreadRecursionCount { pthread_t fThread; uint32_t fCount; }; bool writerThreadIsAnyThreadBut(pthread_t thread); void writerThreadRetain(pthread_t thread); bool writerThreadRelease(pthread_t thread); enum { kMaxReaderThreads = 4 }; bool readerThreadSetRetain(pthread_t thread); bool readerThreadSetRelease(pthread_t thread); bool readerThreadSetContainsAnotherThread(pthread_t thread); ThreadRecursionCount fWriterThread; ThreadRecursionCount fReaderThreads[kMaxReaderThreads]; pthread_cond_t fLockFree; pthread_mutex_t fMutex; #else pthread_mutex_t fMutex; #endif bool fInitialized; // assumes this is statically initialized to false because sLock is static }; // // initIfNeeded() is a hack so that when libSystem_debug.dylb is useable. // The problem is that Objective-C +load methods are called before C++ initialziers are run // If +load method makes a call into dyld, sLock is not initialized. // // The long term solution is for objc and dyld to work more closely together so that instead // of running all +load methods before all initializers, we run each image's +load then its // initializers all in bottom up order. // // This lazy initialization is not thread safe, but as long as someone does not create a // new thread in a +load method, the C++ constructor for sLock will be called before main() // so there will only be one thead. // void RecursiveReaderWriterLock::initIfNeeded() { if ( ! fInitialized ) { pthread_mutexattr_t recursiveMutexAttr; pthread_mutexattr_init(&recursiveMutexAttr); pthread_mutexattr_settype(&recursiveMutexAttr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&fMutex, &recursiveMutexAttr); #if REAL_READER_WRITER_LOCK pthread_cond_init(&fLockFree, NULL); fWriterThread.fThread = NULL; fWriterThread.fCount = 0; for (int i=0; i < kMaxReaderThreads; ++i) { fReaderThreads[i].fThread = NULL; fReaderThreads[i].fCount = 0; } #endif fInitialized = true; } } RecursiveReaderWriterLock::RecursiveReaderWriterLock() { initIfNeeded(); } void RecursiveReaderWriterLock::lockForSingleWritingThread() { this->initIfNeeded(); #if REAL_READER_WRITER_LOCK pthread_mutex_lock(&fMutex); pthread_t thisThread = pthread_self(); // wait as long as there is another writer or any readers on a different thread while ( writerThreadIsAnyThreadBut(thisThread) || readerThreadSetContainsAnotherThread(thisThread) ) { pthread_cond_wait(&fLockFree, &fMutex); } writerThreadRetain(thisThread); pthread_mutex_unlock(&fMutex); #else pthread_mutex_lock(&fMutex); #endif } void RecursiveReaderWriterLock::unlockForSingleWritingThread() { this->initIfNeeded(); #if REAL_READER_WRITER_LOCK pthread_mutex_lock(&fMutex); if ( writerThreadRelease(pthread_self()) ) { pthread_cond_broadcast(&fLockFree); } pthread_mutex_unlock(&fMutex); #else pthread_mutex_unlock(&fMutex); #endif } void RecursiveReaderWriterLock::lockForMultipleReadingThreads() { this->initIfNeeded(); #if REAL_READER_WRITER_LOCK pthread_mutex_lock(&fMutex); pthread_t thisThread = pthread_self(); // wait as long as there is a writer on another thread or too many readers already while ( writerThreadIsAnyThreadBut(thisThread) || !readerThreadSetRetain(thisThread) ) { pthread_cond_wait(&fLockFree, &fMutex); } pthread_mutex_unlock(&fMutex); #else pthread_mutex_lock(&fMutex); #endif } void RecursiveReaderWriterLock::unlockForMultipleReadingThreads() { this->initIfNeeded(); #if REAL_READER_WRITER_LOCK pthread_mutex_lock(&fMutex); if ( readerThreadSetRelease(pthread_self()) ) { pthread_cond_broadcast(&fLockFree); } pthread_mutex_unlock(&fMutex); #else pthread_mutex_unlock(&fMutex); #endif } #if REAL_READER_WRITER_LOCK bool RecursiveReaderWriterLock::writerThreadIsAnyThreadBut(pthread_t thread) { return ( (fWriterThread.fThread != NULL) && (fWriterThread.fThread != thread) ); } void RecursiveReaderWriterLock::writerThreadRetain(pthread_t thread) { ++fWriterThread.fCount; } bool RecursiveReaderWriterLock::writerThreadRelease(pthread_t thread) { return ( --fWriterThread.fCount == 0 ); } bool RecursiveReaderWriterLock::readerThreadSetRetain(pthread_t thread) { // if thread is already in set, bump its count for (int i=0; i < kMaxReaderThreads; ++i) { if ( fReaderThreads[i].fThread == thread ) { ++fReaderThreads[i].fCount; return true; } } // find empty slot in set for (int i=0; i < kMaxReaderThreads; ++i) { if ( fReaderThreads[i].fThread == NULL ) { fReaderThreads[i].fThread = thread; fReaderThreads[i].fCount = 1; return true; } } // all reader slots full return false; } bool RecursiveReaderWriterLock::readerThreadSetRelease(pthread_t thread) { for (int i=0; i < kMaxReaderThreads; ++i) { if ( fReaderThreads[i].fThread == thread ) { if ( --fReaderThreads[i].fCount == 0 ) { fReaderThreads[i].fThread = NULL; return true; } return false; } } // should never get here return false; } bool RecursiveReaderWriterLock::readerThreadSetContainsAnotherThread(pthread_t thread) { for (int i=0; i < kMaxReaderThreads; ++i) { if ( (fReaderThreads[i].fThread != NULL) && (fReaderThreads[i].fThread != thread) ) return true; } return false; } #endif // dyld's global reader/writer lock static RecursiveReaderWriterLock sLock; LockReaderHelper::LockReaderHelper() { sLock.lockForMultipleReadingThreads(); } LockReaderHelper::~LockReaderHelper() { sLock.unlockForMultipleReadingThreads(); } LockWriterHelper::LockWriterHelper() { sLock.lockForSingleWritingThread(); } LockWriterHelper::~LockWriterHelper() { sLock.unlockForSingleWritingThread(); } // needed by lazy binding void lockForLazyBinding() { sLock.lockForMultipleReadingThreads(); } void unlockForLazyBinding() { sLock.unlockForMultipleReadingThreads(); } |