xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/src/c++11/shared_ptr.cc (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg // Support for pointer abstractions -*- C++ -*-
21debfc3dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2011-2020 Free Software Foundation, Inc.
41debfc3dSmrg //
51debfc3dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
61debfc3dSmrg // software; you can redistribute it and/or modify it under the
71debfc3dSmrg // terms of the GNU General Public License as published by the
81debfc3dSmrg // Free Software Foundation; either version 3, or (at your option)
91debfc3dSmrg // any later version.
101debfc3dSmrg 
111debfc3dSmrg // This library is distributed in the hope that it will be useful,
121debfc3dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
131debfc3dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141debfc3dSmrg // GNU General Public License for more details.
151debfc3dSmrg 
161debfc3dSmrg // Under Section 7 of GPL version 3, you are granted additional
171debfc3dSmrg // permissions described in the GCC Runtime Library Exception, version
181debfc3dSmrg // 3.1, as published by the Free Software Foundation.
191debfc3dSmrg 
201debfc3dSmrg // You should have received a copy of the GNU General Public License and
211debfc3dSmrg // a copy of the GCC Runtime Library Exception along with this program;
221debfc3dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
231debfc3dSmrg // <http://www.gnu.org/licenses/>.
241debfc3dSmrg 
251debfc3dSmrg #include <memory>
261debfc3dSmrg 
271debfc3dSmrg #include "mutex_pool.h"
281debfc3dSmrg 
291debfc3dSmrg namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
301debfc3dSmrg {
311debfc3dSmrg   /* Returns different instances of __mutex depending on the passed index
321debfc3dSmrg    * in order to limit contention.
331debfc3dSmrg    */
341debfc3dSmrg   __gnu_cxx::__mutex&
get_mutex(unsigned char i)351debfc3dSmrg   get_mutex(unsigned char i)
361debfc3dSmrg   {
37*8feb0f0bSmrg     // increase alignment to put each lock on a separate cache line
38*8feb0f0bSmrg     struct alignas(64) M : __gnu_cxx::__mutex { };
39*8feb0f0bSmrg     static M m[mask + 1];
401debfc3dSmrg     return m[i];
411debfc3dSmrg   }
421debfc3dSmrg }
431debfc3dSmrg 
441debfc3dSmrg namespace std _GLIBCXX_VISIBILITY(default)
451debfc3dSmrg {
461debfc3dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
471debfc3dSmrg 
481debfc3dSmrg   bad_weak_ptr::~bad_weak_ptr() noexcept = default;
491debfc3dSmrg 
501debfc3dSmrg   char const*
what() const511debfc3dSmrg   bad_weak_ptr::what() const noexcept
521debfc3dSmrg   { return "bad_weak_ptr"; }
531debfc3dSmrg 
541debfc3dSmrg #ifdef __GTHREADS
551debfc3dSmrg   namespace
561debfc3dSmrg   {
key(const void * addr)571debfc3dSmrg     inline unsigned char key(const void* addr)
581debfc3dSmrg     { return _Hash_impl::hash(addr) & __gnu_internal::mask; }
591debfc3dSmrg   }
601debfc3dSmrg 
_Sp_locker(const void * p)611debfc3dSmrg   _Sp_locker::_Sp_locker(const void* p) noexcept
621debfc3dSmrg   {
631debfc3dSmrg     if (__gthread_active_p())
641debfc3dSmrg       {
651debfc3dSmrg 	_M_key1 = _M_key2 = key(p);
661debfc3dSmrg         __gnu_internal::get_mutex(_M_key1).lock();
671debfc3dSmrg       }
681debfc3dSmrg     else
691debfc3dSmrg       _M_key1 = _M_key2 = __gnu_internal::invalid;
701debfc3dSmrg   }
711debfc3dSmrg 
_Sp_locker(const void * p1,const void * p2)721debfc3dSmrg   _Sp_locker::_Sp_locker(const void* p1, const void* p2) noexcept
731debfc3dSmrg   {
741debfc3dSmrg     if (__gthread_active_p())
751debfc3dSmrg       {
761debfc3dSmrg 	_M_key1 = key(p1);
771debfc3dSmrg 	_M_key2 = key(p2);
781debfc3dSmrg 	if (_M_key2 < _M_key1)
791debfc3dSmrg 	  __gnu_internal::get_mutex(_M_key2).lock();
801debfc3dSmrg 	__gnu_internal::get_mutex(_M_key1).lock();
811debfc3dSmrg 	if (_M_key2 > _M_key1)
821debfc3dSmrg 	  __gnu_internal::get_mutex(_M_key2).lock();
831debfc3dSmrg       }
841debfc3dSmrg     else
851debfc3dSmrg       _M_key1 = _M_key2 = __gnu_internal::invalid;
861debfc3dSmrg   }
871debfc3dSmrg 
~_Sp_locker()881debfc3dSmrg   _Sp_locker::~_Sp_locker()
891debfc3dSmrg   {
901debfc3dSmrg     if (_M_key1 != __gnu_internal::invalid)
911debfc3dSmrg       {
921debfc3dSmrg 	__gnu_internal::get_mutex(_M_key1).unlock();
931debfc3dSmrg 	if (_M_key2 != _M_key1)
941debfc3dSmrg 	  __gnu_internal::get_mutex(_M_key2).unlock();
951debfc3dSmrg       }
961debfc3dSmrg   }
971debfc3dSmrg #endif
981debfc3dSmrg 
99c0a68be4Smrg   bool
_S_eq(const type_info & ti)100c0a68be4Smrg   _Sp_make_shared_tag::_S_eq(const type_info& ti) noexcept
101c0a68be4Smrg   {
102c0a68be4Smrg #if __cpp_rtti
103c0a68be4Smrg     return ti == typeid(_Sp_make_shared_tag);
104c0a68be4Smrg #else
105c0a68be4Smrg     // If libstdc++ itself is built with -fno-rtti then just assume that
106c0a68be4Smrg     // make_shared and allocate_shared will never be used with -frtti.
107c0a68be4Smrg     return false;
108c0a68be4Smrg #endif
109c0a68be4Smrg   }
110c0a68be4Smrg 
1111debfc3dSmrg _GLIBCXX_END_NAMESPACE_VERSION
1121debfc3dSmrg } // namespace
113