Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
30 : : #define __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
31 : :
32 : : #include <osl/interlck.h>
33 : : #include <osl/mutex.hxx>
34 : : #include <fwidllapi.h>
35 : :
36 : : namespace framework
37 : : {
38 : :
39 : : class FWI_DLLPUBLIC ShareableMutex
40 : : {
41 : : public:
42 : : ShareableMutex();
43 : : ShareableMutex( const ShareableMutex& rShareableMutex );
44 : : const ShareableMutex& operator=( const ShareableMutex& rShareableMutex );
45 : :
46 : : ~ShareableMutex();
47 : :
48 : : void acquire();
49 : : void release();
50 : :
51 : : private:
52 : 424 : struct MutexRef
53 : : {
54 : 424 : MutexRef() : m_refCount(0) {}
55 : 4090 : void acquire()
56 : : {
57 : 4090 : osl_incrementInterlockedCount( &m_refCount );
58 : 4090 : }
59 : :
60 : 4090 : void release()
61 : : {
62 [ + + ]: 4090 : if ( osl_decrementInterlockedCount( &m_refCount ) == 0 )
63 [ + - ]: 424 : delete this;
64 : 4090 : }
65 : :
66 : : oslInterlockedCount m_refCount;
67 : : osl::Mutex m_oslMutex;
68 : : };
69 : :
70 : : MutexRef* pMutexRef;
71 : : };
72 : :
73 : : class ShareGuard
74 : : {
75 : : public:
76 : 894347 : ShareGuard( ShareableMutex& rShareMutex ) :
77 : 894347 : m_rShareMutex( rShareMutex )
78 : : {
79 : 894347 : m_rShareMutex.acquire();
80 : 894347 : }
81 : :
82 : 894347 : ~ShareGuard()
83 : : {
84 : 894347 : m_rShareMutex.release();
85 : 894347 : }
86 : :
87 : : private:
88 : : ShareGuard();
89 : : ShareGuard& operator=( const ShareGuard& );
90 : :
91 : : ShareableMutex& m_rShareMutex;
92 : : };
93 : :
94 : : }
95 : :
96 : : #endif // #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
97 : :
98 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|