This repository has been archived on 2020-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
tdlib-fork/tdutils/td/utils/port/detail/ThreadIdGuard.cpp
levlam 80c35676a2 Update copyright year.
GitOrigin-RevId: 09afb551b6e637dc69739fa735b0051a38b9e14c
2020-01-01 04:23:48 +03:00

53 lines
1.4 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// 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"
#include "td/utils/common.h"
#include "td/utils/port/thread_local.h"
#include <mutex>
#include <set>
namespace td {
namespace detail {
class ThreadIdManager {
public:
int32 register_thread() {
std::lock_guard<std::mutex> guard(mutex_);
if (unused_thread_ids_.empty()) {
return ++max_thread_id_;
}
auto it = unused_thread_ids_.begin();
auto result = *it;
unused_thread_ids_.erase(it);
return result;
}
void unregister_thread(int32 thread_id) {
std::lock_guard<std::mutex> guard(mutex_);
CHECK(0 < thread_id && thread_id <= max_thread_id_);
bool is_inserted = unused_thread_ids_.insert(thread_id).second;
CHECK(is_inserted);
}
private:
std::mutex mutex_;
std::set<int32> unused_thread_ids_;
int32 max_thread_id_ = 0;
};
static ThreadIdManager thread_id_manager;
ThreadIdGuard::ThreadIdGuard() {
thread_id_ = thread_id_manager.register_thread();
set_thread_id(thread_id_);
}
ThreadIdGuard::~ThreadIdGuard() {
thread_id_manager.unregister_thread(thread_id_);
set_thread_id(0);
}
} // namespace detail
} // namespace td