Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r86445 - trunk/boost/sync/thread_specific
From: andrey.semashev_at_[hidden]
Date: 2013-10-26 07:44:21


Author: andysem
Date: 2013-10-26 07:44:21 EDT (Sat, 26 Oct 2013)
New Revision: 86445
URL: http://svn.boost.org/trac/boost/changeset/86445

Log:
Added at_thread_exit. Added support for thread_specific_ptr cleanup on its destruction.

Added:
   trunk/boost/sync/thread_specific/at_thread_exit.hpp (contents, props changed)
Text files modified:
   trunk/boost/sync/thread_specific/at_thread_exit.hpp | 69 ++++++++++++++++++++++++++++++++++++++++
   trunk/boost/sync/thread_specific/thread_specific_ptr.hpp | 14 ++++----
   2 files changed, 76 insertions(+), 7 deletions(-)

Added: trunk/boost/sync/thread_specific/at_thread_exit.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/thread_specific/at_thread_exit.hpp 2013-10-26 07:44:21 EDT (Sat, 26 Oct 2013) (r86445)
@@ -0,0 +1,69 @@
+/*
+ * 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 2013 Andrey Semashev
+ */
+/*!
+ * \file thread_specific/at_thread_exit.hpp
+ *
+ * \brief This header defines \c at_thread_exit function.
+ */
+
+#ifndef BOOST_SYNC_THREAD_SPECIFIC_AT_THREAD_EXIT_HPP_INCLUDED_
+#define BOOST_SYNC_THREAD_SPECIFIC_AT_THREAD_EXIT_HPP_INCLUDED_
+
+#include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/tss.hpp>
+#include <boost/sync/detail/header.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+namespace boost {
+
+namespace sync {
+
+namespace detail {
+
+template< typename Function >
+void at_thread_exit_trampoline(void* p)
+{
+ Function* const fun = static_cast< Function* >(p);
+ (*fun)();
+ delete fun;
+}
+
+} // namespace detail
+
+/*!
+ * Registers a function to be called at the current thread termination. The function must not accept any arguments, its
+ * return value is ignored. The function must not throw exceptions. If multiple functions are registered by the thread,
+ * these functions will be invoked in the order reverse of their registration.
+ *
+ * \param fun The function to be called at thread termination.
+ */
+template< typename Function >
+inline void at_thread_exit(Function const& fun)
+{
+ Function* p = new Function(fun);
+ try
+ {
+ sync::detail::add_thread_exit_callback(&sync::detail::at_thread_exit_trampoline< Function >, p);
+ }
+ catch (...)
+ {
+ delete p;
+ throw;
+ }
+}
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_THREAD_SPECIFIC_AT_THREAD_EXIT_HPP_INCLUDED_

Modified: trunk/boost/sync/thread_specific/thread_specific_ptr.hpp
==============================================================================
--- trunk/boost/sync/thread_specific/thread_specific_ptr.hpp Sat Oct 26 07:22:14 2013 (r86444)
+++ trunk/boost/sync/thread_specific/thread_specific_ptr.hpp 2013-10-26 07:44:21 EDT (Sat, 26 Oct 2013) (r86445)
@@ -12,8 +12,8 @@
  * \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_
+#ifndef BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_HPP_INCLUDED_
+#define BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_HPP_INCLUDED_
 
 #include <boost/assert.hpp>
 #include <boost/utility/explicit_operator_bool.hpp>
@@ -40,14 +40,14 @@
     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)),
+ explicit thread_specific_ptr(bool cleanup_on_destroy = false) :
+ m_key(sync::detail::new_thread_specific_key(&thread_specific_ptr< T >::default_cleanup, cleanup_on_destroy)),
         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)),
+ explicit thread_specific_ptr(void (*cleanup)(T*), bool cleanup_on_destroy = false) :
+ m_key(sync::detail::new_thread_specific_key((sync::detail::at_thread_exit_callback)cleanup, cleanup_on_destroy)),
         m_cleanup((sync::detail::at_thread_exit_callback)cleanup)
     {
     }
@@ -124,4 +124,4 @@
 
 #include <boost/sync/detail/footer.hpp>
 
-#endif // BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_H_INCLUDED_
+#endif // BOOST_SYNC_THREAD_SPECIFIC_THREAD_SPECIFIC_PTR_HPP_INCLUDED_


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