1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX 25 #define INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX 26 27 #include <osl/mutex.hxx> 28 29 namespace utl { 30 31 class ReadWriteGuard; 32 class ReadWriteMutex 33 { 34 friend class ReadWriteGuard; 35 36 sal_uInt32 nReadCount; 37 sal_uInt32 nBlockCriticalCount; 38 ::osl::Mutex* pMutex; 39 ::osl::Mutex* pWriteMutex; 40 41 public: ReadWriteMutex()42 ReadWriteMutex() 43 : nReadCount(0) 44 , nBlockCriticalCount(0) 45 , pMutex( new ::osl::Mutex ) 46 , pWriteMutex( new ::osl::Mutex ) 47 {} ~ReadWriteMutex()48 ~ReadWriteMutex() 49 { 50 delete pMutex; 51 delete pWriteMutex; 52 } 53 }; 54 55 56 namespace ReadWriteGuardMode { 57 const sal_Int32 nWrite = 0x01; 58 const sal_Int32 nCriticalChange = 0x02 | nWrite; 59 const sal_Int32 nBlockCritical = 0x04; // only a block, not a read, exclusive flag! 60 } 61 62 /** Enable multiple threads to read simultaneously, but a write blocks all 63 other reads and writes, and a read blocks any write. 64 Used in I18N wrappers to be able to maintain a single instance of a wrapper 65 for the standard Office locale. 66 NEVER construct a writing guard if there is already a reading guard in the 67 same context, the following will dead lock EVEN IN THE SAME THREAD! 68 void foo() 69 { 70 ReadWriteGuard aGuard1( aMutex ); 71 bar(); 72 } 73 void bar() 74 { 75 // waits forever for aGuard1 76 ReadWriteGuard aGuard2( aMutex, ReadWriteGuardMode::nWrite ); 77 } 78 */ 79 class ReadWriteGuard 80 { 81 ReadWriteMutex& rMutex; 82 sal_Int32 nMode; 83 public: 84 ReadWriteGuard( 85 ReadWriteMutex& rMutex, 86 sal_Int32 nRequestMode = 0 // read only 87 ); 88 ~ReadWriteGuard(); 89 90 /** Be careful with this, it does wait for ANY read to complete. 91 The following will dead lock EVEN IN THE SAME THREAD! 92 void foo() 93 { 94 ReadWriteGuard aGuard1( aMutex ); 95 bar(); 96 } 97 void bar() 98 { 99 ReadWriteGuard aGuard2( aMutex ); 100 aGuard2.changeReadToWrite(); // waits forever for aGuard1 101 } 102 */ 103 void changeReadToWrite(); 104 }; 105 106 } // namespace utl 107 108 #endif // INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX 109 110