update dev300-m58
[ooovba.git] / unotools / source / i18n / readwritemutexguard.cxx
blob6e596896ceb09849e5b4e9ceb9aeb3ebba62da02
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: readwritemutexguard.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
33 #include "unotools/readwritemutexguard.hxx"
34 #include <tools/debug.hxx>
37 namespace utl {
39 ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
40 sal_Int32 nRequestMode )
41 : rMutex( rMutexP )
43 // don't do anything until a pending write completed (or another
44 // ReadWriteGuard leaves the ctor phase)
45 ::osl::MutexGuard aGuard( rMutex.pWriteMutex );
46 nMode = nRequestMode;
47 if ( nMode & ReadWriteGuardMode::nWrite )
49 rMutex.pWriteMutex->acquire();
50 // wait for any read to complete
51 // TODO: set up a waiting thread instead of a loop
52 sal_Bool bWait = sal_True;
55 rMutex.pMutex->acquire();
56 bWait = (rMutex.nReadCount != 0);
57 if ( nMode & ReadWriteGuardMode::nCriticalChange )
58 bWait |= (rMutex.nBlockCriticalCount != 0);
59 rMutex.pMutex->release();
60 } while ( bWait );
62 else if ( nMode & ReadWriteGuardMode::nBlockCritical )
64 rMutex.pMutex->acquire();
65 ++rMutex.nBlockCriticalCount;
66 rMutex.pMutex->release();
68 else
70 rMutex.pMutex->acquire();
71 ++rMutex.nReadCount;
72 rMutex.pMutex->release();
77 ReadWriteGuard::~ReadWriteGuard()
79 if ( nMode & ReadWriteGuardMode::nWrite )
80 rMutex.pWriteMutex->release();
81 else if ( nMode & ReadWriteGuardMode::nBlockCritical )
83 rMutex.pMutex->acquire();
84 --rMutex.nBlockCriticalCount;
85 rMutex.pMutex->release();
87 else
89 rMutex.pMutex->acquire();
90 --rMutex.nReadCount;
91 rMutex.pMutex->release();
96 void ReadWriteGuard::changeReadToWrite()
98 sal_Bool bOk = !(nMode & (ReadWriteGuardMode::nWrite | ReadWriteGuardMode::nBlockCritical));
99 DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
100 if ( bOk )
102 // MUST release read before acquiring write mutex or dead lock would
103 // occur if there was a write in another thread waiting for this read
104 // to complete.
105 rMutex.pMutex->acquire();
106 --rMutex.nReadCount;
107 rMutex.pMutex->release();
109 rMutex.pWriteMutex->acquire();
110 nMode |= ReadWriteGuardMode::nWrite;
111 // wait for any other read to complete
112 // TODO: set up a waiting thread instead of a loop
113 sal_Bool bWait = sal_True;
116 rMutex.pMutex->acquire();
117 bWait = (rMutex.nReadCount != 0);
118 rMutex.pMutex->release();
119 } while ( bWait );
123 } // namespace utl