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