2010-10-27 13:11:31 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 20:25:33 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2001-03-29 12:17:17 +00:00
|
|
|
|
2013-11-05 02:25:45 +01:00
|
|
|
#ifndef INCLUDED_FRAMEWORK_INC_THREADHELP_RESETABLEGUARD_HXX
|
|
|
|
#define INCLUDED_FRAMEWORK_INC_THREADHELP_RESETABLEGUARD_HXX
|
2001-03-29 12:17:17 +00:00
|
|
|
|
2014-03-17 14:29:13 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
2010-11-10 12:12:05 +01:00
|
|
|
#include <framework/imutex.hxx>
|
2001-06-11 09:25:40 +00:00
|
|
|
|
|
|
|
#include <sal/types.h>
|
2001-03-29 12:17:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace framework{
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
/*-************************************************************************************************************
|
2001-03-29 12:17:17 +00:00
|
|
|
@short implement a guard for implementing save thread access
|
|
|
|
@descr These guard has an additional feature to well known one ::osl::Guard.
|
|
|
|
You can lock() and unlock() it very often!
|
|
|
|
A set bool flag inside protect this implementation against multiple lock() calls
|
|
|
|
without any unlock()! So the increasing of guarded mutex couldn't be greater then 1 ...
|
|
|
|
|
2001-06-11 09:25:40 +00:00
|
|
|
@attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
|
|
|
|
b) Use interface "IMutex" of set LockHelper only - because we must support an exclusiv locking.
|
|
|
|
Interface "IRWLock" should be used by special guard implementations ... like "ReadGuard" or "WriteGuard"!
|
2001-03-29 12:17:17 +00:00
|
|
|
|
|
|
|
@implements -
|
|
|
|
|
|
|
|
@devstatus ready to use
|
|
|
|
*//*-*************************************************************************************************************/
|
2014-03-17 14:29:13 +01:00
|
|
|
class ResetableGuard : private boost::noncopyable
|
2001-03-29 12:17:17 +00:00
|
|
|
{
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
// public methods
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
public:
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
/*-****************************************************************************************************
|
2001-03-29 12:17:17 +00:00
|
|
|
@short ctors
|
|
|
|
@descr Use these ctor methods to initialize the guard right.
|
2013-09-26 11:44:54 +02:00
|
|
|
Given lock reference must be valid - otherwise crashes could occur!
|
2001-03-29 12:17:17 +00:00
|
|
|
|
|
|
|
@seealso -
|
|
|
|
|
2001-06-11 09:25:40 +00:00
|
|
|
@param "pLock", pointer to lock helper of user
|
|
|
|
@param "rLock", reference to lock helper of user
|
2001-03-29 12:17:17 +00:00
|
|
|
@return -
|
|
|
|
|
|
|
|
@onerror -
|
|
|
|
*//*-*****************************************************************************************************/
|
2001-06-11 09:25:40 +00:00
|
|
|
inline ResetableGuard( IMutex* pLock )
|
|
|
|
: m_pLock ( pLock )
|
|
|
|
, m_bLocked ( sal_False )
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-06-11 09:25:40 +00:00
|
|
|
inline ResetableGuard( IMutex& rLock )
|
|
|
|
: m_pLock ( &rLock )
|
|
|
|
, m_bLocked ( sal_False )
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
2001-03-29 12:17:17 +00:00
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
/*-****************************************************************************************************
|
2001-03-29 12:17:17 +00:00
|
|
|
@short dtor
|
|
|
|
@descr We must release set mutex if programmer forget it ...
|
|
|
|
|
|
|
|
@seealso -
|
|
|
|
|
|
|
|
@param -
|
|
|
|
@return -
|
|
|
|
|
|
|
|
@onerror -
|
|
|
|
*//*-*****************************************************************************************************/
|
2001-06-11 09:25:40 +00:00
|
|
|
inline ~ResetableGuard()
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
}
|
2001-03-29 12:17:17 +00:00
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
/*-****************************************************************************************************
|
2001-03-29 12:17:17 +00:00
|
|
|
@short enable/disable the lock
|
|
|
|
@descr Use this methods to lock or unlock the mutex.
|
|
|
|
You can do it so often you wish to do that ...
|
|
|
|
|
|
|
|
@attention We use another member to prevent us against multiple acquire calls of the same guard
|
|
|
|
without suitable release calls!
|
|
|
|
You don't must protect access at these bool member by using an own mutex ....
|
|
|
|
because nobody use the same guard instance from different threads!
|
|
|
|
It will be a function-local object every time.
|
|
|
|
|
|
|
|
@seealso -
|
|
|
|
|
|
|
|
@param -
|
|
|
|
@return -
|
|
|
|
|
|
|
|
@onerror -
|
|
|
|
*//*-*****************************************************************************************************/
|
2001-06-11 09:25:40 +00:00
|
|
|
inline void lock()
|
|
|
|
{
|
|
|
|
if( m_bLocked == sal_False )
|
|
|
|
{
|
|
|
|
m_pLock->acquire();
|
|
|
|
m_bLocked = sal_True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-06-11 09:25:40 +00:00
|
|
|
inline void unlock()
|
|
|
|
{
|
|
|
|
if( m_bLocked == sal_True )
|
|
|
|
{
|
|
|
|
m_pLock->release();
|
|
|
|
m_bLocked = sal_False;
|
|
|
|
}
|
|
|
|
}
|
2001-03-29 12:17:17 +00:00
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
// private methods
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
private:
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
/*-****************************************************************************************************
|
2001-03-29 12:17:17 +00:00
|
|
|
@short disable using of these functions!
|
2013-09-26 11:44:54 +02:00
|
|
|
@descr It's not allowed to use this methods. Different problem can occur otherwise.
|
2001-03-29 12:17:17 +00:00
|
|
|
Thats why we disable it by make it private.
|
|
|
|
|
|
|
|
@seealso other ctor
|
|
|
|
|
|
|
|
@param -
|
|
|
|
@return -
|
|
|
|
|
|
|
|
@onerror -
|
|
|
|
*//*-*****************************************************************************************************/
|
|
|
|
ResetableGuard();
|
|
|
|
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
// private member
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2001-03-29 12:17:17 +00:00
|
|
|
private:
|
|
|
|
|
2001-06-11 09:25:40 +00:00
|
|
|
IMutex* m_pLock ; /// pointer to safed lock member of user
|
2001-03-29 12:17:17 +00:00
|
|
|
sal_Bool m_bLocked ; /// protection against multiple lock() calls without unlock()
|
|
|
|
|
|
|
|
}; // class ResetableGuard
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
|
2013-11-05 02:25:45 +01:00
|
|
|
#endif // INCLUDED_FRAMEWORK_INC_THREADHELP_RESETABLEGUARD_HXX
|
2010-10-27 13:11:31 +01:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|