1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_COMPHELPER_SOLARMUTEX_HXX
21 #define INCLUDED_COMPHELPER_SOLARMUTEX_HXX
23 #include <sal/config.h>
29 #include <osl/mutex.hxx>
30 #include <comphelper/comphelperdllapi.h>
32 namespace comphelper
{
36 * SolarMutex, needed for VCL's Application::GetSolarMutex().
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
43 * Treat this as a singleton, as its constructor sets a global
46 class COMPHELPER_DLLPUBLIC SolarMutex
{
48 typedef void (*BeforeReleaseHandler
) ();
51 virtual ~SolarMutex();
53 void SetBeforeReleaseHandler( const BeforeReleaseHandler
& rLink
)
54 { m_aBeforeReleaseHandler
= rLink
; }
56 void acquire( sal_uInt32 nLockCount
= 1 );
57 sal_uInt32
release( bool bUnlockAll
= false );
59 virtual bool tryToAcquire();
61 // returns true, if the mutex is owned by the current thread
62 virtual bool IsCurrentThread() const;
64 /// Help components to get the SolarMutex easily.
65 static SolarMutex
*get();
68 virtual sal_uInt32
doRelease( bool bUnlockAll
);
69 virtual void doAcquire( sal_uInt32 nLockCount
);
75 std::atomic
<std::thread::id
> m_nThreadId
;
77 SolarMutex(const SolarMutex
&) = delete;
78 SolarMutex
& operator=(const SolarMutex
&) = delete;
80 BeforeReleaseHandler m_aBeforeReleaseHandler
;
83 inline void SolarMutex::acquire( sal_uInt32 nLockCount
)
85 assert( nLockCount
> 0 );
86 doAcquire( nLockCount
);
89 inline sal_uInt32
SolarMutex::release( bool bUnlockAll
)
91 return doRelease( bUnlockAll
);
96 #endif // INCLUDED_COMPHELPER_SOLARMUTEX_HXX
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */