1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
25 #define __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
26 
27 #include <osl/interlck.h>
28 #include <osl/mutex.hxx>
29 #include <fwidllapi.h>
30 
31 namespace framework
32 {
33 
34 class FWI_DLLPUBLIC ShareableMutex
35 {
36     public:
37         ShareableMutex();
38         ShareableMutex( const ShareableMutex& rShareableMutex );
39         const ShareableMutex& operator=( const ShareableMutex& rShareableMutex );
40 
41         ~ShareableMutex();
42 
43         void acquire();
44         void release();
45         ::osl::Mutex& getShareableOslMutex();
46 
47     private:
48         struct MutexRef
49         {
MutexRefframework::ShareableMutex::MutexRef50             MutexRef() : m_refCount(0) {}
acquireframework::ShareableMutex::MutexRef51             void acquire()
52             {
53                 osl_incrementInterlockedCount( &m_refCount );
54             }
55 
releaseframework::ShareableMutex::MutexRef56             void release()
57             {
58                 if ( osl_decrementInterlockedCount( &m_refCount ) == 0 )
59                     delete this;
60             }
61 
62             oslInterlockedCount m_refCount;
63             osl::Mutex          m_oslMutex;
64         };
65 
66         MutexRef* pMutexRef;
67 };
68 
69 class ShareGuard
70 {
71     public:
ShareGuard(ShareableMutex & rShareMutex)72         ShareGuard( ShareableMutex& rShareMutex ) :
73             m_rShareMutex( rShareMutex )
74         {
75             m_rShareMutex.acquire();
76         }
77 
~ShareGuard()78         ~ShareGuard()
79         {
80             m_rShareMutex.release();
81         }
82 
83     private:
84         ShareGuard();
85         ShareGuard& operator=( const ShareGuard& );
86 
87         ShareableMutex& m_rShareMutex;
88 };
89 
90 }
91 
92 #endif // #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
93