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 1744984 : ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
27 : sal_Int32 nRequestMode )
28 1744984 : : rMutex( rMutexP )
29 : {
30 : // don't do anything until a pending write completed (or another
31 : // ReadWriteGuard leaves the ctor phase)
32 1744984 : ::osl::MutexGuard aGuard( rMutex.pWriteMutex );
33 1744984 : nMode = nRequestMode;
34 1744984 : if ( nMode & ReadWriteGuardMode::nWrite )
35 : {
36 6284 : rMutex.pWriteMutex->acquire();
37 : // wait for any read to complete
38 : // TODO: set up a waiting thread instead of a loop
39 6284 : sal_Bool bWait = sal_True;
40 6284 : do
41 : {
42 6284 : rMutex.pMutex->acquire();
43 6284 : bWait = (rMutex.nReadCount != 0);
44 6284 : if ( nMode & ReadWriteGuardMode::nCriticalChange )
45 6284 : bWait |= (rMutex.nBlockCriticalCount != 0);
46 6284 : rMutex.pMutex->release();
47 : } while ( bWait );
48 : }
49 1738700 : else if ( nMode & ReadWriteGuardMode::nBlockCritical )
50 : {
51 21446 : rMutex.pMutex->acquire();
52 21446 : ++rMutex.nBlockCriticalCount;
53 21446 : rMutex.pMutex->release();
54 : }
55 : else
56 : {
57 1717254 : rMutex.pMutex->acquire();
58 1717254 : ++rMutex.nReadCount;
59 1717254 : rMutex.pMutex->release();
60 1744984 : }
61 1744984 : }
62 :
63 :
64 1744984 : ReadWriteGuard::~ReadWriteGuard()
65 : {
66 1744984 : if ( nMode & ReadWriteGuardMode::nWrite )
67 25015 : rMutex.pWriteMutex->release();
68 1719969 : else if ( nMode & ReadWriteGuardMode::nBlockCritical )
69 : {
70 21446 : rMutex.pMutex->acquire();
71 21446 : --rMutex.nBlockCriticalCount;
72 21446 : rMutex.pMutex->release();
73 : }
74 : else
75 : {
76 1698523 : rMutex.pMutex->acquire();
77 1698523 : --rMutex.nReadCount;
78 1698523 : rMutex.pMutex->release();
79 : }
80 1744984 : }
81 :
82 :
83 18731 : void ReadWriteGuard::changeReadToWrite()
84 : {
85 18731 : sal_Bool bOk = !(nMode & (ReadWriteGuardMode::nWrite | ReadWriteGuardMode::nBlockCritical));
86 : DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
87 18731 : 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 18731 : rMutex.pMutex->acquire();
93 18731 : --rMutex.nReadCount;
94 18731 : rMutex.pMutex->release();
95 :
96 18731 : rMutex.pWriteMutex->acquire();
97 18731 : nMode |= ReadWriteGuardMode::nWrite;
98 : // wait for any other read to complete
99 : // TODO: set up a waiting thread instead of a loop
100 18731 : sal_Bool bWait = sal_True;
101 18731 : do
102 : {
103 18731 : rMutex.pMutex->acquire();
104 18731 : bWait = (rMutex.nReadCount != 0);
105 18731 : rMutex.pMutex->release();
106 : } while ( bWait );
107 : }
108 18731 : }
109 :
110 : } // namespace utl
111 :
112 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|