Skip to content

Commit 73484d1

Browse files
committed
tls reserve
1 parent 7154e7f commit 73484d1

File tree

8 files changed

+40
-34
lines changed

8 files changed

+40
-34
lines changed

include/boost/leaf/config/tls.hpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,20 @@ namespace tls
3131
// This function may not fail.
3232
unsigned read_current_error_id() noexcept;
3333

34-
// Write p to the TLS for T. The TLS may be allocated dynamically on the
35-
// first call to write_ptr_alloc<T>, but subsequent calls must reuse the
36-
// same TLS.
34+
// Reserve TLS storage for T. The TLS may be allocated dynamically on the
35+
// first call to reserve<T>, but subsequent calls must reuse the same TLS.
36+
// On platforms where allocation is not needed, this function is still
37+
// defined but does nothing.
3738
//
3839
// This function may throw on allocation failure.
3940
template <class T>
40-
void write_ptr_alloc( T * p );
41+
void reserve();
4142

42-
// Write p to the TLS previously allocated for T by a successful call to
43-
// write_ptr_alloc<T>.
44-
//
45-
// This function may not fail.
43+
// Write p to the TLS previously reserved for T by a call to reserve<T>.
44+
// It is illegal to call write_ptr<T> without a prior successful call to
45+
// reserve<T>. This function may not fail.
4646
template <class T>
4747
void write_ptr( T * p ) noexcept;
48-
49-
// Read the T * value previously written in the TLS for T. Returns nullptr
50-
// if TLS for T has not yet been allocated.
51-
//
52-
// This function may not fail.
53-
template <class T>
54-
T * read_ptr() noexcept;
5548
}
5649

5750
} }

include/boost/leaf/config/tls_array.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ namespace detail
100100
BOOST_LEAF_CFG_TLS_INDEX_TYPE tls_index<T>::idx = BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX + 1;
101101

102102
template <class T>
103-
struct BOOST_LEAF_SYMBOL_VISIBLE alloc_tls_index
103+
struct BOOST_LEAF_SYMBOL_VISIBLE reserve_tls_index
104104
{
105105
static BOOST_LEAF_CFG_TLS_INDEX_TYPE const idx;
106106
};
107107

108108
template <class T>
109-
BOOST_LEAF_CFG_TLS_INDEX_TYPE const alloc_tls_index<T>::idx = tls_index<T>::idx = index_counter<>::next<T>();
109+
BOOST_LEAF_CFG_TLS_INDEX_TYPE const reserve_tls_index<T>::idx = tls_index<T>::idx = index_counter<>::next<T>();
110110
}
111111

112112
} }
@@ -137,12 +137,9 @@ namespace tls
137137
}
138138

139139
template <class T>
140-
BOOST_LEAF_ALWAYS_INLINE void write_ptr_alloc( T * p )
140+
BOOST_LEAF_ALWAYS_INLINE void reserve()
141141
{
142-
int tls_idx = detail::alloc_tls_index<T>::idx;
143-
--tls_idx;
144-
write_void_ptr(tls_idx, p);
145-
BOOST_LEAF_ASSERT(read_void_ptr(tls_idx) == p);
142+
(void) detail::reserve_tls_index<T>::idx;
146143
}
147144

148145
template <class T>

include/boost/leaf/config/tls_cpp11.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ namespace tls
7272
}
7373

7474
template <class T>
75-
BOOST_LEAF_ALWAYS_INLINE void write_ptr_alloc( T * p )
75+
BOOST_LEAF_ALWAYS_INLINE void reserve()
7676
{
77-
detail::ptr<T>::p = p;
7877
}
7978

8079
template <class T>

include/boost/leaf/config/tls_globals.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ namespace tls
7070
}
7171

7272
template <class T>
73-
BOOST_LEAF_ALWAYS_INLINE void write_ptr_alloc( T * p )
73+
BOOST_LEAF_ALWAYS_INLINE void reserve()
7474
{
75-
detail::ptr<T>::p = p;
7675
}
7776

7877
template <class T>

include/boost/leaf/config/tls_win32.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,11 @@ namespace tls
377377
}
378378

379379
template <class T>
380-
BOOST_LEAF_ALWAYS_INLINE void write_ptr_alloc(T * p)
380+
BOOST_LEAF_ALWAYS_INLINE void reserve()
381381
{
382382
using namespace detail;
383383
thread_local DWORD const cached_slot = module<>::state.sm().get(type_hash<T>());
384-
DWORD slot = cached_slot;
385-
BOOST_LEAF_ASSERT(slot != TLS_OUT_OF_INDEXES);
386-
BOOL r = TlsSetValue(slot, p);
387-
BOOST_LEAF_ASSERT(r), (void) r;
384+
BOOST_LEAF_ASSERT(cached_slot != TLS_OUT_OF_INDEXES), (void) cached_slot;
388385
}
389386

390387
template <class T>

include/boost/leaf/context.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ namespace detail
180180
template <int I, class Tup>
181181
struct tuple_for_each
182182
{
183-
BOOST_LEAF_CONSTEXPR static void activate( Tup & tup )
183+
BOOST_LEAF_CONSTEXPR static void reserve( Tup & tup )
184+
{
185+
static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
186+
tuple_for_each<I-1,Tup>::reserve(tup);
187+
std::tuple_element<I-1, Tup>::type::reserve();
188+
}
189+
190+
BOOST_LEAF_CONSTEXPR static void activate( Tup & tup ) noexcept
184191
{
185192
static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
186193
tuple_for_each<I-1,Tup>::activate(tup);
@@ -215,6 +222,7 @@ namespace detail
215222
template <class Tup>
216223
struct tuple_for_each<0, Tup>
217224
{
225+
BOOST_LEAF_CONSTEXPR static void reserve( Tup & ) noexcept { }
218226
BOOST_LEAF_CONSTEXPR static void activate( Tup & ) noexcept { }
219227
BOOST_LEAF_CONSTEXPR static void deactivate( Tup & ) noexcept { }
220228
BOOST_LEAF_CONSTEXPR static void unload( Tup &, int ) noexcept { }
@@ -339,6 +347,7 @@ class context
339347
{
340348
using namespace detail;
341349
BOOST_LEAF_ASSERT(!is_active());
350+
tuple_for_each<std::tuple_size<Tup>::value,Tup>::reserve(tup_);
342351
tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_);
343352
#if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
344353
thread_id_ = std::this_thread::get_id();

include/boost/leaf/error.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,15 @@ namespace detail
132132
BOOST_LEAF_ASSERT(tls::read_ptr<slot<E>>() != this);
133133
}
134134

135-
void activate()
135+
static void reserve()
136+
{
137+
tls::reserve<slot<E>>();
138+
}
139+
140+
void activate() noexcept
136141
{
137142
prev_ = tls::read_ptr<slot<E>>();
138-
tls::write_ptr_alloc<slot<E>>(this);
143+
tls::write_ptr<slot<E>>(this);
139144
}
140145

141146
void deactivate() const noexcept
@@ -283,6 +288,7 @@ namespace detail
283288
BOOST_LEAF_ASSERT(last_ != nullptr);
284289
BOOST_LEAF_ASSERT(*last_ == nullptr);
285290
BOOST_LEAF_ASSERT(tls::read_ptr<slot<T>>() == nullptr);
291+
slot<T>::reserve();
286292
capturing_slot_node<T> * csn = new capturing_slot_node<T>(last_, err_id, std::forward<E>(e));
287293
csn->activate();
288294
return csn->value(err_id);
@@ -334,20 +340,24 @@ namespace detail
334340
inline void dynamic_load_( int err_id, E && e )
335341
{
336342
if( slot<dynamic_allocator> * sl = tls::read_ptr<slot<dynamic_allocator>>() )
343+
{
337344
if( dynamic_allocator * c = sl->has_value_any_key() )
338345
c->dynamic_load(err_id, std::forward<E>(e));
339346
else
340347
sl->load(err_id).dynamic_load(err_id, std::forward<E>(e));
348+
}
341349
}
342350

343351
template <class E, class F>
344352
inline void dynamic_accumulate_( int err_id, F && f )
345353
{
346354
if( slot<dynamic_allocator> * sl = tls::read_ptr<slot<dynamic_allocator>>() )
355+
{
347356
if( dynamic_allocator * c = sl->has_value(err_id) )
348357
(void) std::forward<F>(f)(c->dynamic_load(err_id, E{}));
349358
else
350359
(void) std::forward<F>(f)(sl->load(err_id).dynamic_load(err_id, E{}));
360+
}
351361
}
352362

353363
template <bool OnError, class E>

include/boost/leaf/handle_errors.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ namespace detail
719719
try_capture_all_( TryBlock && try_block )
720720
{
721721
detail::slot<detail::dynamic_allocator> sl;
722+
detail::slot<detail::dynamic_allocator>::reserve();
722723
sl.activate();
723724
#ifndef BOOST_LEAF_NO_EXCEPTIONS
724725
try
@@ -780,6 +781,7 @@ namespace detail
780781
try_capture_all_( TryBlock && try_block )
781782
{
782783
detail::slot<detail::dynamic_allocator> sl;
784+
detail::slot<detail::dynamic_allocator>::reserve();
783785
sl.activate();
784786
#ifndef BOOST_LEAF_NO_EXCEPTIONS
785787
try

0 commit comments

Comments
 (0)