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 : #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
21 : #define __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
22 :
23 : #include <osl/interlck.h>
24 : #include <osl/mutex.hxx>
25 : #include <fwidllapi.h>
26 :
27 : namespace framework
28 : {
29 :
30 : class FWI_DLLPUBLIC ShareableMutex
31 : {
32 : public:
33 : ShareableMutex();
34 : ShareableMutex( const ShareableMutex& rShareableMutex );
35 : const ShareableMutex& operator=( const ShareableMutex& rShareableMutex );
36 :
37 : ~ShareableMutex();
38 :
39 : void acquire();
40 : void release();
41 :
42 : private:
43 0 : struct MutexRef
44 : {
45 0 : MutexRef() : m_refCount(0) {}
46 0 : void acquire()
47 : {
48 0 : osl_atomic_increment( &m_refCount );
49 0 : }
50 :
51 0 : void release()
52 : {
53 0 : if ( osl_atomic_decrement( &m_refCount ) == 0 )
54 0 : delete this;
55 0 : }
56 :
57 : oslInterlockedCount m_refCount;
58 : osl::Mutex m_oslMutex;
59 : };
60 :
61 : MutexRef* pMutexRef;
62 : };
63 :
64 : class ShareGuard
65 : {
66 : public:
67 0 : ShareGuard( ShareableMutex& rShareMutex ) :
68 0 : m_rShareMutex( rShareMutex )
69 : {
70 0 : m_rShareMutex.acquire();
71 0 : }
72 :
73 0 : ~ShareGuard()
74 : {
75 0 : m_rShareMutex.release();
76 0 : }
77 :
78 : private:
79 : ShareGuard();
80 : ShareGuard& operator=( const ShareGuard& );
81 :
82 : ShareableMutex& m_rShareMutex;
83 : };
84 :
85 : }
86 :
87 : #endif // #ifndef __FRAMEWORK_HELPER_SHAREABLEMUTEX_HXX_
88 :
89 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|