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