2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/detail/ThreadIdGuard.h"
|
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-11-22 22:38:16 +01:00
|
|
|
#include "td/utils/ExitGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
|
|
|
|
#include <mutex>
|
2018-07-17 01:46:44 +02:00
|
|
|
#include <set>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
class ThreadIdManager {
|
|
|
|
public:
|
|
|
|
int32 register_thread() {
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2018-07-17 01:46:44 +02:00
|
|
|
if (unused_thread_ids_.empty()) {
|
|
|
|
return ++max_thread_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-17 01:46:44 +02:00
|
|
|
auto it = unused_thread_ids_.begin();
|
|
|
|
auto result = *it;
|
|
|
|
unused_thread_ids_.erase(it);
|
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void unregister_thread(int32 thread_id) {
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2018-07-17 01:46:44 +02:00
|
|
|
CHECK(0 < thread_id && thread_id <= max_thread_id_);
|
|
|
|
bool is_inserted = unused_thread_ids_.insert(thread_id).second;
|
|
|
|
CHECK(is_inserted);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex mutex_;
|
2018-07-17 01:46:44 +02:00
|
|
|
std::set<int32> unused_thread_ids_;
|
|
|
|
int32 max_thread_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
static ThreadIdManager thread_id_manager;
|
2020-11-22 22:38:16 +01:00
|
|
|
static ExitGuard exit_guard;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
ThreadIdGuard::ThreadIdGuard() {
|
|
|
|
thread_id_ = thread_id_manager.register_thread();
|
|
|
|
set_thread_id(thread_id_);
|
|
|
|
}
|
|
|
|
ThreadIdGuard::~ThreadIdGuard() {
|
2020-11-22 22:57:52 +01:00
|
|
|
if (!ExitGuard::is_exited()) {
|
|
|
|
thread_id_manager.unregister_thread(thread_id_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
set_thread_id(0);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|