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 1 : SalYieldMutex::SalYieldMutex()
32 : {
33 1 : mnCount = 0;
34 1 : mnThreadId = 0;
35 1 : ::tools::SolarMutex::SetSolarMutex( this );
36 1 : }
37 :
38 3 : SalYieldMutex::~SalYieldMutex()
39 : {
40 1 : ::tools::SolarMutex::SetSolarMutex( NULL );
41 2 : }
42 :
43 2 : void SalYieldMutex::acquire()
44 : {
45 2 : m_mutex.acquire();
46 2 : mnThreadId = osl::Thread::getCurrentIdentifier();
47 2 : mnCount++;
48 2 : }
49 :
50 2 : void SalYieldMutex::release()
51 : {
52 : OSL_ENSURE(mnCount > 0, "SalYieldMutex::release() called with zero count");
53 2 : if ( mnThreadId == osl::Thread::getCurrentIdentifier() )
54 : {
55 2 : if ( mnCount == 1 )
56 2 : mnThreadId = 0;
57 2 : mnCount--;
58 : }
59 2 : m_mutex.release();
60 2 : }
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 0 : comphelper::SolarMutex* SalGenericInstance::GetYieldMutex()
75 : {
76 0 : return mpSalYieldMutex;
77 : }
78 :
79 2 : sal_uLong SalGenericInstance::ReleaseYieldMutex()
80 : {
81 2 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
82 4 : if ( pYieldMutex->GetThreadId() ==
83 2 : osl::Thread::getCurrentIdentifier() )
84 : {
85 2 : sal_uLong nCount = pYieldMutex->GetAcquireCount();
86 2 : sal_uLong n = nCount;
87 6 : while ( n )
88 : {
89 2 : pYieldMutex->release();
90 2 : n--;
91 : }
92 :
93 2 : return nCount;
94 : }
95 : else
96 0 : return 0;
97 : }
98 :
99 2 : void SalGenericInstance::AcquireYieldMutex( sal_uLong nCount )
100 : {
101 2 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
102 6 : while ( nCount )
103 : {
104 2 : pYieldMutex->acquire();
105 2 : nCount--;
106 : }
107 2 : }
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 2 : SalGenericInstance::~SalGenericInstance()
124 : {
125 1 : ::tools::SolarMutex::SetSolarMutex( 0 );
126 1 : delete mpSalYieldMutex;
127 4 : }
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|