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 : // -------------------------------------------------------------------------
30 : //
31 : // SalYieldMutex
32 : //
33 : // -------------------------------------------------------------------------
34 :
35 98 : SalYieldMutex::SalYieldMutex()
36 : {
37 98 : mnCount = 0;
38 98 : mnThreadId = 0;
39 98 : ::tools::SolarMutex::SetSolarMutex( this );
40 98 : }
41 :
42 24 : SalYieldMutex::~SalYieldMutex()
43 : {
44 8 : ::tools::SolarMutex::SetSolarMutex( NULL );
45 16 : }
46 :
47 2068694 : void SalYieldMutex::acquire()
48 : {
49 2068694 : SolarMutexObject::acquire();
50 2068694 : mnThreadId = osl::Thread::getCurrentIdentifier();
51 2068694 : mnCount++;
52 2068694 : }
53 :
54 2068604 : void SalYieldMutex::release()
55 : {
56 : OSL_ENSURE(mnCount > 0, "SalYieldMutex::release() called with zero count");
57 2068604 : if ( mnThreadId == osl::Thread::getCurrentIdentifier() )
58 : {
59 2068604 : if ( mnCount == 1 )
60 4133 : mnThreadId = 0;
61 2068604 : mnCount--;
62 : }
63 2068604 : SolarMutexObject::release();
64 2068604 : }
65 :
66 0 : sal_Bool SalYieldMutex::tryToAcquire()
67 : {
68 0 : if ( SolarMutexObject::tryToAcquire() )
69 : {
70 0 : mnThreadId = osl::Thread::getCurrentIdentifier();
71 0 : mnCount++;
72 0 : return sal_True;
73 : }
74 : else
75 0 : return sal_False;
76 : }
77 :
78 1132089 : osl::SolarMutex* SalGenericInstance::GetYieldMutex()
79 : {
80 1132089 : return mpSalYieldMutex;
81 : }
82 :
83 3210 : sal_uLong SalGenericInstance::ReleaseYieldMutex()
84 : {
85 3210 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
86 6420 : if ( pYieldMutex->GetThreadId() ==
87 3210 : osl::Thread::getCurrentIdentifier() )
88 : {
89 3210 : sal_uLong nCount = pYieldMutex->GetAcquireCount();
90 3210 : sal_uLong n = nCount;
91 15409 : while ( n )
92 : {
93 8989 : pYieldMutex->release();
94 8989 : n--;
95 : }
96 :
97 3210 : return nCount;
98 : }
99 : else
100 0 : return 0;
101 : }
102 :
103 3300 : void SalGenericInstance::AcquireYieldMutex( sal_uLong nCount )
104 : {
105 3300 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
106 15679 : while ( nCount )
107 : {
108 9079 : pYieldMutex->acquire();
109 9079 : nCount--;
110 : }
111 3300 : }
112 :
113 0 : bool SalGenericInstance::CheckYieldMutex()
114 : {
115 0 : bool bRet = true;
116 :
117 0 : SalYieldMutex* pYieldMutex = mpSalYieldMutex;
118 0 : if ( pYieldMutex->GetThreadId() != osl::Thread::getCurrentIdentifier() )
119 0 : bRet = false;
120 :
121 0 : return bRet;
122 : }
123 :
124 16 : SalGenericInstance::~SalGenericInstance()
125 : {
126 8 : ::tools::SolarMutex::SetSolarMutex( 0 );
127 8 : delete mpSalYieldMutex;
128 8 : }
|