summaryrefslogtreecommitdiff
path: root/libs/log/src/thread_specific.cpp
blob: f5dc22562584fdc95a1114ab87ac40ccae245023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * 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)
 */
/*!
 * \file   thread_specific.cpp
 * \author Andrey Semashev
 * \date   01.03.2008
 *
 * \brief  This header is the Boost.Log library implementation, see the library documentation
 *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
 */

#include <string>
#include <stdexcept>
#include <boost/log/exceptions.hpp>
#include <boost/log/detail/thread_specific.hpp>

#if !defined(BOOST_LOG_NO_THREADS)

#include <boost/thread/tss.hpp> // To hook on Boost.Thread configuration macros
#include <boost/log/detail/header.hpp>


#if defined(BOOST_THREAD_PLATFORM_WIN32)

#include "windows_version.hpp"
#include <windows.h>

namespace boost {

BOOST_LOG_OPEN_NAMESPACE

namespace aux {

thread_specific_base::thread_specific_base()
{
    m_Key.as_dword = TlsAlloc();
    if (m_Key.as_dword == TLS_OUT_OF_INDEXES)
    {
        BOOST_LOG_THROW_DESCR(system_error, "TLS capacity depleted");
    }
    set_content(0);
}

thread_specific_base::~thread_specific_base()
{
    TlsFree(m_Key.as_dword);
}

void* thread_specific_base::get_content() const
{
    return TlsGetValue(m_Key.as_dword);
}

void thread_specific_base::set_content(void* value) const
{
    TlsSetValue(m_Key.as_dword, value);
}

} // namespace aux

BOOST_LOG_CLOSE_NAMESPACE // namespace log

} // namespace boost

#elif defined(BOOST_THREAD_PLATFORM_PTHREAD)

#include <pthread.h>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/alignment_of.hpp>

namespace boost {

BOOST_LOG_OPEN_NAMESPACE

namespace aux {

BOOST_LOG_ANONYMOUS_NAMESPACE {

    //! A helper template to disable early name binding
    template< typename NonDependentT, typename DependentT >
    struct make_dependent
    {
        typedef NonDependentT type;
    };

    //! Some portability magic to detect where to store the TLS key
    template<
        typename StorageT,
        bool IsStoreableV = sizeof(pthread_key_t) <= sizeof(StorageT)
            && alignment_of< pthread_key_t >::value <= alignment_of< StorageT >::value,
        bool IsPointerV = is_pointer< pthread_key_t >::value
    >
    struct pthread_key_traits;

    //! Worst case - the key is probably some structure
    template< typename StorageT, bool IsPointerV >
    struct pthread_key_traits< StorageT, false, IsPointerV >
    {
        typedef typename make_dependent< pthread_key_t, StorageT >::type pthread_key_type;

        static void allocate(StorageT& stg)
        {
            pthread_key_type* pkey = new pthread_key_type;
            if (pthread_key_create(pkey, 0) != 0)
            {
                delete pkey;
                BOOST_LOG_THROW_DESCR(system_error, "TLS capacity depleted");
            }
            stg.as_pointer = pkey;
        }

        static void deallocate(StorageT& stg)
        {
            pthread_key_type* pkey = static_cast< pthread_key_type* >(stg.as_pointer);
            pthread_key_delete(*pkey);
            delete pkey;
        }

        static void set_value(StorageT const& stg, void* value)
        {
            pthread_setspecific(*static_cast< pthread_key_type* >(stg.as_pointer), value);
        }

        static void* get_value(StorageT const& stg)
        {
            return pthread_getspecific(*static_cast< pthread_key_type* >(stg.as_pointer));
        }
    };

    //! The key is a pointer
    template< typename StorageT >
    struct pthread_key_traits< StorageT, true, true >
    {
        typedef typename make_dependent< pthread_key_t, StorageT >::type pthread_key_type;

        static void allocate(StorageT& stg)
        {
            if (pthread_key_create(reinterpret_cast< pthread_key_type* >(&stg.as_pointer), 0) != 0)
            {
                BOOST_LOG_THROW_DESCR(system_error, "TLS capacity depleted");
            }
        }

        static void deallocate(StorageT& stg)
        {
            pthread_key_delete(reinterpret_cast< pthread_key_type >(stg.as_pointer));
        }

        static void set_value(StorageT const& stg, void* value)
        {
            pthread_setspecific(reinterpret_cast< const pthread_key_type >(stg.as_pointer), value);
        }

        static void* get_value(StorageT const& stg)
        {
            return pthread_getspecific(reinterpret_cast< const pthread_key_type >(stg.as_pointer));
        }
    };

    //! The most probable case - the key is an integral or a structure that contains one
    template< typename StorageT >
    struct pthread_key_traits< StorageT, true, false >
    {
        typedef typename make_dependent< pthread_key_t, StorageT >::type pthread_key_type;

        static void allocate(StorageT& stg)
        {
            if (pthread_key_create(reinterpret_cast< pthread_key_type* >(&stg.as_dword), 0) != 0)
            {
                BOOST_LOG_THROW_DESCR(system_error, "TLS capacity depleted");
            }
        }

        static void deallocate(StorageT& stg)
        {
            pthread_key_delete(*reinterpret_cast< pthread_key_type* >(&stg.as_dword));
        }

        static void set_value(StorageT const& stg, void* value)
        {
            pthread_setspecific(*reinterpret_cast< pthread_key_type const* >(&stg.as_dword), value);
        }

        static void* get_value(StorageT const& stg)
        {
            return pthread_getspecific(*reinterpret_cast< pthread_key_type const* >(&stg.as_dword));
        }
    };

} // namespace

thread_specific_base::thread_specific_base()
{
    typedef pthread_key_traits< key_storage > traits_t;
    traits_t::allocate(m_Key);
    set_content(0);
}

thread_specific_base::~thread_specific_base()
{
    typedef pthread_key_traits< key_storage > traits_t;
    traits_t::deallocate(m_Key);
}

void* thread_specific_base::get_content() const
{
    typedef pthread_key_traits< key_storage > traits_t;
    return traits_t::get_value(m_Key);
}

void thread_specific_base::set_content(void* value) const
{
    typedef pthread_key_traits< key_storage > traits_t;
    traits_t::set_value(m_Key, value);
}

} // namespace aux

BOOST_LOG_CLOSE_NAMESPACE // namespace log

} // namespace boost

#else
#error Boost.Log: unsupported threading API
#endif

#include <boost/log/detail/footer.hpp>

#endif // !defined(BOOST_LOG_NO_THREADS)