LibreOffice Module comphelper (master)  1
solarmutex.cxx
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 #include <sal/config.h>
21 
23 #include <osl/thread.hxx>
24 
25 #include <assert.h>
26 #include <cstdlib>
27 
28 namespace comphelper {
29 
30 namespace {
31  static SolarMutex* g_pSolarMutex = nullptr;
32 }
33 
35 {
36  return g_pSolarMutex;
37 }
38 
40  : m_nCount( 0 )
41  , m_nThreadId( 0 )
42  , m_aBeforeReleaseHandler( nullptr )
43 {
44  assert(!g_pSolarMutex);
45  g_pSolarMutex = this;
46 }
47 
49 {
50  g_pSolarMutex = nullptr;
51 }
52 
53 void SolarMutex::doAcquire( const sal_uInt32 nLockCount )
54 {
55  for ( sal_uInt32 n = nLockCount; n ; --n )
56  m_aMutex.acquire();
57  m_nThreadId = osl::Thread::getCurrentIdentifier();
58  m_nCount += nLockCount;
59 }
60 
61 sal_uInt32 SolarMutex::doRelease( bool bUnlockAll )
62 {
63  if ( !IsCurrentThread() )
64  std::abort();
65  if ( m_nCount == 0 )
66  std::abort();
67 
68  const sal_uInt32 nCount = bUnlockAll ? m_nCount : 1;
69  m_nCount -= nCount;
70 
71  if ( 0 == m_nCount )
72  {
75  m_nThreadId = 0;
76  }
77 
78  for ( sal_uInt32 n = nCount ; n ; --n )
79  m_aMutex.release();
80 
81  return nCount;
82 }
83 
85 {
86  return m_nThreadId == osl::Thread::getCurrentIdentifier();
87 }
88 
90 {
91  if ( m_aMutex.tryToAcquire() )
92  {
93  m_nThreadId = osl::Thread::getCurrentIdentifier();
94  m_nCount++;
95  return true;
96  }
97  else
98  return false;
99 }
100 
101 } // namespace comphelper
102 
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BeforeReleaseHandler m_aBeforeReleaseHandler
Definition: solarmutex.hxx:80
virtual void doAcquire(sal_uInt32 nLockCount)
Definition: solarmutex.cxx:53
std::atomic< oslThreadIdentifier > m_nThreadId
Definition: solarmutex.hxx:75
static SolarMutex * get()
Help components to get the SolarMutex easily.
Definition: solarmutex.cxx:34
virtual bool tryToAcquire()
Definition: solarmutex.cxx:89
virtual bool IsCurrentThread() const
Definition: solarmutex.cxx:84
SolarMutex, needed for VCL's Application::GetSolarMutex().
Definition: solarmutex.hxx:46
virtual sal_uInt32 doRelease(bool bUnlockAll)
Definition: solarmutex.cxx:61