Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:42

0001 //
0002 // detail/posix_global.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_POSIX_GLOBAL_HPP
0012 #define BOOST_ASIO_DETAIL_POSIX_GLOBAL_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_HAS_PTHREADS)
0021 
0022 #include <exception>
0023 #include <pthread.h>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 template <typename T>
0032 struct posix_global_impl
0033 {
0034   // Helper function to perform initialisation.
0035   static void do_init()
0036   {
0037     instance_.static_ptr_ = instance_.ptr_ = new T;
0038   }
0039 
0040   // Destructor automatically cleans up the global.
0041   ~posix_global_impl()
0042   {
0043     delete static_ptr_;
0044   }
0045 
0046   static ::pthread_once_t init_once_;
0047   static T* static_ptr_;
0048   static posix_global_impl instance_;
0049   T* ptr_;
0050 };
0051 
0052 template <typename T>
0053 ::pthread_once_t posix_global_impl<T>::init_once_ = PTHREAD_ONCE_INIT;
0054 
0055 template <typename T>
0056 T* posix_global_impl<T>::static_ptr_ = 0;
0057 
0058 template <typename T>
0059 posix_global_impl<T> posix_global_impl<T>::instance_;
0060 
0061 template <typename T>
0062 T& posix_global()
0063 {
0064   int result = ::pthread_once(
0065       &posix_global_impl<T>::init_once_,
0066       &posix_global_impl<T>::do_init);
0067 
0068   if (result != 0)
0069     std::terminate();
0070 
0071   return *posix_global_impl<T>::instance_.ptr_;
0072 }
0073 
0074 } // namespace detail
0075 } // namespace asio
0076 } // namespace boost
0077 
0078 #include <boost/asio/detail/pop_options.hpp>
0079 
0080 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
0081 
0082 #endif // BOOST_ASIO_DETAIL_POSIX_GLOBAL_HPP