Line data Source code
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 <string.h>
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 :
24 : #include "osl/module.hxx"
25 : #include "tools/solarmutex.hxx"
26 :
27 : #include "generic/geninst.h"
28 :
29 : // SalYieldMutex
30 :
31 165 : SalYieldMutex::SalYieldMutex()
32 : {
33 165 : mnCount = 0;
34 165 : mnThreadId = 0;
35 165 : ::tools::SolarMutex::SetSolarMutex( this );
36 165 : }
37 :
38 273 : SalYieldMutex::~SalYieldMutex()
39 : {
40 91 : ::tools::SolarMutex::SetSolarMutex( NULL );
41 182 : }
42 :
43 13261690 : void SalYieldMutex::acquire()
44 : {
45 13261690 : m_mutex.acquire();
46 13261691 : mnThreadId = osl::Thread::getCurrentIdentifier();
47 13261691 : mnCount++;
48 13261691 : }
49 :
50 13261617 : void SalYieldMutex::release()
51 : {
52 : OSL_ENSURE(mnCount > 0, "SalYieldMutex::release() called with zero count");
53 13261617 : if ( mnThreadId == osl::Thread::getCurrentIdentifier() )
54 : {
55 13261617 : if ( mnCount == 1 )
56 475811 : mnThreadId = 0;
57 13261617 : mnCount--;
58 : }
59 13261617 : m_mutex.release();
60 13261613 : }
61 :
62 0 : bool SalYieldMutex::tryToAcquire()
63 : {
64 0 : if ( m_mutex.tryToAcquire() )
65 : {
66 0 : mnThreadId = osl::Thread::getCurrentIdentifier();
67 0 : mnCount++;
68 0 : return true;
69 : }
70 : else
71 0 : return false;
72 : }
73 :
74 12798798 : comphelper::SolarMutex* SalGenericInstance::GetYieldMutex()
75 : {
76 12798798 : return mpSalYieldMutex;
77 : }
78 :
79 256722 : sal_uLong SalGenericInstance::ReleaseYieldMutex()
80 : {
81 256722 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
82 513444 : if ( pYieldMutex->GetThreadId() ==
83 256722 : osl::Thread::getCurrentIdentifier() )
84 : {
85 256722 : sal_uLong nCount = pYieldMutex->GetAcquireCount();
86 256722 : sal_uLong n = nCount;
87 786326 : while ( n )
88 : {
89 272882 : pYieldMutex->release();
90 272882 : n--;
91 : }
92 :
93 256722 : return nCount;
94 : }
95 : else
96 0 : return 0;
97 : }
98 :
99 256796 : void SalGenericInstance::AcquireYieldMutex( sal_uLong nCount )
100 : {
101 256796 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
102 786548 : while ( nCount )
103 : {
104 272956 : pYieldMutex->acquire();
105 272956 : nCount--;
106 : }
107 256796 : }
108 :
109 0 : bool SalGenericInstance::CheckYieldMutex()
110 : {
111 0 : bool bRet = true;
112 :
113 0 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
114 0 : if ( pYieldMutex->GetThreadId() != osl::Thread::getCurrentIdentifier() )
115 : {
116 : SAL_WARN_IF( pYieldMutex->GetThreadId() != osl::Thread::getCurrentIdentifier(), "vcl", "CheckYieldMutex: " << pYieldMutex->GetThreadId() << "!=" << osl::Thread::getCurrentIdentifier() );
117 0 : bRet = false;
118 : }
119 :
120 0 : return bRet;
121 : }
122 :
123 182 : SalGenericInstance::~SalGenericInstance()
124 : {
125 91 : ::tools::SolarMutex::SetSolarMutex( 0 );
126 91 : delete mpSalYieldMutex;
127 607 : }
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|