os/ossrv/ossrv_pub/boost_apis/boost/thread/tss.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (C) 2001-2003 William E. Kempf
sl@0
     2
// Copyright (C) 2006 Roland Schwarz
sl@0
     3
//
sl@0
     4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
sl@0
     5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
sl@0
     7
#ifndef BOOST_TSS_WEK070601_HPP
sl@0
     8
#define BOOST_TSS_WEK070601_HPP
sl@0
     9
sl@0
    10
#include <boost/thread/detail/config.hpp>
sl@0
    11
sl@0
    12
#include <boost/utility.hpp>
sl@0
    13
#include <boost/function.hpp>
sl@0
    14
#include <boost/thread/exceptions.hpp>
sl@0
    15
sl@0
    16
#if defined(BOOST_HAS_PTHREADS)
sl@0
    17
#   include <pthread.h>
sl@0
    18
#elif defined(BOOST_HAS_MPTASKS)
sl@0
    19
#   include <Multiprocessing.h>
sl@0
    20
#endif
sl@0
    21
sl@0
    22
namespace boost {
sl@0
    23
sl@0
    24
// disable warnings about non dll import
sl@0
    25
// see: http://www.boost.org/more/separate_compilation.html#dlls
sl@0
    26
#ifdef BOOST_MSVC
sl@0
    27
#   pragma warning(push)
sl@0
    28
#   pragma warning(disable: 4251 4231 4660 4275)
sl@0
    29
#endif
sl@0
    30
sl@0
    31
namespace detail {
sl@0
    32
sl@0
    33
class BOOST_THREAD_DECL tss : private noncopyable
sl@0
    34
{
sl@0
    35
public:
sl@0
    36
    tss(boost::function1<void, void*>* pcleanup) {
sl@0
    37
        if (pcleanup == 0) throw boost::thread_resource_error();
sl@0
    38
        try
sl@0
    39
        {
sl@0
    40
            init(pcleanup);
sl@0
    41
        }
sl@0
    42
        catch (...)
sl@0
    43
        {
sl@0
    44
            delete pcleanup;
sl@0
    45
            throw boost::thread_resource_error();
sl@0
    46
        }
sl@0
    47
    }
sl@0
    48
sl@0
    49
    ~tss();
sl@0
    50
    void* get() const;
sl@0
    51
    void set(void* value);
sl@0
    52
    void cleanup(void* p);
sl@0
    53
sl@0
    54
private:
sl@0
    55
    unsigned int m_slot; //This is a "pseudo-slot", not a native slot
sl@0
    56
sl@0
    57
    void init(boost::function1<void, void*>* pcleanup);
sl@0
    58
};
sl@0
    59
sl@0
    60
#if defined(BOOST_HAS_MPTASKS)
sl@0
    61
void thread_cleanup();
sl@0
    62
#endif
sl@0
    63
sl@0
    64
template <typename T>
sl@0
    65
struct tss_adapter
sl@0
    66
{
sl@0
    67
    template <typename F>
sl@0
    68
    tss_adapter(const F& cleanup) : m_cleanup(cleanup) { }
sl@0
    69
    void operator()(void* p) { m_cleanup(static_cast<T*>(p)); }
sl@0
    70
    boost::function1<void, T*> m_cleanup;
sl@0
    71
};
sl@0
    72
sl@0
    73
} // namespace detail
sl@0
    74
sl@0
    75
template <typename T>
sl@0
    76
class thread_specific_ptr : private noncopyable
sl@0
    77
{
sl@0
    78
public:
sl@0
    79
    thread_specific_ptr()
sl@0
    80
        : m_tss(new boost::function1<void, void*>(
sl@0
    81
                    boost::detail::tss_adapter<T>(
sl@0
    82
                        &thread_specific_ptr<T>::cleanup)))
sl@0
    83
    {
sl@0
    84
    }
sl@0
    85
    thread_specific_ptr(void (*clean)(T*))
sl@0
    86
        : m_tss(new boost::function1<void, void*>(
sl@0
    87
                    boost::detail::tss_adapter<T>(clean)))
sl@0
    88
    {
sl@0
    89
    }
sl@0
    90
    ~thread_specific_ptr() { reset(); }
sl@0
    91
sl@0
    92
    T* get() const { return static_cast<T*>(m_tss.get()); }
sl@0
    93
    T* operator->() const { return get(); }
sl@0
    94
    T& operator*() const { return *get(); }
sl@0
    95
    T* release() { T* temp = get(); if (temp) m_tss.set(0); return temp; }
sl@0
    96
    void reset(T* p=0)
sl@0
    97
    {
sl@0
    98
        T* cur = get();
sl@0
    99
        if (cur == p) return;
sl@0
   100
        m_tss.set(p);
sl@0
   101
        if (cur) m_tss.cleanup(cur);
sl@0
   102
    }
sl@0
   103
sl@0
   104
private:
sl@0
   105
    static void cleanup(T* p) { delete p; }
sl@0
   106
    detail::tss m_tss;
sl@0
   107
};
sl@0
   108
sl@0
   109
#ifdef BOOST_MSVC
sl@0
   110
#   pragma warning(pop)
sl@0
   111
#endif
sl@0
   112
sl@0
   113
} // namespace boost
sl@0
   114
sl@0
   115
#endif //BOOST_TSS_WEK070601_HPP
sl@0
   116
sl@0
   117
// Change Log:
sl@0
   118
//   6 Jun 01  
sl@0
   119
//      WEKEMPF Initial version.
sl@0
   120
//  30 May 02  WEKEMPF 
sl@0
   121
//      Added interface to set specific cleanup handlers.
sl@0
   122
//      Removed TLS slot limits from most implementations.
sl@0
   123
//  22 Mar 04 GlassfordM for WEKEMPF
sl@0
   124
//      Fixed: thread_specific_ptr::reset() doesn't check error returned
sl@0
   125
//          by tss::set(); tss::set() now throws if it fails.
sl@0
   126
//      Fixed: calling thread_specific_ptr::reset() or 
sl@0
   127
//          thread_specific_ptr::release() causes double-delete: once on
sl@0
   128
//          reset()/release() and once on ~thread_specific_ptr().