libstdc++
safe_base.h
Go to the documentation of this file.
00001 // Safe sequence/iterator base implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file debug/safe_base.h
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
00030 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
00031 
00032 #include <ext/concurrence.h>
00033 
00034 namespace __gnu_debug
00035 {
00036   class _Safe_sequence_base;
00037 
00038   /** \brief Basic functionality for a @a safe iterator.
00039    *
00040    *  The %_Safe_iterator_base base class implements the functionality
00041    *  of a safe iterator that is not specific to a particular iterator
00042    *  type. It contains a pointer back to the sequence it references
00043    *  along with iterator version information and pointers to form a
00044    *  doubly-linked list of iterators referenced by the container.
00045    *
00046    *  This class must not perform any operations that can throw an
00047    *  exception, or the exception guarantees of derived iterators will
00048    *  be broken.
00049    */
00050   class _Safe_iterator_base
00051   {
00052     friend class _Safe_sequence_base;
00053 
00054   public:
00055     /** The sequence this iterator references; may be NULL to indicate
00056         a singular iterator. */
00057     _Safe_sequence_base*        _M_sequence;
00058 
00059     /** The version number of this iterator. The sentinel value 0 is
00060      *  used to indicate an invalidated iterator (i.e., one that is
00061      *  singular because of an operation on the container). This
00062      *  version number must equal the version number in the sequence
00063      *  referenced by _M_sequence for the iterator to be
00064      *  non-singular.
00065      */
00066     unsigned int                _M_version;
00067 
00068     /** Pointer to the previous iterator in the sequence's list of
00069         iterators. Only valid when _M_sequence != NULL. */
00070     _Safe_iterator_base*        _M_prior;
00071 
00072     /** Pointer to the next iterator in the sequence's list of
00073         iterators. Only valid when _M_sequence != NULL. */
00074     _Safe_iterator_base*        _M_next;
00075 
00076   protected:
00077     /** Initializes the iterator and makes it singular. */
00078     _Safe_iterator_base()
00079     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00080     { }
00081 
00082     /** Initialize the iterator to reference the sequence pointed to
00083      *  by @p __seq. @p __constant is true when we are initializing a
00084      *  constant iterator, and false if it is a mutable iterator. Note
00085      *  that @p __seq may be NULL, in which case the iterator will be
00086      *  singular. Otherwise, the iterator will reference @p __seq and
00087      *  be nonsingular.
00088      */
00089     _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
00090     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00091     { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
00092 
00093     /** Initializes the iterator to reference the same sequence that
00094         @p __x does. @p __constant is true if this is a constant
00095         iterator, and false if it is mutable. */
00096     _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
00097     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00098     { this->_M_attach(__x._M_sequence, __constant); }
00099 
00100     ~_Safe_iterator_base() { this->_M_detach(); }
00101 
00102     /** For use in _Safe_iterator. */
00103     __gnu_cxx::__mutex&
00104     _M_get_mutex() throw ();
00105 
00106     /** Attaches this iterator to the given sequence, detaching it
00107      *  from whatever sequence it was attached to originally. If the
00108      *  new sequence is the NULL pointer, the iterator is left
00109      *  unattached.
00110      */
00111     void
00112     _M_attach(_Safe_sequence_base* __seq, bool __constant);
00113 
00114     /** Likewise, but not thread-safe. */
00115     void
00116     _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
00117 
00118     /** Detach the iterator for whatever sequence it is attached to,
00119      *  if any.
00120     */
00121     void
00122     _M_detach();
00123 
00124   public:
00125     /** Likewise, but not thread-safe. */
00126     void
00127     _M_detach_single() throw ();
00128 
00129     /** Determines if we are attached to the given sequence. */
00130     bool
00131     _M_attached_to(const _Safe_sequence_base* __seq) const
00132     { return _M_sequence == __seq; }
00133 
00134     /** Is this iterator singular? */
00135     _GLIBCXX_PURE bool
00136     _M_singular() const throw ();
00137 
00138     /** Can we compare this iterator to the given iterator @p __x?
00139         Returns true if both iterators are nonsingular and reference
00140         the same sequence. */
00141     _GLIBCXX_PURE bool
00142     _M_can_compare(const _Safe_iterator_base& __x) const throw ();
00143 
00144     /** Invalidate the iterator, making it singular. */
00145     void
00146     _M_invalidate()
00147     { _M_version = 0; }
00148 
00149     /** Reset all member variables */
00150     void
00151     _M_reset() throw ();
00152 
00153     /** Unlink itself */
00154     void
00155     _M_unlink() throw ()
00156     {
00157       if (_M_prior)
00158         _M_prior->_M_next = _M_next;
00159       if (_M_next)
00160         _M_next->_M_prior = _M_prior;
00161     }
00162   };
00163 
00164   /** Iterators that derive from _Safe_iterator_base can be determined singular
00165    *  or non-singular.
00166    **/
00167   inline bool
00168   __check_singular_aux(const _Safe_iterator_base* __x)
00169   { return __x->_M_singular(); }
00170 
00171   /**
00172    * @brief Base class that supports tracking of iterators that
00173    * reference a sequence.
00174    *
00175    * The %_Safe_sequence_base class provides basic support for
00176    * tracking iterators into a sequence. Sequences that track
00177    * iterators must derived from %_Safe_sequence_base publicly, so
00178    * that safe iterators (which inherit _Safe_iterator_base) can
00179    * attach to them. This class contains two linked lists of
00180    * iterators, one for constant iterators and one for mutable
00181    * iterators, and a version number that allows very fast
00182    * invalidation of all iterators that reference the container.
00183    *
00184    * This class must ensure that no operation on it may throw an
00185    * exception, otherwise @a safe sequences may fail to provide the
00186    * exception-safety guarantees required by the C++ standard.
00187    */
00188   class _Safe_sequence_base
00189   {
00190     friend class _Safe_iterator_base;
00191 
00192   public:
00193     /// The list of mutable iterators that reference this container
00194     _Safe_iterator_base* _M_iterators;
00195 
00196     /// The list of constant iterators that reference this container
00197     _Safe_iterator_base* _M_const_iterators;
00198 
00199     /// The container version number. This number may never be 0.
00200     mutable unsigned int _M_version;
00201 
00202   protected:
00203     // Initialize with a version number of 1 and no iterators
00204     _Safe_sequence_base() _GLIBCXX_NOEXCEPT
00205     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
00206     { }
00207 
00208 #if __cplusplus >= 201103L
00209     _Safe_sequence_base(const _Safe_sequence_base&) noexcept
00210     : _Safe_sequence_base() { }
00211 
00212     // Move constructor swap iterators.
00213     _Safe_sequence_base(_Safe_sequence_base&& __seq) noexcept
00214     : _Safe_sequence_base()
00215     { _M_swap(__seq); }
00216 #endif
00217 
00218     /** Notify all iterators that reference this sequence that the
00219         sequence is being destroyed. */
00220     ~_Safe_sequence_base()
00221     { this->_M_detach_all(); }
00222 
00223     /** Detach all iterators, leaving them singular. */
00224     void
00225     _M_detach_all();
00226 
00227     /** Detach all singular iterators.
00228      *  @post for all iterators i attached to this sequence,
00229      *   i->_M_version == _M_version.
00230      */
00231     void
00232     _M_detach_singular();
00233 
00234     /** Revalidates all attached singular iterators.  This method may
00235      *  be used to validate iterators that were invalidated before
00236      *  (but for some reason, such as an exception, need to become
00237      *  valid again).
00238      */
00239     void
00240     _M_revalidate_singular();
00241 
00242     /** Swap this sequence with the given sequence. This operation
00243      *  also swaps ownership of the iterators, so that when the
00244      *  operation is complete all iterators that originally referenced
00245      *  one container now reference the other container.
00246      */
00247     void
00248     _M_swap(_Safe_sequence_base& __x) _GLIBCXX_USE_NOEXCEPT;
00249 
00250     /** For use in _Safe_sequence. */
00251     __gnu_cxx::__mutex&
00252     _M_get_mutex() throw ();
00253 
00254     /** Invalidates all iterators. */
00255     void
00256     _M_invalidate_all() const
00257     { if (++_M_version == 0) _M_version = 1; }
00258 
00259   private:
00260     /** Attach an iterator to this sequence. */
00261     void
00262     _M_attach(_Safe_iterator_base* __it, bool __constant);
00263 
00264     /** Likewise but not thread safe. */
00265     void
00266     _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
00267 
00268     /** Detach an iterator from this sequence */
00269     void
00270     _M_detach(_Safe_iterator_base* __it);
00271 
00272     /** Likewise but not thread safe. */
00273     void
00274     _M_detach_single(_Safe_iterator_base* __it) throw ();
00275   };
00276 } // namespace __gnu_debug
00277 
00278 #endif