2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/Destructor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
#if TD_GCC || TD_CLANG
|
|
|
|
#define TD_THREAD_LOCAL __thread
|
|
|
|
#elif TD_INTEL || TD_MSVC
|
|
|
|
#define TD_THREAD_LOCAL thread_local
|
|
|
|
#else
|
|
|
|
#warning "TD_THREAD_LOCAL is not defined, trying 'thread_local'"
|
|
|
|
#define TD_THREAD_LOCAL thread_local
|
|
|
|
#endif
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
// If raw_ptr is not nullptr, allocate T as in std::make_unique<T>(args...) and store pointer into raw_ptr
|
|
|
|
template <class T, class P, class... ArgsT>
|
|
|
|
bool init_thread_local(P &raw_ptr, ArgsT &&... args);
|
|
|
|
|
|
|
|
// Destroy all thread locals, and store nullptr into corresponding pointers
|
|
|
|
void clear_thread_locals();
|
|
|
|
|
|
|
|
void set_thread_id(int32 id);
|
|
|
|
|
|
|
|
int32 get_thread_id();
|
|
|
|
|
|
|
|
namespace detail {
|
2018-09-27 03:19:03 +02:00
|
|
|
void add_thread_local_destructor(unique_ptr<Destructor> destructor);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class T, class P, class... ArgsT>
|
|
|
|
void do_init_thread_local(P &raw_ptr, ArgsT &&... args) {
|
|
|
|
auto ptr = std::make_unique<T>(std::forward<ArgsT>(args)...);
|
|
|
|
raw_ptr = ptr.get();
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
detail::add_thread_local_destructor(create_destructor([ptr = std::move(ptr), &raw_ptr]() mutable {
|
2018-12-31 20:04:05 +01:00
|
|
|
ptr.reset();
|
|
|
|
raw_ptr = nullptr;
|
|
|
|
}));
|
2018-09-07 02:41:21 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <class T, class P, class... ArgsT>
|
|
|
|
bool init_thread_local(P &raw_ptr, ArgsT &&... args) {
|
|
|
|
if (likely(raw_ptr != nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
detail::do_init_thread_local<T>(raw_ptr, std::forward<ArgsT>(args)...);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|