LibreOffice Module comphelper (master)  1
solarmutex.hxx
Go to the documentation of this file.
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 
28 #include <osl/thread.h>
29 #include <osl/mutex.hxx>
31 
32 namespace comphelper {
33 
34 
47 public:
48  typedef void (*BeforeReleaseHandler) ();
49 
50  SolarMutex();
51  virtual ~SolarMutex();
52 
53  void SetBeforeReleaseHandler( const BeforeReleaseHandler& rLink )
54  { 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 
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<oslThreadIdentifier> 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 {
85  assert( nLockCount > 0 );
86  doAcquire( nLockCount );
87 }
88 
89 inline sal_uInt32 SolarMutex::release( bool bUnlockAll )
90 {
91  return doRelease( bUnlockAll );
92 }
93 
94 }
95 
96 #endif // INCLUDED_COMPHELPER_SOLARMUTEX_HXX
97 
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BeforeReleaseHandler m_aBeforeReleaseHandler
Definition: solarmutex.hxx:80
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
void acquire(sal_uInt32 nLockCount=1)
Definition: solarmutex.hxx:83
sal_uInt32 release(bool bUnlockAll=false)
Definition: solarmutex.hxx:89
virtual void doAcquire(sal_uInt32 nLockCount)
Definition: solarmutex.cxx:53
std::atomic< oslThreadIdentifier > m_nThreadId
Definition: solarmutex.hxx:75
#define COMPHELPER_DLLPUBLIC
SolarMutex, needed for VCL's Application::GetSolarMutex().
Definition: solarmutex.hxx:46
virtual sal_uInt32 doRelease(bool bUnlockAll)
Definition: solarmutex.cxx:61
void SetBeforeReleaseHandler(const BeforeReleaseHandler &rLink)
Definition: solarmutex.hxx:53
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo