global_static.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_DETAIL_GLOBAL_STATIC_HPP
  11. #define BOOST_COMPUTE_DETAIL_GLOBAL_STATIC_HPP
  12. #include <boost/compute/config.hpp>
  13. #ifdef BOOST_COMPUTE_THREAD_SAFE
  14. # ifdef BOOST_COMPUTE_HAVE_THREAD_LOCAL
  15. // use c++11 thread local storage
  16. # define BOOST_COMPUTE_DETAIL_GLOBAL_STATIC(type, name, ctor) \
  17. thread_local type name ctor;
  18. # else
  19. // use thread_specific_ptr from boost.thread
  20. # include <boost/thread/tss.hpp>
  21. # define BOOST_COMPUTE_DETAIL_GLOBAL_STATIC(type, name, ctor) \
  22. static ::boost::thread_specific_ptr< type > BOOST_PP_CAT(name, _tls_ptr_); \
  23. if(!BOOST_PP_CAT(name, _tls_ptr_).get()){ \
  24. BOOST_PP_CAT(name, _tls_ptr_).reset(new type ctor); \
  25. } \
  26. static type &name = *BOOST_PP_CAT(name, _tls_ptr_);
  27. # endif
  28. #else
  29. // no thread-safety, just use static
  30. # define BOOST_COMPUTE_DETAIL_GLOBAL_STATIC(type, name, ctor) \
  31. static type name ctor;
  32. #endif
  33. #endif // BOOST_COMPUTE_DETAIL_GLOBAL_STATIC_HPP