Files
libreoffice/configmgr/source/treecache/cachewritescheduler.cxx

231 lines
7.6 KiB
C++

/*************************************************************************
*
* $RCSfile: cachewritescheduler.cxx,v $
*
* $Revision: 1.3 $
*
* last change: $Author: jb $ $Date: 2002-03-28 09:08:05 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#include <stdio.h>
#include "cachewritescheduler.hxx"
#ifndef CONFIGMGR_BACKEND_CACHECONTROLLER_HXX
#include "cachecontroller.hxx"
#endif
#ifndef _CONFIGMGR_TRACER_HXX_
#include "tracer.hxx"
#endif
namespace configmgr
{
// =========================================================================
OCacheWriteScheduler::~OCacheWriteScheduler()
{
stopAndWriteCache();// last chance - violates precond
}
void OCacheWriteScheduler::stopAndWriteCache()
{
// PRE: if m_xTime.isValid() => The timer's shot mutex is acquired
osl::ClearableMutexGuard aOwnGuard( m_aMutex );
CFG_TRACE_INFO("Cancelling all cache writings, Stopping timer");
if (m_xTimer.isValid())
m_xTimer->dispose(); // just to be sure
aOwnGuard.clear();
runWriter();
}
// -------------------------------------------------------------------------
void OCacheWriteScheduler::Timer::onShot()
{
osl::MutexGuard aGuard(m_aMutex);
if (pParent)
pParent->onTimerShot();
}
// -----------------------------------------------------------------------------
void OCacheWriteScheduler::onTimerShot()
{
//m_aTimer.stop();
CFG_TRACE_INFO("Cleanup Timer invoked - executing dispose task");
try
{
runWriter();
}
catch (...)
{
OSL_ENSURE(false, "ERROR: Unknown Exception left a writer");
}
TimeStamp aNewTime = implGetScheduleTime(TimeStamp::getCurrentTime(), m_aWriteInterval);
osl::MutexGuard aGuard(m_aMutex);
implStartBefore(aNewTime);
}
// -------------------------------------------------------------------------
void OCacheWriteScheduler::runWriter()
{
// Write Cache
CFG_TRACE_INFO("Starting lazy write");
// osl::ClearableMutexGuard aGuard( m_rTreeManager.m_aUpdateMutex );
for (CacheWriteList::iterator it = m_aWriteList.begin();
it != m_aWriteList.end();
)
{
RequestOptions aTaskOption = *it;
++it; // advance iterator now - writeOneTree.. may erase current element
try
{
writeOneTreeFoundByOption(aTaskOption);
}
catch (uno::Exception& e)
{
CFG_TRACE_ERROR("TreeCacheWriteScheduler: Attempt to write data failed - error is '%s' (currently ignored)",OUSTRING2ASCII(e.Message));
}
}
// m_aWriteList.clear();
}
// -----------------------------------------------------------------------------
void OCacheWriteScheduler::writeOneTreeFoundByOption(RequestOptions const& _aOptions) CFG_UNO_THROW_ALL( )
{
// PRE: m_aUpdateMutex of TreeMgr must be acuired
CacheManager::CacheRef aCache = m_rTreeManager.m_aCacheList.get(_aOptions);
if (aCache.is())
{
CFG_TRACE_INFO_NI("- Found matching data container - starting write task");
m_rTreeManager.saveAllPendingChanges(aCache,_aOptions);
// we got a pending list with pointers from TreeInfo.
}
else
{
CFG_TRACE_WARNING_NI("- Data container (TreeInfo) to write not found: Ignoring task");
}
m_aWriteList.erase(_aOptions);
}
// -----------------------------------------------------------------------------
bool OCacheWriteScheduler::clearTasks(RequestOptions const& _aOptions)
{
// osl::MutexGuard aGuard( m_rTreeManager.m_aUpdateMutex );
// sadly list::remove doesn't return an indication of what it did
bool bFound = m_aWriteList.erase(_aOptions) !=0;
return bFound;
}
// -----------------------------------------------------------------------------
// should be called guarded only
void OCacheWriteScheduler::implStartBefore(TimeStamp const& _aTime)
{
// check if we were cleared
if (!m_aWriteList.empty())
{
if (!m_xTimer->isTicking())
{
m_xTimer->setAbsoluteTime(_aTime.getTimeValue());
if (!m_xTimer->isTicking())
m_xTimer->start();
OSL_ASSERT( m_xTimer->isTicking() );
}
CFG_TRACE_INFO_NI("- Cleanup timer running - next execution in %d seconds", int (m_xTimer->getRemainingTime().Seconds) );
CFG_TRACE_INFO_NI("- %d cleanup tasks are pending", int(m_aWriteList.size()) );
}
else
{
m_xTimer->stop();
CFG_TRACE_INFO_NI("- Stopped timer - no more open cleanup tasks");
}
}
// -----------------------------------------------------------------------------
void OCacheWriteScheduler::scheduleWrite(backend::ComponentRequest _aComponent) CFG_UNO_THROW_ALL( )
{
// PRE: m_aUpdateMutex of TreeMgr must be acuired
OSL_ENSURE(_aComponent.getOptions().hasLocale(), "ERROR: OTreeDisposeScheduler: cannot handle complete user scheduling");
osl::MutexGuard aGuard( m_aMutex );
CFG_TRACE_INFO("Scheduling cache write for user '%s' with locale '%s'",
OUSTRING2ASCII(_aComponent.getOptions().getEntity()),
OUSTRING2ASCII(_aComponent.getOptions().getLocale()));
// lasy writing
m_aWriteList.insert(_aComponent.getOptions());
TimeStamp aNewTime = implGetScheduleTime(TimeStamp::getCurrentTime(), m_aWriteInterval);
implStartBefore(aNewTime);
CFG_TRACE_INFO_NI("- cache write will be started in about %d seconds", int(m_aWriteInterval.getTimeValue().Seconds));
}
// -----------------------------------------------------------------------------
} // namespace