2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include "thread_info.h"
25 #include "os/threading.h"
30 #include <boost/thread/tss.hpp>
34 class enumerable_thread_infos
37 std::map<void*, std::weak_ptr<thread_info>> enumerable_;
38 boost::thread_specific_ptr<std::shared_ptr<thread_info>> infos_;
40 static enumerable_thread_infos& get_instance()
42 static enumerable_thread_infos instance;
47 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
49 std::lock_guard<std::mutex> lock(mutex_);
51 std::vector<spl::shared_ptr<thread_info>> result;
52 result.reserve(enumerable_.size());
54 for (auto it = enumerable_.begin(); it != enumerable_.end();)
56 auto strong = it->second.lock();
60 result.push_back(spl::make_shared_ptr(strong));
65 it = enumerable_.erase(it);
69 std::sort(result.begin(), result.end(), [](const spl::shared_ptr<thread_info>& lhs, const spl::shared_ptr<thread_info>& rhs) { return lhs->native_id < rhs->native_id; });
74 thread_info& get_thread_info()
76 auto local = infos_.get();
80 std::unique_ptr<thread_info> p(new thread_info);
81 local = new std::shared_ptr<thread_info>(p.get(), [this](thread_info* p)
83 std::lock_guard<std::mutex> lock(mutex_);
89 std::lock_guard<std::mutex> lock(mutex_);
90 enumerable_.insert(std::make_pair(local->get(), *local));
97 thread_info::thread_info()
98 : native_id(get_current_thread_id())
102 thread_info& get_thread_info()
104 return enumerable_thread_infos::get_instance().get_thread_info();
107 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
109 return enumerable_thread_infos::get_instance().get_thread_infos();