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>
25 #include <osl/thread.hxx>
26 #include <osl/mutex.hxx>
27 #include <comphelper/comphelperdllapi.h>
29 namespace comphelper
{
33 * SolarMutex, needed for VCL's Application::GetSolarMutex().
35 * The SolarMutex is the one big recursive code lock used
36 * to protect the vast majority of the LibreOffice code-base,
37 * in particular anything that is graphical and the cores of
40 * Treat this as a singleton, as its constructor sets a global
43 class COMPHELPER_DLLPUBLIC SolarMutex
{
45 typedef void (*BeforeReleaseHandler
) ();
47 void SetBeforeReleaseHandler( const BeforeReleaseHandler
& rLink
)
48 { m_aBeforeReleaseHandler
= rLink
; }
50 void acquire( sal_uInt32 nLockCount
= 1 );
51 sal_uInt32
release( bool bUnlockAll
= false );
53 virtual bool tryToAcquire();
55 // returns true, if the mutex is owned by the current thread
56 virtual bool IsCurrentThread() const;
58 /// Help components to get the SolarMutex easily.
59 static SolarMutex
*get();
63 virtual ~SolarMutex();
65 virtual sal_uInt32
doRelease( bool bUnlockAll
);
66 virtual void doAcquire( sal_uInt32 nLockCount
);
70 oslThreadIdentifier m_nThreadId
;
73 SolarMutex(const SolarMutex
&) = delete;
74 SolarMutex
& operator=(const SolarMutex
&) = delete;
76 BeforeReleaseHandler m_aBeforeReleaseHandler
;
79 inline void SolarMutex::acquire( sal_uInt32 nLockCount
)
81 assert( nLockCount
> 0 );
82 doAcquire( nLockCount
);
85 inline sal_uInt32
SolarMutex::release( bool bUnlockAll
)
87 return doRelease( bUnlockAll
);
92 #endif // INCLUDED_COMPHELPER_SOLARMUTEX_HXX
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */