Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / generic / app / geninst.cxx
blobd73b72e2a6522969e5f7e87182c0c8b1f9350a30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <osl/module.hxx>
25 #include <tools/solarmutex.hxx>
26 #include <vcl/opengl/OpenGLContext.hxx>
28 #include "generic/geninst.h"
30 // SalYieldMutex
32 SalYieldMutex::SalYieldMutex()
34 mnCount = 0;
35 mnThreadId = 0;
36 ::tools::SolarMutex::SetSolarMutex( this );
39 SalYieldMutex::~SalYieldMutex()
41 ::tools::SolarMutex::SetSolarMutex( NULL );
44 void SalYieldMutex::acquire()
46 m_mutex.acquire();
47 mnThreadId = osl::Thread::getCurrentIdentifier();
48 mnCount++;
51 void SalYieldMutex::release()
53 OSL_ENSURE(mnCount > 0, "SalYieldMutex::release() called with zero count");
54 if ( mnThreadId == osl::Thread::getCurrentIdentifier() )
56 if ( mnCount == 1 )
58 OpenGLContext::prepareForYield();
59 mnThreadId = 0;
61 mnCount--;
63 m_mutex.release();
66 bool SalYieldMutex::tryToAcquire()
68 if ( m_mutex.tryToAcquire() )
70 mnThreadId = osl::Thread::getCurrentIdentifier();
71 mnCount++;
72 return true;
74 else
75 return false;
78 comphelper::SolarMutex* SalGenericInstance::GetYieldMutex()
80 return mpSalYieldMutex;
83 sal_uLong SalGenericInstance::ReleaseYieldMutex()
85 SalYieldMutex* pYieldMutex = mpSalYieldMutex;
86 if ( pYieldMutex->GetThreadId() ==
87 osl::Thread::getCurrentIdentifier() )
89 sal_uLong nCount = pYieldMutex->GetAcquireCount();
90 sal_uLong n = nCount;
91 while ( n )
93 pYieldMutex->release();
94 n--;
97 return nCount;
99 else
100 return 0;
103 void SalGenericInstance::AcquireYieldMutex( sal_uLong nCount )
105 SalYieldMutex* pYieldMutex = mpSalYieldMutex;
106 while ( nCount )
108 pYieldMutex->acquire();
109 nCount--;
113 bool SalGenericInstance::CheckYieldMutex()
115 bool bRet = true;
117 SalYieldMutex* pYieldMutex = mpSalYieldMutex;
118 if ( pYieldMutex->GetThreadId() != osl::Thread::getCurrentIdentifier() )
120 SAL_WARN("vcl", "CheckYieldMutex: " << pYieldMutex->GetThreadId() << "!=" << osl::Thread::getCurrentIdentifier() );
121 bRet = false;
124 return bRet;
127 SalGenericInstance::~SalGenericInstance()
129 ::tools::SolarMutex::SetSolarMutex( 0 );
130 delete mpSalYieldMutex;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */