/src/libreoffice/include/comphelper/solarmutex.hxx
Line | Count | Source |
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 INCLUDED_COMPHELPER_SOLARMUTEX_HXX |
21 | | #define INCLUDED_COMPHELPER_SOLARMUTEX_HXX |
22 | | |
23 | | #include <sal/config.h> |
24 | | |
25 | | #include <assert.h> |
26 | | #include <atomic> |
27 | | #include <thread> |
28 | | |
29 | | #include <osl/mutex.hxx> |
30 | | #include <comphelper/comphelperdllapi.h> |
31 | | |
32 | | namespace comphelper { |
33 | | |
34 | | |
35 | | /** |
36 | | * SolarMutex, needed for VCL's Application::GetSolarMutex(). |
37 | | * |
38 | | * The SolarMutex is the one big recursive code lock used |
39 | | * to protect the vast majority of the LibreOffice code-base, |
40 | | * in particular anything that is graphical and the cores of |
41 | | * the applications. |
42 | | * |
43 | | * Treat this as a singleton, as its constructor sets a global |
44 | | * pointing at itself. |
45 | | */ |
46 | | class COMPHELPER_DLLPUBLIC SolarMutex { |
47 | | public: |
48 | | typedef void (*BeforeReleaseHandler) (); |
49 | | |
50 | | SolarMutex(); |
51 | | virtual ~SolarMutex(); |
52 | | |
53 | | void SetBeforeReleaseHandler( const BeforeReleaseHandler& rLink ) |
54 | 0 | { m_aBeforeReleaseHandler = rLink; } |
55 | | |
56 | | void acquire( sal_uInt32 nLockCount = 1 ); |
57 | | sal_uInt32 release( bool bUnlockAll = false ); |
58 | | |
59 | | virtual bool tryToAcquire(); |
60 | | |
61 | | // returns true, if the mutex is owned by the current thread |
62 | | virtual bool IsCurrentThread() const; |
63 | | |
64 | | /// Help components to get the SolarMutex easily. |
65 | | static SolarMutex *get(); |
66 | | |
67 | | protected: |
68 | | virtual sal_uInt32 doRelease( bool bUnlockAll ); |
69 | | virtual void doAcquire( sal_uInt32 nLockCount ); |
70 | | |
71 | | osl::Mutex m_aMutex; |
72 | | sal_uInt32 m_nCount; |
73 | | |
74 | | private: |
75 | | std::atomic<std::thread::id> m_nThreadId; |
76 | | |
77 | | SolarMutex(const SolarMutex&) = delete; |
78 | | SolarMutex& operator=(const SolarMutex&) = delete; |
79 | | |
80 | | BeforeReleaseHandler m_aBeforeReleaseHandler; |
81 | | }; |
82 | | |
83 | | inline void SolarMutex::acquire( sal_uInt32 nLockCount ) |
84 | 349M | { |
85 | 349M | assert( nLockCount > 0 ); |
86 | 349M | doAcquire( nLockCount ); |
87 | 349M | } |
88 | | |
89 | | inline sal_uInt32 SolarMutex::release( bool bUnlockAll ) |
90 | 349M | { |
91 | 349M | return doRelease( bUnlockAll ); |
92 | 349M | } |
93 | | |
94 | | } |
95 | | |
96 | | #endif // INCLUDED_COMPHELPER_SOLARMUTEX_HXX |
97 | | |
98 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |