Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "unotools/readwritemutexguard.hxx"
21 : : #include <tools/debug.hxx>
22 : :
23 : :
24 : : namespace utl {
25 : :
26 : 2816176 : ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
27 : : sal_Int32 nRequestMode )
28 : 2816176 : : rMutex( rMutexP )
29 : : {
30 : : // don't do anything until a pending write completed (or another
31 : : // ReadWriteGuard leaves the ctor phase)
32 [ + - ]: 2816176 : ::osl::MutexGuard aGuard( rMutex.pWriteMutex );
33 : 2816176 : nMode = nRequestMode;
34 [ + + ]: 2816176 : if ( nMode & ReadWriteGuardMode::nWrite )
35 : : {
36 [ + - ]: 32434 : rMutex.pWriteMutex->acquire();
37 : : // wait for any read to complete
38 : : // TODO: set up a waiting thread instead of a loop
39 : 32434 : sal_Bool bWait = sal_True;
40 [ - + ]: 32434 : do
41 : : {
42 [ + - ]: 32434 : rMutex.pMutex->acquire();
43 : 32434 : bWait = (rMutex.nReadCount != 0);
44 [ + - ]: 32434 : if ( nMode & ReadWriteGuardMode::nCriticalChange )
45 : 32434 : bWait |= (rMutex.nBlockCriticalCount != 0);
46 [ + - ]: 32434 : rMutex.pMutex->release();
47 : : } while ( bWait );
48 : : }
49 [ + + ]: 2783742 : else if ( nMode & ReadWriteGuardMode::nBlockCritical )
50 : : {
51 [ + - ]: 39033 : rMutex.pMutex->acquire();
52 : 39033 : ++rMutex.nBlockCriticalCount;
53 [ + - ]: 39033 : rMutex.pMutex->release();
54 : : }
55 : : else
56 : : {
57 [ + - ]: 2744709 : rMutex.pMutex->acquire();
58 : 2744709 : ++rMutex.nReadCount;
59 [ + - ]: 2744709 : rMutex.pMutex->release();
60 [ + - ]: 2816176 : }
61 : 2816176 : }
62 : :
63 : :
64 : 2816176 : ReadWriteGuard::~ReadWriteGuard()
65 : : {
66 [ + + ]: 2816176 : if ( nMode & ReadWriteGuardMode::nWrite )
67 : 60792 : rMutex.pWriteMutex->release();
68 [ + + ]: 2755384 : else if ( nMode & ReadWriteGuardMode::nBlockCritical )
69 : : {
70 : 39033 : rMutex.pMutex->acquire();
71 : 39033 : --rMutex.nBlockCriticalCount;
72 : 39033 : rMutex.pMutex->release();
73 : : }
74 : : else
75 : : {
76 : 2716351 : rMutex.pMutex->acquire();
77 : 2716351 : --rMutex.nReadCount;
78 : 2716351 : rMutex.pMutex->release();
79 : : }
80 : 2816176 : }
81 : :
82 : :
83 : 28358 : void ReadWriteGuard::changeReadToWrite()
84 : : {
85 : 28358 : sal_Bool bOk = !(nMode & (ReadWriteGuardMode::nWrite | ReadWriteGuardMode::nBlockCritical));
86 : : DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
87 [ + - ]: 28358 : if ( bOk )
88 : : {
89 : : // MUST release read before acquiring write mutex or dead lock would
90 : : // occur if there was a write in another thread waiting for this read
91 : : // to complete.
92 : 28358 : rMutex.pMutex->acquire();
93 : 28358 : --rMutex.nReadCount;
94 : 28358 : rMutex.pMutex->release();
95 : :
96 : 28358 : rMutex.pWriteMutex->acquire();
97 : 28358 : nMode |= ReadWriteGuardMode::nWrite;
98 : : // wait for any other read to complete
99 : : // TODO: set up a waiting thread instead of a loop
100 : 28358 : sal_Bool bWait = sal_True;
101 [ - + ]: 28358 : do
102 : : {
103 : 28358 : rMutex.pMutex->acquire();
104 : 28358 : bWait = (rMutex.nReadCount != 0);
105 : 28358 : rMutex.pMutex->release();
106 : : } while ( bWait );
107 : : }
108 : 28358 : }
109 : :
110 : : } // namespace utl
111 : :
112 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|