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