mtlist.h revision 4520ec42b031779169b52cb9536f22937fd9bf97
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/** @file
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * IPRT - Generic thread-safe list Class.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Copyright (C) 2011 Oracle Corporation
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * available from http://www.virtualbox.org. This file is free software;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * General Public License (GPL) as published by the Free Software
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The contents of this file may alternatively be used under the terms
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * of the Common Development and Distribution License Version 1.0
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * CDDL are applicable instead of those of the GPL.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * You may elect to license modified versions of this file under the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * terms and conditions of either the GPL or the CDDL or both.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#ifndef ___iprt_cpp_mtlist_h
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define ___iprt_cpp_mtlist_h
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#include <iprt/cpp/list.h>
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#include <iprt/semaphore.h>
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/** @addtogroup grp_rt_cpp_list
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @{
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/**
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * A guard class for thread-safe read/write access.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynctemplate <>
class RTCListGuard<true>
{
public:
RTCListGuard() { int rc = RTSemRWCreate(&m_hRWSem); AssertRC(rc); }
~RTCListGuard() { RTSemRWDestroy(m_hRWSem); }
inline void enterRead() const { int rc = RTSemRWRequestRead(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
inline void leaveRead() const { int rc = RTSemRWReleaseRead(m_hRWSem); AssertRC(rc); }
inline void enterWrite() { int rc = RTSemRWRequestWrite(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
inline void leaveWrite() { int rc = RTSemRWReleaseWrite(m_hRWSem); AssertRC(rc); }
private:
mutable RTSEMRW m_hRWSem;
};
/**
* @brief Generic thread-safe list class.
*
* RTCMTList is a thread-safe implementation of the list class. It uses a
* read/write semaphore to serialize the access to the items. Several readers
* can simultaneous access different or the same item. If one thread is writing
* to an item, the other accessors are blocked until the write has finished.
*
* Although the access is guarded, the user has to make sure the list content
* is consistent when iterating over the list or doing any other kind of access
* which makes assumptions about the list content. For a finer control of access
* restrictions, use your own locking mechanism and the standard list
* implementation.
*
* @see RTCListBase
*/
template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
class RTCMTList : public RTCListBase<T, ITYPE, true>
{
typedef RTCListBase<T, ITYPE, true> BASE;
public:
RTCMTList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
};
/**
* Specialized thread-safe list class for using the native type list for
* unsigned 64-bit values even on a 32-bit host.
*
* @see RTCListBase
*/
template <>
class RTCMTList<uint64_t>: public RTCListBase<uint64_t, uint64_t, true>
{
typedef RTCListBase<uint64_t, uint64_t, true> BASE;
public:
RTCMTList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
};
/**
* Specialized thread-safe list class for using the native type list for
* signed 64-bit values even on a 32-bit host.
*
* @see RTCListBase
*/
template <>
class RTCMTList<int64_t>: public RTCListBase<int64_t, int64_t, true>
{
typedef RTCListBase<int64_t, int64_t, true> BASE;
public:
RTCMTList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
};
/** @} */
#endif /* !___iprt_cpp_mtlist_h */