Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r86430 - in trunk: boost/sync/detail boost/sync/thread_specific libs/sync/src
From: andrey.semashev_at_[hidden]
Date: 2013-10-25 13:38:08


Author: andysem
Date: 2013-10-25 13:38:08 EDT (Fri, 25 Oct 2013)
New Revision: 86430
URL: http://svn.boost.org/trac/boost/changeset/86430

Log:
Added implementation of thread_specific_ptr.

Added:
   trunk/boost/sync/thread_specific/
   trunk/boost/sync/thread_specific/thread_specific_ptr.hpp (contents, props changed)
Text files modified:
   trunk/boost/sync/detail/tss.hpp | 2
   trunk/boost/sync/thread_specific/thread_specific_ptr.hpp | 123 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/sync/src/tss_pthread.cpp | 2
   trunk/libs/sync/src/tss_windows.cpp | 2
   4 files changed, 126 insertions(+), 3 deletions(-)

Modified: trunk/boost/sync/detail/tss.hpp
==============================================================================
--- trunk/boost/sync/detail/tss.hpp Fri Oct 25 12:51:18 2013 (r86429)
+++ trunk/boost/sync/detail/tss.hpp 2013-10-25 13:38:08 EDT (Fri, 25 Oct 2013) (r86430)
@@ -68,7 +68,7 @@
  *
  * \returns The thread-specific value. Upon the first call, when the thread has not yet set the value, the function returns \c NULL.
  */
-BOOST_SYNC_API void* get_thread_specific(thread_specific_key key);
+BOOST_SYNC_API void* get_thread_specific(thread_specific_key key) BOOST_NOEXCEPT;
 /*!
  * \brief Installs a value for the key and the current thread.
  *

Added: trunk/boost/sync/thread_specific/thread_specific_ptr.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/thread_specific/thread_specific_ptr.hpp 2013-10-25 13:38:08 EDT (Fri, 25 Oct 2013) (r86430)
@@ -0,0 +1,123 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * (C) Copyright 2007-2008 Anthony Williams
+ * (C) Copyright 2013 Andrey Semashev
+ */
+/*!
+ * \file thread_specific/thread_specific_ptr.hpp
+ *
+ * \brief This header defines \c thread_specific_ptr smart-pointer.
+ */
+
+#ifndef BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_H_INCLUDED_
+#define BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_H_INCLUDED_
+
+#include <boost/assert.hpp>
+#include <boost/utility/explicit_operator_bool.hpp>
+#include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/tss.hpp>
+#include <boost/sync/detail/header.hpp>
+
+namespace boost {
+
+namespace sync {
+
+template< typename T >
+class thread_specific_ptr
+{
+public:
+ typedef T element_type;
+
+private:
+ const sync::detail::thread_specific_key m_key;
+ const sync::detail::at_thread_exit_callback m_cleanup;
+
+public:
+ thread_specific_ptr() :
+ m_key(sync::detail::new_thread_specific_key(&thread_specific_ptr< T >::default_cleanup, false)),
+ m_cleanup(&thread_specific_ptr< T >::default_cleanup)
+ {
+ }
+
+ explicit thread_specific_ptr(void (*cleanup)(T*)) :
+ m_key(sync::detail::new_thread_specific_key((sync::detail::at_thread_exit_callback)cleanup, false)),
+ m_cleanup((sync::detail::at_thread_exit_callback)cleanup)
+ {
+ }
+
+ ~thread_specific_ptr()
+ {
+ sync::detail::delete_thread_specific_key(m_key);
+ }
+
+ T* get() const BOOST_NOEXCEPT
+ {
+ return static_cast< T* >(sync::detail::get_thread_specific(m_key));
+ }
+
+ T* operator-> () const BOOST_NOEXCEPT
+ {
+ T* const p = get();
+ BOOST_ASSERT(!!p);
+ return p;
+ }
+
+ T& operator* () const BOOST_NOEXCEPT
+ {
+ T* const p = get();
+ BOOST_ASSERT(!!p);
+ return *p;
+ }
+
+ bool operator! () const BOOST_NOEXCEPT
+ {
+ return !get();
+ }
+
+ BOOST_EXPLICIT_OPERATOR_BOOL()
+
+ T* release()
+ {
+ T* const p = get();
+ if (p)
+ sync::detail::set_thread_specific(m_key, 0);
+ return p;
+ }
+
+ void reset(T* new_value = 0)
+ {
+ T* const old_value = get();
+ if (old_value != new_value)
+ {
+ sync::detail::set_thread_specific(m_key, new_value);
+ if (old_value && m_cleanup)
+ m_cleanup(old_value);
+ }
+ }
+
+ BOOST_DELETED_FUNCTION(thread_specific_ptr(thread_specific_ptr const&))
+ BOOST_DELETED_FUNCTION(thread_specific_ptr& operator= (thread_specific_ptr const&))
+
+private:
+ static void default_cleanup(void* p)
+ {
+ delete static_cast< T* >(p);
+ }
+};
+
+template< typename T >
+inline T* get_pointer(thread_specific_ptr< T > const& ptr)
+{
+ return ptr.get();
+}
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_H_INCLUDED_

Modified: trunk/libs/sync/src/tss_pthread.cpp
==============================================================================
--- trunk/libs/sync/src/tss_pthread.cpp Fri Oct 25 12:51:18 2013 (r86429)
+++ trunk/libs/sync/src/tss_pthread.cpp 2013-10-25 13:38:08 EDT (Fri, 25 Oct 2013) (r86430)
@@ -142,7 +142,7 @@
     }
 }
 
-BOOST_SYNC_API void* get_thread_specific(thread_specific_key key)
+BOOST_SYNC_API void* get_thread_specific(thread_specific_key key) BOOST_NOEXCEPT
 {
     tss_manager::thread_context* ctx = get_thread_context();
     if (ctx)

Modified: trunk/libs/sync/src/tss_windows.cpp
==============================================================================
--- trunk/libs/sync/src/tss_windows.cpp Fri Oct 25 12:51:18 2013 (r86429)
+++ trunk/libs/sync/src/tss_windows.cpp 2013-10-25 13:38:08 EDT (Fri, 25 Oct 2013) (r86430)
@@ -212,7 +212,7 @@
     }
 }
 
-BOOST_SYNC_API void* get_thread_specific(thread_specific_key key)
+BOOST_SYNC_API void* get_thread_specific(thread_specific_key key) BOOST_NOEXCEPT
 {
     tss_manager::thread_context* ctx = get_thread_context();
     if (ctx)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk