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